43 lines
1.1 KiB
Python
Executable File
43 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import serial, time, string
|
|
from datetime import datetime
|
|
|
|
PORT="/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0"
|
|
BAUD=9600
|
|
|
|
PRINTABLE=set(bytes(string.printable, "ascii"))
|
|
|
|
def classify(line: bytes):
|
|
# strip CR/LF
|
|
raw=line.strip(b"\r\n")
|
|
if not raw:
|
|
return None, None
|
|
printable = sum(b in PRINTABLE for b in raw)
|
|
ratio = printable / max(1, len(raw))
|
|
try:
|
|
txt = raw.decode("ascii", errors="replace")
|
|
except Exception:
|
|
txt = repr(raw)
|
|
return ratio, txt
|
|
|
|
ser=serial.Serial(PORT, BAUD, timeout=0.2)
|
|
buf=b""
|
|
cnt=0
|
|
try:
|
|
while True:
|
|
buf += ser.read(4096)
|
|
while b"\n" in buf:
|
|
line, buf = buf.split(b"\n", 1)
|
|
ratio, txt = classify(line+b"\n")
|
|
if ratio is None:
|
|
continue
|
|
cnt += 1
|
|
ts=datetime.now().strftime("%H:%M:%S")
|
|
tag="OK" if txt.startswith("Test #") else "GARBAGE"
|
|
print(f"[{ts}] #{cnt:04d} {tag} printable_ratio={ratio:.2f} {txt}")
|
|
time.sleep(0.01)
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
ser.close()
|