Reader Thread
The Reader thread is the primary data acquisition engine. It continuously pulls RS-422 serial packets from the MCU, validates their integrity via CRC-32, and distributes samples to downstream consumers.
Architecture
Section titled “Architecture”The Reader acts as a Single Producer, feeding four Concurrent Consumers via thread-safe queues.
- Input: RS-422 Serial Stream (250,000 bps)
- Outputs: -
msed_writer_queue(Archival)websocket_queue(Live Dashboard)trigger_queue(Earthquake Detection)notifier_queue(Alerts)
Initialization
Section titled “Initialization”The thread is initialized with the global settings and a reference to the shared shutdown_event.
| Parameter | Type | Description |
|---|---|---|
settings | Settings | Pydantic model containing serial port/baudrate. |
queues | list[Queue] | The four downstream distribution queues. |
shutdown_event | Event | Global signal to stop processing. |
| Variable | Type | Purpose |
|---|---|---|
port | str | e.g., /dev/ttyUSB0 |
last_heartbeat | float | Tracks the 500ms heartbeat timer. |
buffer | bytearray | Sliding window for packet alignment. |
The Handshake Protocol
Section titled “The Handshake Protocol”Before streaming begins, the Reader must synchronize settings with the MCU. If this fails, the daemon aborts to prevent data with incorrect sample rates or gains from being recorded.
- Serial Connection: Opens the port. Note that opening the serial port often triggers a hardware reset on the MCU.
- Boot Delay: Waits 2 seconds for the MCU to initialize its internal registers.
- Transmit Settings: Sends a 6-byte
MCUSettingsFramecontaining the requestedsampling_rate,gain, andadc_sample_rate. - Echo Verification: Waits up to 10 seconds for the MCU to echo the exact same 6 bytes back.
- Validation: If the echo matches, streaming starts; otherwise, the daemon raises
MCUNoResponseand exits.
Main Loop Logic
Section titled “Main Loop Logic”The thread runs a high-frequency loop that performs three primary tasks:
1. Heartbeat Generation
Section titled “1. Heartbeat Generation”To prevent the MCU from streaming data into a “dead” serial port, the MCU pauses if it doesn’t see a heartbeat.
- Frequency: Every 500ms.
- Payload: A single byte
0x01.
2. Packet Alignment
Section titled “2. Packet Alignment”If the serial stream is interrupted, the Reader may find itself in the middle of a packet. It uses a sliding window to find the next valid header.
# Sliding window logicif buffer[0] == 0xAA and buffer[1] == 0xBB: # Potential packet foundelse: del buffer[0] # Shift 1 byte and try again