C1233 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 33 · Commands over serial

Parsing text commands, and framing binary messages

A UART moves bytes; a protocol gives them meaning. The simplest is a line of text: a person (or a script) types pwm 30, the board splits it into words, looks up the command, acts, and answers ok. Text is easy to debug with any terminal, so most development boards and many products have such a command line. Machines talking to machines often prefer binary frames instead: a start byte, a length, the data and a checksum, compact and quick to check, at the cost of being unreadable without a tool.

  • The board runs a small command line. The terminal types four commands; the board echoes each one and answers on the next line.
  • Read handle: split the line into words, take the first as the command and the rest as arguments, check the count, convert numbers, and report errors instead of crashing.
  • The terminal also sends blink, which isn't a command yet: the board answers with an error. Add a blink command that toggles the LED, and run it again.

Predict

A binary frame carries 2 start bytes, 1 length byte, a payload and 1 checksum byte. For a 4-byte payload, what fraction of the bytes is payload?

Name it

Text commands

A line-based protocol reads characters until a newline, then:

  1. splits the line into words;
  2. looks up the first word in a table of commands;
  3. checks the number of arguments and converts them, rejecting anything malformed;
  4. acts, and always answers, ok, a value or an error message, so the sender knows the command arrived.

Answering every line is what makes the protocol usable from a script: the script waits for the answer before sending the next command.

Binary frames

FieldPurpose
start byte(s)find the beginning of a frame in a stream, e.g. 0xAA 0x55
lengthhow many payload bytes follow
payloadthe data
checksumdetect corrupted frames

A receiver discards bytes until it sees the start, reads the length, collects the payload, then checks the checksum; on a mismatch it throws the frame away and searches for the next start. That receiver is a state machine (lesson 36).

Checksums

  • Sum: add the bytes modulo 256. Often the sender appends (−sum)mod256, so that all bytes including the checksum add to 0.
  • XOR: combine the bytes with exclusive-or.
  • CRC (C7): catches far more error patterns, at the cost of more computation or a table.

A sum misses two bytes swapped; XOR misses a bit flipped in the same position in two bytes; a CRC catches both.

Worked example

A payload is 0x10, 0x20, 0xF0. What's its sum checksum, and what byte makes the total zero?

Show the solution
  1. Add the bytes. 0x10+0x20+0xF0=0x120
  2. Keep the low byte. Modulo 256 that's 0x20, the checksum.
  3. Find the byte that makes zero. (256−0x20)mod256=0xE0

Answer the checksum is 0x20, and 0xE0 makes the total zero.

Worked example

A command line answers each command with ok\n at 115,200 baud. How long does the answer take on the wire?

Show the solution
  1. Count the bits. 3 characters of 10 bits each make 30 bits.
  2. Time on the wire. 30115200s=0.26ms
  3. With an echo. Echoing the command too roughly triples that, still fast enough for a person, but worth counting when a script sends thousands of commands.

Answer 0.26 ms.

Worked example

Why can't a simple sum checksum detect two payload bytes swapped in order?

Show the solution
  1. Addition ignores order. 0x10 + 0x20 and 0x20 + 0x10 give the same sum, so a swap leaves the checksum unchanged.
  2. What catches a swap. A CRC, or a Fletcher checksum that also sums the running sums, weights each byte by its position, so a swap changes the result.

Answer a sum comes out the same in any order, so swapped bytes go unnoticed; a CRC or a Fletcher checksum weights each byte by its position and catches the swap.

Check

What's the 8-bit sum checksum (modulo 256) of the bytes 200, 100 and 180, in hex?

Check

A frame's bytes sum to 7F (mod 256). What checksum byte makes the total 0?

Check

A frame has 3 header bytes, an 8-byte payload and a 1-byte checksum. What fraction of it is payload?

Check

A reply of 20 characters goes out at 9600 baud. How long does it take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A command's argument arrives as the characters '4', '0', '8' (ASCII codes 48 + digit). Accumulating n = n*10 + (c - '0') for each, what's n?

Check

A receiver expects frames starting 0xAA 0x55, but it was switched on halfway through a frame. What should it do?

Check

Answer mul a b with the product of the two whole numbers, and with error if they aren't numbers. Each answer is one line.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

A payload is the bytes A, B and C (170, 32, 99). Print its 8-bit sum checksum, then the byte that would make the frame's total zero (0 if the sum is already 0).

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

A payload is 16, 32, 7.

(a)

What is its 8-bit sum?

(b)

What checksum byte makes the total 0 modulo 256?

Check

A command line answers with 12 characters at 9600 baud.

(a)

How long is the answer on the wire?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

At most how many such answers a second?

Explain why

Why should a text command protocol answer every command, even with just ok?

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.