# _detect_port.ps1 -- Detect the best serial port via arduino-cli # # Called by upload.bat and monitor.bat. Outputs a single port name # (e.g. COM3) or nothing if no port is found. # Prefers USB serial ports over legacy motherboard COM ports. $ErrorActionPreference = 'SilentlyContinue' $raw = arduino-cli board list --format json 2>$null if (-not $raw) { exit } $data = $raw | ConvertFrom-Json if (-not $data.detected_ports) { exit } $serial = $data.detected_ports | Where-Object { $_.port.protocol -eq 'serial' } if (-not $serial) { exit } # Prefer USB serial ports (skip legacy COM1-style ports) $usb = $serial | Where-Object { $_.port.protocol_label -like '*USB*' } | Select-Object -First 1 if ($usb) { Write-Output $usb.port.address exit } # Fall back to any serial port $any = $serial | Select-Object -First 1 if ($any) { Write-Output $any.port.address }