I have developed a testing program in C# that communicates with a certain device via serial port to exchange data. I open and close the serial port to transmit and receive data using Open() and Close(). Sometimes, when I call Open() or Close(), it executes immediately, but other times it hangs for about 30 seconds or 1 minute.
While it’s not a problem to keep the serial port open from the program’s start until it’s closed, my testing concept involves performing tests with different devices, meaning I need to open and close the serial port for each test item. If there’s a hang during these operations, it becomes difficult to determine if the test item was executed successfully.
I tried using the ‘SerialPortStream’ library, a third-party library, but it also could not avoid the hang.
Is there a way to modify the code in this C# program to avoid the hang phenomenon when the serial port must be repeatedly opened and closed during program operation?
Or the FW (or driver) of the communicating in the device should solve this hang problem?