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
639 stars 199 forks source link

WinNativeSerial.IsOpen flag is always true #6

Closed vadrianc closed 8 years ago

vadrianc commented 8 years ago

After physically severing the serial connection, the WinNativeSerial.IsOpen property is always true. I have to mention that I'm using a USB to Serial adapter for testing. Regardless if I remove just the serial cable or disconnect the USB to Serial hub from the PC, the IsOpen flag is always true.

Regards, Adrian V.

jcurl commented 8 years ago

I have to check, but when removing a device, I would guess that the main loop in CommOverlappedIO would die. There's an internal flag called IsRunning that probably should be checked.

However, the IsOpen flag should (and must) remain true, even if the device is no longer present, as it's the responsibility of the class calling Open() to also call Close() or Dispose(). The SerialPortStream should not close itself. A similar example can be seen in Network sockets. The remote side closes and all operations should result in an exception, but doesn't close itself.

It should however detect such a situation and raise exceptions (probably IOException) on Read/Write/Flush and other methods that might indicate that the connection is lost.

jcurl commented 8 years ago

So they way Read() will work is that if the device is returned, it will not raise an exception immediately, but return the number of bytes in the buffer, or zero for end of file. This means if you use blocking behaviour, you can conveniently know when we got to the end by reading zero. That's the specification and behaviour of other streams.

To not confuse a return value of zero on Read() with that of a timeout, the first return is zero, the second is an IOException which is similar to how sockets work (they return zero on read, the second is EPIPE).

Write should raise an exception immediately.

Now to write and test :)

jcurl commented 8 years ago

Please test. Closing with commit 30919dd9cefa213ae1df4e4fd7aedee634c7d993