Jayy
Jayy

Reputation: 14798

Ascii control characters SOH, DLE, STX, ETX : How to escape binary data over a UART?

I want a simple, light-weight way for two basic 8-bit MCUs to talk to each other over an 8-bit UART connection, sending both ASCII characters as 8-bit values, and binary data as 8-bit values.

I would rather not re-invent the wheel, so I'm wondering if some ASCII implementation would work, using ASCII control characters in some standard way.

The problem: either I'm not understanding it correctly, or it's not capable of doing what I want.

The Wikipedia page on control characters says a packet could be sent like this:

  1. < DLE > < SOH > - data link escape and start of heading
  2. Heading data
  3. < DLE > < STX > - data link escape and start of text
  4. < payload >
  5. < DLE > < ETX > - data link escape and end of text

But what if the payload is binary data containing two consecutive bytes equivalent to DLE and ETX? how should those bytes be escaped?

The link may be broken and re-established, so a receiving MCU should be able to start receiving mid-packet, and have a simple way of telling when the next packet has begun, so it can ignore data until the end of that partial packet.

Error checking will happen at a higher level to ensure that a received packet is valid - unless ASCII standads can solve this too

Upvotes: 3

Views: 7455

Answers (2)

JMercer
JMercer

Reputation: 372

I understand this is an old question but I thought I should suggest Serial Line Internet Protocol (SLIP) which is defined in RFC 1055. It is a very simple protocol.

Upvotes: 0

Ivan Kosarev
Ivan Kosarev

Reputation: 334

Since you are going to transfer binary data along with text messages, you indeed would have to make sure the receiver won't confuse control bytes with payload contents. One way to do that is to encode the payload data so that none of the special characters appear on the output. If the overhead is not a problem, then a simplest encoding like Base16 should be enough. Otherwise, you may want to take a look at escapeless encodings that have been specifically designed to remove certain characters from encoded data.

Upvotes: 0

Related Questions