title: Ascii control characters SOH, DLE, STX, ETX : How to escape binary data over a UART?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The problem: either I'm not understanding it correctly, or it's not capable of doing what I want.</p>
  
  <p>The Wikipedia page on control characters says a packet could be sent like this:</p>
  
  <ol>
  <li>&lt; DLE > &lt; SOH > - data link escape and start of heading</li>
  <li>Heading data</li>
  <li>&lt; DLE > &lt; STX > - data link escape and start of text</li>
  <li>&lt; payload ></li>
  <li>&lt; DLE > &lt; ETX > - data link escape and end of text</li>
  </ol>
  
  <p>But what if the payload is binary data containing two consecutive bytes equivalent to DLE and ETX? how should those bytes be escaped?</p>
  
  <p>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.</p>
  
  <p>Error checking will happen at a higher level to ensure that a received packet is valid - unless ASCII standads can solve this too</p>