I have an application that needs to interact with several custom devices, some of which use FTDI USB-to-serial chips, and some of them use TCP. The application should be able to receive data at any time, from devices that can be connected or disconnected at any time; The application serves as a bridge between devices and the database.
It appears that when the device is disconnected from the network, this often leads to the SerialPort class throwing an exception in the BackgroundWorker thread and crashing the application.
My real tool, which is probably absurdly complicated, is to have a helper application to send / receive serial port data and relay it to / from a TCP socket. When my main application sees that a USB device is connected, it launches this other application and then uses a TCP socket to communicate with it. If several USB devices are connected, a separate instance of the auxiliary application will be launched for each. When the USB port is disconnected, the helper application crashes, but the message “Unexpected shutdown” is suppressed.
This approach works, but it seems massively unsatisfactory. There must be a better way.
source
share