"""Capture N seconds from two serial ports in parallel and print what arrived.""" import sys import time import threading import serial PORTS = sys.argv[1].split(",") if len(sys.argv) > 1 else ["COM8", "COM9"] SECONDS = float(sys.argv[2]) if len(sys.argv) > 2 else 6.0 MAX_LINES = 40 def cap(port, out): try: s = serial.Serial(port, 115200, timeout=0.5) end = time.time() + SECONDS buf = b"" while time.time() < end: buf += s.read(4096) s.close() out[port] = buf except Exception as e: out[port] = ("ERR: %s" % e).encode() out = {} threads = [threading.Thread(target=cap, args=(p, out)) for p in PORTS] for t in threads: t.start() for t in threads: t.join() for p in PORTS: data = out.get(p, b"") print("=" * 20, p, "bytes:", len(data), "=" * 20) lines = data.decode("utf-8", "replace").splitlines() for ln in lines[:MAX_LINES]: print(ln) if len(lines) > MAX_LINES: print("... (%d more lines)" % (len(lines) - MAX_LINES))