I am trying to implement an UART protocol to make communication between two boards. data are ascii encoded and messages length's are variables. I've chosen to implement a packet in order to "encapsulate" information.
Packet: {STX, DATA, ETX}.
STX is "Start of text" in ASCII table. Its Hex code is 0x02
, ETX is "End of text". Its Hex code 0x03
. Every byte in DATA field can take any value from 0x00
to 0x7F
except 0x02
and 0x03
.
Problem 1: Data length
How to send variable data lengths over uart?
Example: {STX, Hello\0, RTX} and {STX, This is a long text\0, RTX}
I've been digging in the internet and I found that the best approach is to use a FIFO circular buffer. The problem is how to fix BUFFER_SIZE
in this case? Let's say that I've done some maths and I figured out that the longest message I am going to transmit over UART is 90 Bytes and the shortest one is 4 Bytes. should I #define BUFFER_SIZE 90
or I need to take a larger buffer?
In both cases how does FIFO works in these cases:
Case 1: DATA length < BUFFER_SIZE.
Example: {STX, ABCD, ETX} and BUFFER_SIZE = 90 Bytes.
Case 2: Buffer is full but data are still coming in.
Example: BUFFER_SIZE = 13
Packet: {STX, READ_X, ETX}{STX, REMOVE_Y,ETX}.
In HEX, these messages are
0x02 0x52 0x45 0x41 0x44 0x5f 0x58 0x00 0x03 0X02 0x52 0x45 0x4d 0x4f 0x56 0x45 0x5f 0x59 0x00 0x03
Buffer is full when we reach 0x040x4d
(in Italic). How does it deal with this situation?