Delimiter & Escape Bytes

by Coach Cindy

January 17 2020 Robotics


Summary

This article is based on Ethan's teaching that introduced delimiter and escape bytes.


Full text

Serial communication is the process of sending data one bit at a time, sequentially, over the Arduino's built-in digital pins. That is, the data transferred between the sender and receiver are single bytes. This is simple and convenient, but hardly sufficient in the general sense. We want to be able to send multiple-byte data frames between the communicating parties.

However, since the receiver is just receiving a stream of bytes from the serial port, how does it know when a message begins or ends?

It looks like we need to frame the data before sending it out. What we can do is to create frames by appending DELIMITERS at the beginning and end of the data chunk.

To make it simple, let's use 0xAA as our delimiter byte that denotes the start and end of a frame.

For example, to send a message with 0x68, 0x65, 0x65 in it we want to frame the data shown below:

But what if 0xAA appears somewhere in the data? The receiver will get confused and end the message.

A simple solution for this is Escaping. Here is how it works with the chosen escape byte (ESC):

  • Whenever a delimiter byte appears in the data, we shall
    • insert a ESC before it;
    • change the delimiter byte to a different value.
  • When the receiver sees an ESC, it will
    • ignore ESC and not insert it into the actual data received;
    • convert the byte following ESC back to the original value, in this case, the delimiter byte

The same process will be followed if ESC itself has to appear in the data.

In another example, the data to be sent over has both 0xAA and 0xAB in it. In other words, the data contains both the delimiter byte and the escape byte.

As we can see, the Delimiter (0xAA) following the Escape byte need to be changed to a different value so that it doesn't signify the end of the message. You can define the rule for changing the value on your own, just make sure that the receiving part has to follow the corresponding rule to change it back to the original data. Hence, to use the same example, we have the following flow between both sender and receiver:

Now we figured out how to use delimiter and escape bytes in data frames during serial communications. Next we will introduce State Machine that tells the system what to do based on the present state.




About the author


Coach Cindy - BananaBots head coach

Cindy has years of experience in IT and project management. As the lead supervisor of BananaBots, she hopes that, one day, the younger generation will show their true power through their efforts and persistence.


More posts by Coach Cindy






More posts under Robotics