Files

57 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""
Example client for FR-1.4 protocol
"""
import serial
import time
from protocol_parser import ProtocolParser
def main():
SERIAL_PORT = "/dev/ttyACM0"
BAUDRATE = 115200
parser = ProtocolParser()
try:
with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=1) as ser:
print(f"Connected to {SERIAL_PORT}")
while True:
# Read available data
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting)
# Parse packets
packets = parser.feed(data)
for pkt in packets:
if pkt.valid:
print(
f"[{pkt.timestamp_ms:010d}] "
f"RMS: {pkt.rms_db:+6.1f} dB "
f"Freq: {pkt.freq_hz:4d} Hz"
)
else:
print(f"[WARN] Invalid packet: {pkt}")
# Show stats every 30 packets
stats = parser.get_stats()
if stats.packets_received % 30 == 0 and stats.packets_received > 0:
print(
f"Stats: RX={stats.packets_received} "
f"CRC_err={stats.crc_errors} "
f"LEN_err={stats.length_errors} "
f"Range_err={stats.range_errors}"
)
time.sleep(0.01)
except KeyboardInterrupt:
print("\nExiting...")
except serial.SerialException as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()