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

disconnect /reconnect #92

Closed maas78 closed 5 years ago

maas78 commented 5 years ago

Hi, Is it possible to handle disconnect/reconnect on usb-to-com like usb scanner. If I unplug the device and replug I need to remake the connection manually. I didn't find any event to handle disconnection, the "errors event" doesn't trigger it.

jcurl commented 5 years ago

When you "read()" and ReadTimeout==Timeout.Infinite, you'll get zero when the device is unplugged and there is no more data in the buffer. The value of zero on read is defined as "end of file". If you read again, you'll get an exception (regardless also of the ReadTimeout parameter).

You will have to make the connection manually again, by closing the serial port first, then reopening it. Closing the serial port is required to release/free any resources.

If you need USB notifications of insertion/removal, you'll have to find out how to do this yourself, that is outside of the scope of this library, it handles reading/writing serial ports but not insertion/removal of devices. That would be for a USB specific library, which this one is not.