# Reads the Cardputer's serial port, reconnecting when it re-enumerates. Run via serial_log.sh. import glob, os, sys, time import serial end = time.time() + float(sys.argv[1]) commands = sys.argv[2:] # sent once after connecting, e.g. "burst" while time.time() < end: ports = glob.glob('/dev/serial/by-id/*Espressif*') if not ports: time.sleep(1) continue try: s = serial.Serial(os.path.realpath(ports[0]), 115200, timeout=0.2) print('[connected]', flush=True) for c in commands: # "sleep:N" waits N seconds between commands if c.startswith('sleep:'): time.sleep(float(c[6:])) continue time.sleep(1) s.write((c + '\n').encode()) commands = [] buf = b'' while time.time() < end: buf += s.read(512) while b'\n' in buf: line, buf = buf.split(b'\n', 1) print(line.decode(errors='replace').rstrip(), flush=True) except Exception as e: print('[disconnected]', e, flush=True) time.sleep(1)