Running on Windows 10 with .NET 4.7.2 and virtual serial ports, I have a SerialPort
DataReceived
handler that looks like:
private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
_serialPort.ReadTimeout = 10000;
while (true)
{
try
{
var nextByte = _serialPort.ReadByte();
Debug.WriteLine(((byte)nextByte).ToString("X2"));
continue;
}
catch (TimeoutException)
{
}
break;
}
}
If I write a byte to the port it appears immediately in the debug window, and because of the while loop and continue, ReadByte()
is called again where it will block until the 10 second timeout elapses. If I write more data to the port during that time, ReadByte()
does not return immediately. Instead, it continues to block until the full timeout has elapsed, and only then returns the newly written byte.
Why would it not return as soon as new data is available?