Content originally posted in LPCWare by triffid_hunter on Sat Jan 18 02:22:40 MST 2014
if the second byte gets corrupted, then the UART will flag a framing error or similar. Then your code waits to receive a second byte, and instead eats the first byte of the next packet.
You want to establish a protocol- perhaps a preamble byte, your data payload, then a checksum byte.
Since you never know which bytes could be lost, or whether you're receiving junk bytes, you must keep checking for new preamble bytes, and only accept valid frames with correct checksum
uarts and similar should always be hooked up to a ringbuffer, then you can drain the buffer from mainloop and crunch data there.