Remove port override, fail on detection failure

This commit is contained in:
Eddy de Vink 2026-08-13 13:23:30 +02:00
commit 024462f422
3 changed files with 21 additions and 24 deletions

View file

@ -70,14 +70,14 @@ cd /Users/eddy/src/usbtoggle
./relay 1 # relais aan → USB afgekoppeld (module uit) ./relay 1 # relais aan → USB afgekoppeld (module uit)
./relay 0 # relais uit → USB aangekoppeld (module aan) ./relay 0 # relais uit → USB aangekoppeld (module aan)
./relay id # identificatie (chip-ID) ./relay id # identificatie (chip-ID)
./relay 1 /dev/cu.usbserial-XXXX # poort expliciet opgeven (alleen als auto-detect faalt)
``` ```
- Zonder poortargument zoekt het script de module **automatisch**: het - Het script zoekt de module **automatisch**: het probeert alle
probeert alle CH340-poorten (`VID:PID=1A86:7523`) met het `id`-commando CH340-poorten (`VID:PID=1A86:7523`) met het `id`-commando en gebruikt
en gebruikt de eerste die `ID usbtoggle-relay` antwoordt. De module de eerste die `ID usbtoggle-relay` antwoordt. De module wordt dus
wordt dus gevonden waar die ook is aangesloten; de poortnaam hoeft gevonden waar die ook is aangesloten; de poortnaam hoeft nooit
nooit hardcoded te worden. hardcoded te worden. Kan de module niet gevonden worden, dan faalt het
script met een foutmelding (exit 1).
- Replies: `ON` / `OFF` / `ID ...`; exit 0 = gelukt, exit 1 = fout (usage, - Replies: `ON` / `OFF` / `ID ...`; exit 0 = gelukt, exit 1 = fout (usage,
poort niet te openen, geen geldige reply). poort niet te openen, geen geldige reply).

View file

@ -12,12 +12,12 @@ Sluit aan via USB en stuur commando's:
./relay 1 # relais aan ./relay 1 # relais aan
./relay 0 # relais uit ./relay 0 # relais uit
./relay id # toont module-ID (chip-ID, uniek per chip, onafhankelijk van USB-poort) ./relay id # toont module-ID (chip-ID, uniek per chip, onafhankelijk van USB-poort)
./relay 1 /dev/cu.usbserial-XXXX # poort expliciet opgeven (alleen als auto-detect faalt)
``` ```
Zonder poortargument zoekt het script de module zelf: het probeert alle Het script zoekt de module altijd zelf: het probeert alle CH340-poorten
CH340-poorten met het `id`-commando en gebruikt de eerste die `ID met het `id`-commando en gebruikt de eerste die `ID usbtoggle-relay`
usbtoggle-relay` antwoordt — waar de module ook is aangesloten. antwoordt — waar de module ook is aangesloten. Kan de module niet
gevonden worden, dan faalt het script met een foutmelding (exit 1).
## Linux ## Linux

15
relay
View file

@ -1,8 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Toggle the USB relay via serial: ./relay 1|0|id [serial_port]. """Toggle the USB relay via serial: ./relay 1|0|id.
Without an explicit port the relay board is found automatically: every The relay board is found automatically: every USB serial port is probed
USB serial port is probed with the ID command until one answers. with the ID command until one answers.
""" """
import sys import sys
import serial import serial
@ -39,17 +39,14 @@ def find_relay_port() -> str | None:
return None return None
def main() -> int: def main() -> int:
if len(sys.argv) not in (2, 3) or sys.argv[1] not in ("0", "1", "id"): if len(sys.argv) != 2 or sys.argv[1] not in ("0", "1", "id"):
print(f"Usage: {sys.argv[0]} 0|1|id [serial_port]", file=sys.stderr) print(f"Usage: {sys.argv[0]} 0|1|id", file=sys.stderr)
return 1 return 1
command = sys.argv[1] command = sys.argv[1]
serial_cmd = "?" if command == "id" else command serial_cmd = "?" if command == "id" else command
if len(sys.argv) == 3:
port = sys.argv[2]
else:
port = find_relay_port() port = find_relay_port()
if port is None: if port is None:
print("ERROR: no relay module found on any USB serial port", print("ERROR: no relay module found — is the Wemos D1 connected?",
file=sys.stderr) file=sys.stderr)
return 1 return 1
print(f"Using {port}", file=sys.stderr) print(f"Using {port}", file=sys.stderr)