As asynchronous serial communication is widely spread among electronic devices even nowadays, I believe many of us have encountered such a question from time to time. Consider an electronic device
D and a computer
PC connected with serial line (RS-232 or similar) and required to exchange information continuously. I.e.
PC is sending a command frame each
X ms, and
D is replying with status report/telemetry frame each
Y ms (The report can be sent as response to requests or independently - doesn't really matter here). The communication frames can contain any arbitrary binary data. Assuming the communication frames are fixed-length packets.
As the protocol is continuous, the receiving side might loose the synchronization or just "join" in the middle of an ongoing sent frame, so it just won't know where the start of frame (SOF) is. A the data has different meaning based on its position relatively to the SOF, the received data will become corrupted, potentially forever.
The required solution
Reliable delimiting/synchronization scheme to detect the SOF with short recovery time (i.e. it shouldn't take more than, say 1 frame to resynchronize).
The existing techniques I am aware (and using some) of:
1) Header / checksum - SOF as predefined byte value. Checksum in the end of frame.
- Pros: Simple.
- Cons: Not reliable. Unknown recovery time.
2) Byte stuffing:
- Pros: Reliable, fast recovery, can be used with any hardware
- Cons: Not that suitable for fixed-size frame-based communication
3) 9th bit marking - prepend each byte with additional bit, while SOF marked with
1 and the data bytes are marked with
- Pros: Reliable, fast recovery
- Cons: Requires hardware support. Not directly supported by most of
PChardware and software.
4) 8th bit marking - kind of emulation of the above, while using the 8th bit instead of 9th, which is leaving only 7bits for each data word.
- Pros: Reliable, fast recovery, can be used with any hardware.
- Cons: Requires an encoding/decoding scheme from/to the conventional 8-bit representation to/from 7-bit representation. Somewhat wasteful.
5) Timeout based - assume the SOF as the first byte coming after some defined idle time.
- Pros: No data overhead, simple.
- Cons: Not that reliable. Won't work well with poor timing systems like, say, Windows PC. Potential throughput overhead.
Question: What are the other possible techniques/solutions exist to address the problem? Can you point to the cons in the above list which can be easily worked around, thus removing them? How do you (or would you) design your systems protocol?