I want to write a binary transfer to/from Arduino to the other side through USB. Right now, I'm doing it in ASCII, and it's easy. With ASCII, you can see the junk until the two sides synchronize. But with binary, it will be difficult to distinguish data from noise. Is there some standard way to reset the connection and get a reliable byte stream out? Can I send a break and force the Arduino to reset somehow? Just looking for a suggestion on best practices.
2 Answers
For reliable communications, you need to include some protocol with your binary data, so that
- you know when things have gone wrong
- you can get back in sync
A detailed description of serial framing can be found here
As an example, the point-to-point protocol (PPP) provides a simple method of framing sections of data by surrounding them with a special character which is guaranteed never to occur on it's own in the binary stream (there is an escaping mechanism to allow this to happen). The receiver can use the framing characters to synchronise.
You could (depending on how bad it is if erroneous data gets through) add a checksum or CRC to your messages also - PPP calls this the frame-check sequence or FCS.

- 1
- 1

- 16,395
- 1
- 38
- 56
If you really want to roll your own protocol, which you want to be true 8-bit capable, there are a few things you have to do. If you were in a ideal world where data never got lost it's simpler, but you are not!
- you have to escape two character bytes, say X and Z. Every time you want to send a X instead you send Z1 and when you want to send a Z you send Z2. that way you will never send a plain X
- Now you can start every message withe a completely unique identifier, X
- the receiver can now wait for a X, and start collecting bytes. every time it sees a Z it will have to read an extra byte and decode the number to get the real byte
- Now start every message with a header explaining how much data the message will contain, and a checksum. Reader should stop reading when it have the specified number and discard everything if checksum fails.
- right after the header, you put your data
Be aware that using structs to serialize data is not considered safe, and will often break if you cross architecture boundaries, where packing are different.

- 4,441
- 1
- 18
- 22