I have an application which is supposed to communicate with multiple custom devices, some of which use FTDI USB-to-serial converter chips, and some of which use TCP. The application needs to be able to accept data at any time, from devices which may be plugged or unplugged at any time; the application serves as a bridge between devices and a database.
It seems that when a device is unplugged, it will often cause the SerialPort class to throw an exception in a BackgroundWorker thread and crash the application.
My present remedy, which is probably absurdly complicated, is to have a helper application send/receive serial port data and relay it to/from a TCP socket. When my main application sees that a USB device is plugged in, it launches this other application and then uses a TCP socket to communicate with it. If multiple USB devices are plugged in, a separate instance of the helper application will be launched for each. When a USB port is unplugged, the helper application will crash, but the "Unexpected shutdown" message will be stifled.
This approach works, but it seems massively unsatisfying. There's got to be a better way.