none
.NET Error Closing serial port BaseStream error is only available when the port is open RRS feed

  • Question

  • I'm using the .NET System.IO.Ports.SerialPort using the BaseStream as suggested in this post If you must use .NET System.IO.Ports.SerialPort

    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:

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

    and the error occurrs when I try to close the window / port / basestream here, 

       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!!


    Friday, January 16, 2015 5:21 PM

Answers

  • Hello franco,

    With your current provided code, it is hard to state something more specific about this exception. If it is possible, please provide a small demo sample code which could reproduce this issue.

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

    From my experience, I would try to close the stream as soon as I obtain the data stored in the stream. Do you have a try to close stream as soon as after obtaining data from it to see if the stream could be closed successfully rather than closing it in the Window_Closing event? I  suggest that you could have a try.

    Since we cannot exactly reproduce this issue, here is a blog which might help you debug this issue deeply, you could debug into the .NET source code:

    http://blogs.msdn.com/b/dotnet/archive/2014/02/24/a-new-look-for-net-reference-source.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 3:17 AM
    Moderator