I'm looking at the code from
msg #19. While it's better (and much easier to read the code now
), the parsing for the message start sequence is still not what it really should be.
You're *still* taking the approach of reading 3 bytes before checking ANY of them.
Code:
input_bytes = 0;
while (!(input_bytes >= 3)) {
if ((Serial1.available() > 0) && (input_bytes < 3)) {
Incoming_Data.vData[input_bytes] = Serial1.read();
input_bytes++;
}
}
// check if the first three bytes are correct
if (checkPacket_start(Incoming_Data.vData)) {
When "input_bytes" is 0, 1 or 2, you should read 1 byte. If that 1 byte is not exactly the expected byte of your 3 byte sequence, you should immediately set input_bytes back to zero. You should use "continue" or otherwise begin looking for the first byte. Do not call checkPacket_end() after you set input_bytes back to zero. You know there isn't any data, but your code does this anyway, perhaps rechecking the message in the buffer from last time?
Anyway, the point is when "input_bytes" is zero, read 1 byte and check it. Increment "input_bytes" to 1 only if that byte is correct. Then read 1 more, and check it. The idea is to immediately put "input_bytes" back to zero when you receive any byte that isn't the correct start sequence WITHOUT READING MORE BYTES after the bad one.
When something goes wrong, the very next byte might be the true beginning of a packet. Never read beyond a bad byte and discard the stuff you didn't use. Always stop when something is bad and treat the very next byte after a wrong start as the possible new start of a good message.