"""Probe the USB-CAN adapter to confirm it speaks a Lawicel-style ASCII protocol. The adapter is a CANdapter (Ewert Energy Systems / candapter.com), which enumerates as an FTDI FT240X USB-serial chip and shares its bring-up commands (V/N/S/O/C) with SLCAN, though its frame format diverges - see candapter.py. This script only exercises the shared commands (no CAN bus needs to be connected yet) to read the firmware version and serial number. """ import sys import time import serial PORT = "/dev/cu.usbserial-DNBJV4F5" BAUD = 115200 # standard SLCAN control baud rate (independent of CAN bitrate) def query(ser: serial.Serial, cmd: str, wait: float = 0.3) -> str: ser.reset_input_buffer() ser.write((cmd + "\r").encode("ascii")) time.sleep(wait) return ser.read(ser.in_waiting or 1).decode("ascii", errors="replace") def main() -> int: try: ser = serial.Serial(PORT, BAUD, timeout=1) except serial.SerialException as exc: print(f"Could not open {PORT}: {exc}", file=sys.stderr) return 1 with ser: # Close channel first in case it was left open, ignore any response. query(ser, "C") version = query(ser, "V") serial_no = query(ser, "N") print(f"Port: {PORT}") print(f"Raw version reply: {version!r}") print(f"Raw serial# reply: {serial_no!r}") if version.startswith("V") or serial_no.startswith("N"): print("\nAdapter responded to SLCAN commands - looks like a Lawicel/CANUSB-compatible device.") else: print("\nNo recognizable SLCAN response. The adapter may use a different protocol.") return 0 if __name__ == "__main__": raise SystemExit(main())