I'm using the .NET System.IO.Ports.SerialPort using the BaseStream as suggested in this post <a href="http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport">If you must use NET SerialPort</a> But when I try to close the port or the baseStream, an System.InvalidOperationException is raised saying "The BaseStream is only available when the port is open" This is my code: <pre lang="c#"> private void ActionStarted() { //ajusta el puerto setupSerial(); serial.Open(); //conecta al plc byte[] buffer = new byte[15]; Action kickoffRead = null; if (serial.IsOpen) //si esta abierto el puerto hace todo esto { kickoffRead = delegate() { serial.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { int actualLength = serial.BaseStream.EndRead(ar); byte[] received = new byte[actualLength]; Buffer.BlockCopy(buffer, 0, received, 0, actualLength); raiseAppSerialDataEvent(received); } catch { } kickoffRead(); }, null); }; kickoffRead(); } }
//<-- here is where the error occurs --> private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { serial.BaseStream.Flush(); serial.BaseStream.Close(); serial.Close(); }
serial.Close();
if (serial.IsOpen) { serial.Close(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)