Hi everyone,
We are running an edge controller on Debian 12 (Raspberry Pi CM4, kernel 6.6.x) using Python 3.11 (pyserial) over a USB-to-RS485 FTDI adapter (/dev/ttyUSB0 @ 9600 baud) to drive modular 12V solenoid lock controller boards for an on-prem smart locker system.
Expected Behavior
When an employee scans an RFID badge or QR code at the kiosk, the Python daemon sends a 10-byte RS485 command frame to the target slave board, waits ~80ms for the door-sensor status ACK (0xAA 0x55 ...), and updates the local SQLite compartment state.
Actual Behavior
During shift changes—when multiple users trigger unlocks back-to-back or when our background door-state polling loop overlaps an unlock request—serial.read() returns partial 4–6 byte frames or times out, causing the daemon to log a false "door failed to open" state even though the solenoid physically clicked open.
Relevant snippet from our serial worker:
python
with serial_lock:
ser.reset_input_buffer()
ser.write(build_unlock_frame(board_id, slot_id))
ser.flush()
time.sleep(0.05)
resp = ser.read(10)
Error logged via journalctl -u locker-daemon:
text
Nov 25 08:51:14 edge-node-02 locker-daemon[812]: WARNING: board=0x03 slot=14 short read (got 5/10 bytes): b'\xaa\x55\x03\x0e\x01'
Nov 25 08:51:14 edge-node-02 locker-daemon[812]: ERROR: CRC mismatch on compartment status poll, retrying frame
What We've Tried
- Added a threading.Lock() around all RS485 bus reads/writes so polling and unlock commands don't collide in Python.
- Lowered /sys/bus/usb-serial/devices/ttyUSB0/latency_timer from 16 to 1.
- Increased timeout=0.3 on pyserial.Serial, which reduces timeouts slightly but still drops tail bytes when 3+ boards reply sequentially.
Is calling ser.reset_input_buffer() right before write() discarding delayed turnaround bytes from the RS485 transceiver, or should we switch from fixed-length ser.read(10) to a frame-header/CRC state machine reading byte-by-byte until the footer?
Top comments (0)