Escapes (DLEs) are correctly handled by a special state.The start and end flags are not inserted into the data buffer.The "stray" byte before the header is ignored: according to the protocol each frame has to start with a header, so this isn't part of the frame.
'Data buffer' is the currently accumulated message buffer to pass to an upper level: In the following diagram, 'Receiver state' is the state of the receiver after the received byte. For demonstration, assume that the first byte the receiver draws from the serial port is not a real part of the message (we want to see how it handles this).
Let's see how the receiver handles such a frame. The sender's data link layer turns the data into the frame shown in (b) - start and end flags are added, and in-message flags are escaped. The data contains two flags that need to be escaped - an end flag at position 2 (counting from 0, of course!), and a DLE at position 4. Suppose we define the following protocol:Īnd the sender wants to send the following data message (let's ignore its contents for the sake of the example - they're really not that important). Let's now see a completely worked-out example that demonstrates how this works. This scheme is quite robust: any lost byte (be it a flag, an escape, a data byte or a checksum byte) will cause the receiver to lose just one frame, after which it will resynchronize onto the start flag byte of the next one. This is customarily the last byte (or last word) of the frame, referring to all the bytes in the frame (in its un-stuffed form). The only really required part of the data is some kind of error checking - a checksum, or better yet a CRC. Note that we didn't specify what the data is - it's arbitrary and up the the protocol to decide. The receiver removes the first one but keeps the second one.
We want to be able to send multiple-byte data frames between the communicating parties. This is simple and convenient, but hardly sufficient in the general sense. The "chunks" of data in the protocol between the sender and receiver are single bytes. Notice that the sender script (sender_sim.py) is just sending one byte at a time.
#RELIABLE SERIAL COMMUNICATION PROTOCOL HOW TO#
In the previous post we've seen how to send and receive data on the serial port with Python and plot it live using a pretty GUI.