Attempts to auto-detect the serial port for the ESP32 device. Returns a list of all detected ports. If no suitable port is found, the list will be empty. Darwin/Linux logic by jonasdiemer
()
| 389 | |
| 390 | |
| 391 | def get_auto_detected_port() -> list[str]: |
| 392 | """ |
| 393 | Attempts to auto-detect the serial port for the ESP32 device. |
| 394 | Returns a list of all detected ports. |
| 395 | If no suitable port is found, the list will be empty. |
| 396 | Darwin/Linux logic by jonasdiemer |
| 397 | """ |
| 398 | port_list = [] |
| 399 | system = platform.system() |
| 400 | # Code for darwin (macOS), linux, and windows |
| 401 | if system in ("Darwin", "Linux"): |
| 402 | pattern = "/dev/tty.usbmodem*" if system == "Darwin" else "/dev/ttyACM*" |
| 403 | port_list = sorted(glob.glob(pattern)) |
| 404 | elif system == "Windows": |
| 405 | from serial.tools import list_ports |
| 406 | |
| 407 | # Be careful with this pattern list - it should be specific |
| 408 | # enough to avoid picking up unrelated devices, but broad enough |
| 409 | # to catch all common USB-serial adapters used with ESP32 |
| 410 | # Caveat: localized versions of Windows may have different descriptions, |
| 411 | # so we also check for specific VID:PID (but that may not cover all clones) |
| 412 | pattern_list = ["CP210x", "CH340", "USB Serial"] |
| 413 | found_ports = list_ports.comports() |
| 414 | port_list = [ |
| 415 | port.device |
| 416 | for port in found_ports |
| 417 | if any(pat in port.description for pat in pattern_list) |
| 418 | or port.hwid.startswith( |
| 419 | "USB VID:PID=303A:1001" |
| 420 | ) # Add specific VID:PID for XTEINK X4 |
| 421 | ] |
| 422 | |
| 423 | return port_list |
| 424 | |
| 425 | |
| 426 | def main() -> None: |