dotnet/runtime

SerialPort constantly throws IOException

Open

#113.296 aperta il 8 mar 2025

Vedi su GitHub
 (14 commenti) (3 reazioni) (0 assegnatari)C# (5445 fork)batch import
area-System.IO.Portshelp wanted

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

Description

The SerialPort seems to be broken in .NET 9. It throws this exception all the time: System.IO.IOException: "The I/O operation has been aborted because of either a thread exit or an application request."

This code has worked wonderfully in older versions of .NET but now it only fails. I can read and write from the serial port synchronously, i.e. wait for a response after sending a request. But for this project I need constant awareness on the port to always handle incoming data.

See attached demo code that reproduces the bug immediately. SerialPort is unusable like this. Run the project with the name of a serial port as only command line argument.

Reproduction Steps

Run attached code.

Expected behavior

Reading whatever bytes have been received.

Actual behavior

System.IO.IOException: "The I/O operation has been aborted because of either a thread exit or an application request."

Regression?

I think so.

Known Workarounds

None

Configuration

.NET 9 Windows 11 x64 Not tested anything else

Other information

The web suggests that something is tampering with the serial port. But that's not the case. I can verify with PuTTY that the serial port is working fine and receiving data. I can also send data over the port from .NET (ignoring these exceptions) and the data is properly received on the other end. I just can't receive any of the data that is sent, and I have these exceptions in a very tight loop.

SerialPortTest.zip

Guida contributor