I have a serial device that sends a 348 bytes data block upon reset.
Using the following code :
private void Uart_DataReceived(UartController sender, DataReceivedEventArgs e)
{
Debug.WriteLine($"BTR = {_uart.BytesToRead}");
Debug.WriteLine($"e.Count = {e.Count}");
var btr = _uart.BytesToRead;
var _buffer = new Byte[btr];
while (btr != 0)
{
_uart.Read(_buffer, 0, btr);
btr = _uart.BytesToRead;
}
}
I get the following result(s) :
BTR = 348
e.Count = 1
BTR = 0
e.Count = 112
BTR = 0
e.Count = 141
BTR = 0
e.Count = 51
BTR = 0
e.Count = 43
Launching the program many times (like 10 times), I always get BTR= 348 (expected) and many DataReceived events (generally 5 or 6 events), with first one always giving e.Count = 1.
Last event e.Count value of 43 is not systematic, although it appears to be the same 90% of the time.
So my questions are :
- why e.Count is not equal to _uart.BytesToRead() in the first event ?
- why e.Count is always 1 in the first event received ?
- why are there so many DataReceived events thrown even though there are no more BytesToRead after first read ? (I’ve tried to ClearReadBuffer() but it does not change anything)