Test a radar UART parser without the radar
Most radar integration bugs are parser bugs, and a parser bug rarely looks like one. It looks like phantom presence, a target that freezes at one distance, or a device that "sometimes" reports nothing.
The module is not always at fault, and re-flashing the host is not a diagnostic. This chapter describes a test harness that exercises the parser with recorded bytes, so a firmware change can be checked before it reaches a room.
Split the stack before testing it
A radar integration that is hard to test usually has one function doing three jobs. Separate them:
- A byte source: the UART, a file, or a socket.
- A frame decoder: finds a start marker, reads length, validates a checksum, emits a frame object.
- A domain mapper: turns a frame into presence, gate energy or coordinates.
Only the first layer knows what a UART is, and only the third knows what presence means. The middle layer is where the bugs live, and it is also the easiest layer to test because it is pure bytes in, structures out.
A minimal decoder interface is enough:
from dataclasses import dataclass
from typing import Iterable, Optional
@dataclass
class Frame:
kind: str
payload: bytes
class Decoder:
"""Vendor frame rules live here; this skeleton only shows the contract."""
def __init__(self):
self.buffer = bytearray()
self.rejected = 0
self.resyncs = 0
def feed(self, chunk: bytes) -> list[Frame]:
self.buffer.extend(chunk)
frames = []
while True:
frame = self._next_frame()
if frame is None:
return frames
frames.append(frame)
def _next_frame(self) -> Optional[Frame]:
raise NotImplementedError("fill in the header, length and checksum rules")
Record one trace and keep it
A ten-second capture at the configured baud rate is usually a few kilobytes, and it is enough to catch a regression.
- Enable engineering mode during the capture so per-gate energy appears in the trace.
- Record three segments: empty room, person walking, person sitting still.
- Store the raw bytes with the firmware version, baud rate and mounting geometry in the file name or a sidecar JSON.
Do not record a trace without the conditions. A byte stream with no context cannot tell you whether a change made the parser better or the room quieter.
Replay it with realistic timing
A decoder that only ever sees a whole frame at once will pass tests and fail in production, because a UART delivers bytes in arbitrary chunks.
def replay(decoder, blob: bytes, chunk_sizes: Iterable[int]):
pos = 0
frames = []
for size in chunk_sizes:
frames.extend(decoder.feed(blob[pos:pos + size]))
pos += size
if pos >= len(blob):
break
return frames
# 1 byte at a time, then random sizes, then the whole buffer
sizes = [1] * len(blob)
assert len(replay(Decoder(), blob, sizes)) == len(replay(Decoder(), blob, [len(blob)]))
Feeding the trace in one-byte chunks is the cheapest test that catches an off-by-one length bug. Feeding it in random chunk sizes catches the assumption that a frame arrives whole.
Inject the five failures that happen in the field
A parser earns its keep on damaged input. Five cases cover most of it:
- A frame cut in half, then followed by a valid frame.
- The same frame sent twice, as a retry or a duplicated read.
- A garbage prefix before the header, which is what a host sees when it attaches mid-stream.
- A bad checksum, which must be rejected rather than corrected.
- A byte-level corruption, such as a wrong baud rate, which produces plausible-looking lengths.
Each case has an expected outcome, and writing it down is the test:
| Injected fault | Expected behaviour |
|---|---|
| Half frame then valid frame | One frame emitted, one rejection counted |
| Duplicate frame | Two frames emitted, no state corruption |
| Garbage prefix | Resync at the next header, rejected bytes counted |
| Bad checksum | Frame dropped, rejection counted, next frame accepted |
| Corrupted length field | Timeout, buffer reset, no allocation blow-up |
The last row is the one that finds real bugs. A decoder that trusts a length field can be told to buffer megabytes by one corrupt byte, and the failure appears as memory pressure on a device with no console.
Measure three numbers, not one
A parser report should include:
- Frames per second, compared with the module's published rate.
- Rejected frames as a fraction of total, which should be zero on a clean trace.
- Resynchronisations per minute, which should be zero on a clean trace and small on a noisy one.
A parser that emits the right values at half the expected rate is dropping frames, and the automation will blame the radar.
Put the trace in version control, the capture tool in the repo
Two artefacts, two homes:
- The trace files are small and stable, so they belong beside the tests.
- The capture script belongs in the same repository, because the next person needs to record the same three segments.
Version the trace with the firmware string, not just a date. A trace recorded on an older firmware answers a different question.
What this harness does not prove
It does not prove the antenna works, the supply is adequate, or the room behaves. It proves the decoder turns a known byte stream into known structures, which is the one layer you can test without hardware.
A passing harness plus a failing installation points at the electrical or physical layer. That is useful: it turns a vague "the sensor is flaky" into a specific next check, and the power and UART chapter is where that check starts.