How to write a parser for UART 'control packets'?
Hi all
I've just jumped on the STM32 bandwagon so apologies if this post isn't in the right place. Just let me know and I'll go annoy the right people :)
I've done some work with the ATMega AVR range, both via Arduino and native so I'm not a complete newbie. However I'm not an experienced MCU programmer.
I've started a project where I need to communicate with an existing product via UART. The existing product uses a fairly simple 'control packet' format i.e. each update is in the form
0xEE 0x?? 0x?? 0x00 0x00i.e. header, data byte, data byte, terminator, terminator and expects an ACK of 0xF5.
Obviously it wouldn't be hard to write a basic routine to
- read the first byte received
- if it is 0xEE read the next four bytes and ensure the last two are 0x00 then deal with the payload bytes
- if the first byte is 0xF5 treat it as an ACK and continue
However, that'll clearly only work well if nothing goes wrong and things don't get out of sequence or data corrupted, etc. I'm wondering how to deal with errors such as only receiving two bytes and not locking up the interface waiting for the rest of the message, etc.
I'm guessing this is a fairly standard requirement and there are tried and tested methods of dealing with it but I'm having trouble finding any resources. The main difficulty is probably terminology, I'm not sure what I'm looking for. Searching for UART and 'control packets' leads to any number of articles about how the async serial protocol works.
My requirement isn't high bandwidth, the existing device is configured for 115,200 baud and a couple of control packets per second would be a busy period (although they do tend to come in batches of three or four).
If anyone could point me towards the right terminology and / or resources I'd greatly appreciate it.
Tommy
