Files
ESP32-S3-Touch-LCD-5B/tools/diag_session.py
T

71 lines
2.0 KiB
Python

"""Run the Route A OBD-II diag probe sequence on Main CTRL and log everything.
Opens the port once (one reboot), waits for boot, then sends each diag command
with a generous wait, echoing replies and saving the full console stream
(CANRAW + CANTX + diag output) to wiki/raw/diag-<timestamp>.log.
Usage: python tools/diag_session.py COM10 [cmd [cmd ...]]
Default sequence: pids, pid 0C, pid 05, pid 0B, pid 70, vin, ecu, status.
"""
import os
import sys
import time
import serial
port = sys.argv[1] if len(sys.argv) > 1 else "COM10"
cmds = ["diag " + c for c in sys.argv[2:]] if len(sys.argv) > 2 else [
"diag pids",
"diag pid 0C",
"diag pid 05",
"diag pid 0B",
"diag pid 70",
"diag vin",
"diag ecu",
"diag status",
]
# pids walks up to 8 blocks with 500 ms timeouts each; give it room.
WAITS = {"diag pids": 15.0, "diag vin": 6.0, "diag ecu": 6.0}
DEFAULT_WAIT = 4.0
raw_dir = os.path.join(os.path.dirname(__file__), "..", "wiki", "raw")
log_path = os.path.join(raw_dir, time.strftime("diag-%Y%m%d-%H%M%S.log"))
log = open(log_path, "wb")
s = serial.Serial(port, 115200, timeout=0.1)
print("port open, waiting for boot...")
time.sleep(4)
def pump(seconds, echo_all=False):
"""Read for `seconds`, log everything, echo non-CANRAW lines."""
end = time.time() + seconds
pending = b""
while time.time() < end:
chunk = s.read(4096)
if not chunk:
continue
log.write(chunk)
pending += chunk
while b"\n" in pending:
line, pending = pending.split(b"\n", 1)
text = line.decode("utf-8", "replace").rstrip()
if echo_all or not text.startswith("CANRAW,"):
print(text)
log.flush()
try:
pump(2) # drain boot output
for cmd in cmds:
print("\n>>> " + cmd)
log.write(("\n>>> %s\n" % cmd).encode())
s.write((cmd + "\n").encode())
s.flush()
pump(WAITS.get(cmd, DEFAULT_WAIT))
finally:
log.close()
s.close()
print("\nlog saved: " + os.path.abspath(log_path))