I am a bit desperate meanwhile because I cannot resolve the issue and it seems there is a bug in synaser/LazSerial:
To summarize:
- I need to readout data from sensors
- According to the datasheet, the sensors should send every 1.7 seconds 25 bytes. Tests revealed however, that some sensors send every 2.0, some even only every 2.2 seconds. So I cannot use a fixed readout timer.
- I read now every 2.5 seconds and do this:
// we want the latest/last data
while serSensor.WaitingData > 24 do
k:= serSensor.RecvBuffer(@dataArray[0], 25);
// attempt to completely empty the buffer by reading the remaining bytes
while serSensor.WaitingData > 0 do
serSensor.RecvByte(100);
I do the latter because I debugged that after about 5 minutes, after the last 25 byte readouts, there are 22 or 19 bytes left.
The problem is now that after about an hour, WaitingData tells me it has no bytes to be read, but when I nevertheless readout 25 bytes, I get 25 bytes and their checksum is correct.
Therefore, hour by hour I don't get the current sensor data but some older data.
I have no idea how to fix this.
After hours of debugging I also found out that when I have after
while serSensor.WaitingData > 24 do
k:= serSensor.RecvBuffer(@dataArray[0], 25);
e.g. 22 bytes left and readout 25, I get 25 bytes and their checksum is OK.
So my problem is that I cannot rely on WaitingData since it reports me often wrong info.
Can I flush the buffer somehow? Or is there a way to readout bytes until I get an error, that there are really no bytes? If so, how can I do this assuring that the COM connection is not closed?