Here is my version of serial.flush(). My application expects two frames of 5 byte data, the fifth byte of each frame being an directional code (sync1, sync2). I test these 5th bytes to see if they are what I expect, if not it is a bad frame and the causes can be extra character(s) or dropped character(s). To recover synchronization with the process, I needed to flush the serial buffer (overrun).
start:
(buffer 1 and buffer 2 fill code)
// validation of data frames If sync1 or sync2 are not present clear the buffer for new frames
if ((buffer1[4] != sync1) || (buffer2[4] != sync2)) {
delay(25); // delay to give Serial.available a bit of room to set
if (Serial.available() > 0) {
// Overrun
while(Serial.read() != -1) { //flush the buffer
delay(10); // may not be necessary and if used set based on baud rate
}
}
goto start;
}
(further processing.....)
My Serial flush() that works well
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
Please be positive and constructive with your questions and comments.