C# .NET Serial Port hang on close function.

Joined
Mar 1, 2023
Messages
1
Reaction score
0
I create a simple application (.NET 6 on Ubuntu) that works with usb to serial port converter by using System.IO.Port.
I open the port, then i remove usb to serial converter from computer, finally I close the port. My application hang at Close function.
Is there any workaround to fix it?
 
Joined
Mar 5, 2023
Messages
36
Reaction score
12
It is possible that the Close() function is hanging because it is waiting for data to be sent or received on the serial port. When you remove the USB to serial converter, the port is no longer available and the Close() function may be waiting indefinitely for the data transfer to complete.

One workaround is to set a timeout for the Close() function using the ReadTimeout and WriteTimeout properties of the SerialPort object before opening the port. For example:

Code:
using System.IO.Ports;


SerialPort port = new SerialPort("/dev/ttyUSB0", 9600);
port.ReadTimeout = 1000;
port.WriteTimeout = 1000;
port.Open();
// Do some data transfer
port.Close();

In the above example, the ReadTimeout and WriteTimeout properties are set to 1000 milliseconds (1 second) to specify that the Read() and Write() functions should not block for more than 1 second.

Another workaround is to handle the SerialPort.ErrorReceived event, which is raised when there is an error on the serial port, such as when the USB to serial converter is removed. You can close the port in the event handler to avoid hanging at the Close() function. For example:

Code:
using System.IO.Ports;


SerialPort port = new SerialPort("/dev/ttyUSB0", 9600);
port.ErrorReceived += (sender, e) =>
{
    if (e.EventType == SerialError.RXOver || e.EventType == SerialError.Overrun || e.EventType == SerialError.Frame || e.EventType == SerialError.RXParity)
    {
        // Handle errors
    }
    else if (e.EventType == SerialError.SerialErrorReceived)
    {
        // Close the port
        port.Close();
    }
};
port.Open();
// Do some data transfer
port.Close();

In the above example, the SerialPort.ErrorReceived event is handled to check for the SerialError.SerialErrorReceived event type, which indicates that there is an error on the serial port. When this event is raised, the port is closed to avoid hanging at the Close() function.

Note that these workarounds may not be sufficient in all cases, depending on the specific behavior of the USB to serial converter and the serial port. It is recommended to test thoroughly and handle errors appropriately to ensure the robustness of the application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top