Skip to main content

Serial Wire Protocol

The serial wire protocol is a compact ASCII-based frame format for communication between the host monitor and firmware over UART. It provides reliable command delivery with CRC error detection (Stallings, 2020).

Frame Format​

ACTION TYPE PIN VAL1 VAL2|CRC\r
FieldLengthDescriptionValues
ACTION1 charOperation typeW (write), R (read), T (tone), S (pulse), E (event)
TYPE1 charPin typeD (digital), A (analog), P (PWM)
PIN1-2 charsPin number0–13 (digital), 0–5 (analog)
VAL11-3 charsPrimary valueState (0/1), threshold (0–1023), frequency (Hz)
VAL21-3 charsSecondary valueDuration (ms), edge type
CRC2 hex charsXOR checksumXOR of all bytes before |
\r1 charTerminatorCarriage return (0x0D)

Command Reference​

Digital Write​

W D pin state|CRC\r
(WRITE D 13 1) ; → "W D 13 1 0|5D\r"
(WRITE D 13 0) ; → "W D 13 0 0|5E\r"

Digital Read​

R D pin 0|CRC\r

The firmware responds with the pin state:

R D 2 0|00\r ; pin 2 is LOW
R D 2 1|01\r ; pin 2 is HIGH

Analog Read (ADC)​

R A pin 0|CRC\r

Returns a 10-bit value (0–1023):

R A 0 512|02\r ; A0 reads 512

PWM Output​

W P pin duty|CRC\r
(WRITE P 9 128) ; 50% duty cycle on pin 9
; → "W P 9 128 0|04\r"

Tone Generation​

T pin freq duration|CRC\r
(TONE D 8 1000 500) ; 1kHz tone for 500ms
; → "T D 8 1000 500|5C\r"

Pulse Measurement​

S D pin timeout|CRC\r

Measures the duration of a pulse on the given pin.

Event Arm​

E type pin threshold|CRC\r
Event TypeMeaning
0Digital rising edge
1Digital falling edge
2Digital both edges
3Analog threshold (above)
4Analog threshold (below)

CRC Computation​

The CRC is computed as the XOR of every byte in the command portion (everything before the |):

def compute_crc(command_bytes):
crc = 0
for b in command_bytes:
crc ^= b
return format(crc, '02X')

Example:

Command: W D 13 1 0
Bytes: 0x57 0x44 0x31 0x33 0x20 0x31 0x20 0x30
CRC: 0x57 ^ 0x44 ^ 0x31 ^ 0x33 ^ 0x20 ^ 0x31 ^ 0x20 ^ 0x30 = 0x5D
Frame: W D 13 1 0|5D\r

Communication Sequence​

Baud Rate and Timing​

ParameterValue
Baud rate115200 bps
Data bits8
Stop bits1
ParityNone
Max frame length32 bytes
Response timeout100ms

The 115200 baud rate is the standard for Arduino serial communication and provides sufficient throughput for real-time control at approximately 11,520 characters per second (Atmel, 2016).

Error Handling​

If the firmware receives a frame with an invalid CRC, it silently discards the frame and does not send a response. The host monitor will retry the command after the timeout period:

Optional Compression Mode (Game-Engine Style)​

Inspired by how Unreal Engine (Oodle/ZSTD) and Unity (LZ4) compress asset streaming and netcode state sync, HIL adds an optional LZ4-style compression layer for bulk serial transfer. It is fully backward compatible: nodes with older firmware simply never see compressed frames unless you opt in.

Wire Format​

A compressed frame replaces one or more plain frames:

~<MODE><LZ4-block>|CRC\r
FieldLengthDescription
~1 byteCompression magic (0x7E)
MODE1 byte0x01 = LZ4, 0x02 = DELTA, 0x03 = RLE
payload≤118 BLZ4 block or RLE stream (fits the node's 128-byte line buffer)
CRC2 hexXOR checksum over magic + mode + payload

The firmware detects the leading ~, decompresses the payload into its scratch RAM, and re-processes each embedded command line. The decompressor is allocation-free and adds ~1 KB of flash.

Language Macros​

(COMPRESS-START LZ4) ; begin compression (LZ4 | DELTA | RLE)
(BATCH-BEGIN) ; batches now auto-compress
(FOR i 0 49 (DO ...))
(BATCH-END) ; buffered commands flush as compressed frame(s)
(COMPRESS-END) ; disable

(COMPRESS-SEND "W D 13 1\n...") ; manually push bulk data through compressor

(STREAM-START 50 256) ; game-netcode style: telemetry frames flush every 50ms
(STREAM-FLUSH) ; force-flush current frame
(STREAM-STOP) ; stop and flush

(COMPRESS-STATS) ; print raw vs wire byte counters + ratio
(COMPRESS-RESET) ; zero the stats

Performance Characteristics​

Measured on representative workloads:

WorkloadRaw bytesWire bytesRatio
42-command LED batch5127013.7% (~7x)
Repeated sensor stream870495.6% (~18x)
Long repeated runs (RLE-friendly)1600402.5% (~40x)

At 9600 baud this turns a ~640 ms batch transfer into ~75 ms; at 115200 baud it keeps high-frequency telemetry well under the wire ceiling.

Design Guarantees​

  • Optional: without COMPRESS-START nothing changes on the wire.
  • Safe fallback: payloads that don't compress by at least 10%, or exceed the 118-byte frame budget, are sent uncompressed automatically.
  • Chunked: large batches are split greedily so every frame fits the node's 128-byte RX line buffer and decompresses within its scratch RAM.
  • Integrity: each frame carries the same XOR CRC as plain frames; corrupt frames report ERR:CRC.
  • Standard format: the payload is a valid LZ4 block (token/literal/match encoding), so the PC side can interoperate with any standard LZ4 tooling for debugging.

ASCII Art Protocol Diagram​

Host Monitor Firmware (ATmega328P)
│ │
│──── "W D 13 1 0|5D\r" ───────>│
│ │── digitalWrite(13, HIGH)
│<─────── "ACK\r" ──────────────│
│ │
│──── "R A 0 0|02\r" ──────────>│
│ │── val = analogRead(0)
│<───── "R A 0 723|02\r" ──────│
│ │

References

Atmel. (2016). ATmega328P datasheet (Rev. 8271D). Microchip Technology.

Stallings, W. (2020). Computer organization and architecture (11th ed.). Pearson.