"""Print the micrOBU's STATUS (incl. the battery fields) from its native USB Serial/JTAG port. py -3.11 tools/station_status_watch.py COM10 [seconds] Speaks the station-link framing of obu-firmware (0xAA55, type, u16 length, payload, CRC-16/CCITT-FALSE over type..payload, little endian; frame type 0x10 = LINK) and asks for STATUS once a second. The board also broadcasts STATUS itself every second. Opening the port resets the board (DTR/RTS), so the first frames come after the boot. Needs pyserial (the ESP-IDF python env has it). """ import struct import sys import time import serial LINK = 0x10 STATUS_REQUEST = 0x0C STATUS = 0x84 def crc16(data: bytes) -> int: crc = 0xFFFF for b in data: crc ^= b << 8 for _ in range(8): crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF return crc def frame(type_: int, payload: bytes) -> bytes: head = bytes([type_]) + struct.pack(' 0: del buffer[:i] return length = buffer[i + 3] | (buffer[i + 4] << 8) end = i + 5 + length + 2 if len(buffer) < end: if i > 0: del buffer[:i] return head, crc = bytes(buffer[i + 2:end - 2]), buffer[end - 2] | (buffer[end - 1] << 8) del buffer[:end if crc16(head) == crc else i + 2] if crc16(head) == crc: yield head[0], head[3:] def describe(body: bytes) -> str: uptime, configured = struct.unpack_from('= 106: mv, flags = struct.unpack_from(' 1 else 'COM10' seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 15 buffer = bytearray() sequence = 0 with serial.Serial(port, 115200, timeout=0.2) as s: s.dtr = True # a CDC host asserts DTR; the firmware may gate TX on it end = time.time() + seconds next_request = 0.0 while time.time() < end: if time.time() >= next_request: sequence += 1 s.write(frame(LINK, bytes([STATUS_REQUEST, 0]) + struct.pack('= 4 and payload[0] == STATUS: print(time.strftime('%H:%M:%S'), describe(payload[4:]), flush=True) if __name__ == '__main__': main()