File
Blob: scripts/serial_device.py
| 1 | """Select the native USB serial port; explicit ESP32_PORT takes precedence.""" |
| 2 | import os |
| 3 | from pathlib import Path |
| 4 | |
| 5 | def device_port(): |
| 6 | explicit = os.environ.get("ESP32_PORT") |
| 7 | if explicit: |
| 8 | return explicit |
| 9 | ports = list(Path("/dev/serial/by-id").glob("usb-Espressif_USB_JTAG_serial_debug_unit_*-if00")) |
| 10 | if len(ports) != 1: |
| 11 | raise RuntimeError("Expected one Espressif USB debug device; set ESP32_PORT explicitly otherwise") |
| 12 | return str(ports[0]) |
| 13 | |
| 14 | |
| 15 | def device_identity(port): |
| 16 | """Use USB serial identity rather than the reusable ttyACM device number.""" |
| 17 | from serial.tools.list_ports import comports |
| 18 | selected = Path(port).resolve() |
| 19 | for device in comports(): |
| 20 | if Path(device.device).resolve() == selected and device.serial_number: |
| 21 | return f"usb:{device.vid}:{device.pid}:{device.serial_number}" |
| 22 | return None |