| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/usr/bin/env python3
- """监控 COM9(板子唯一的 USB 转 232 口)的全部输出,持续 30 秒。"""
- import serial
- import serial.tools.list_ports
- import time
- import sys
- PORT = 'COM9'
- BAUD = 9600
- print(f"Opening {PORT} @ {BAUD}...", flush=True)
- try:
- s = serial.Serial(PORT, BAUD, timeout=0.1)
- except Exception as e:
- print(f"FAIL: {e}")
- sys.exit(1)
- print(f"OK. Watching for 30s...", flush=True)
- start = time.time()
- rx_total = 0
- line_buf = b''
- while time.time() - start < 30:
- data = s.read(1024)
- if data:
- rx_total += len(data)
- for b in data:
- if b == 0x0D:
- continue
- if b == 0x0A:
- print(f" RX[{rx_total:5d}]: {line_buf.decode('ascii', errors='replace')}", flush=True)
- line_buf = b''
- else:
- line_buf += bytes([b])
- else:
- # 每 5 秒打印一次 "alive"
- elapsed = int(time.time() - start)
- if elapsed % 5 == 0 and not hasattr(s, '_last_print'):
- pass
- # flush remaining
- if line_buf:
- print(f" RX[{rx_total:5d}]: {line_buf.decode('ascii', errors='replace')}", flush=True)
- s.close()
- print(f"\nDone. Total received: {rx_total} bytes in 30s")
|