jcurl / RJCP.DLL.SerialPortStream

SerialPortStream is an independent implementation of System.IO.Ports.SerialPort and SerialStream for better reliability and maintainability. Default branch is 2.x and now has support for Mono with help of a C library.
Microsoft Public License
624 stars 197 forks source link

Incorrect IsOpen value #69

Closed VPeruS closed 5 years ago

VPeruS commented 5 years ago

After device manual disconnection IsOpen property of SerialPortStream continue to return true. We using USB to COM converted, host system Windows 7.

Code that was used for testing

        static void TestRJCP()
        {
            var s = new SerialPortStream("COM4");

            s.Open();

            while (true)
            {
                foreach (var portName in SerialPortStream.GetPortNames())
                {
                    System.Console.Write($"{portName} ");
                }
                System.Console.WriteLine();

                System.Console.WriteLine($"{s.IsOpen}");
                if (s.IsOpen)
                {
                    try
                    {
                        s.WriteByte(1);
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine($"{ex}");
                    }
                }
            }
        }

After disconnect s.WriteByte will continuously throw exception

jcurl commented 5 years ago

Thank you for the report. Even if the underlying device driver is removed, the object still needs to be closed to free up resources - the library doesn't do this automatically (that would lead to memory errors). Have you tested the MS implementation? If this differs to MS, then I'll certainly consider a change to improve compatibility

VPeruS commented 5 years ago

Yes, it different from MS's, their IsOpen starts to returns false after device removed.

jcurl commented 5 years ago

Thankyou. I'll consider this a bug.

jcurl commented 5 years ago

Please note, that the IsOpen now returns false when the device is physically removed, but the SerialPortStream still needs to be closed. Resources are still being held.