Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
CSS
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 &quot;The BaseStream is only available when the port is open&quot;

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();
            }
            
        }



When I try to close the window, is where the error occurs, check this:

C#
//<-- here is where the error occurs -->
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
        serial.BaseStream.Flush();
        serial.BaseStream.Close();
        serial.Close();            
         }



I read somewhere that the serial port should be close on a different thread but I can't find that, so any thoughts??

Thanks!!
Posted

1 solution

I would think that tou should only close the serial port but not the base stream as it will get close anyway. The error is probably caused by the fact that serial port assumes that the stream is not closed directly.

Thus you should consider that the stream is managed by the serial port and call open or close only on the port.
 
Share this answer
 
Comments
Ferz Franco 16-Jan-15 13:36pm    
already tried that but it is has the same result
Philippe Mori 16-Jan-15 14:11pm    
With only serial.Close();?
Have you tried : if (serial.IsOpen) { serial.Close(); }?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900