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

25
relay
View file

@ -1,8 +1,8 @@
#!/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
USB serial port is probed with the ID command until one answers.
The relay board is found automatically: every USB serial port is probed
with the ID command until one answers.
"""
import sys
import serial
@ -39,20 +39,17 @@ def find_relay_port() -> str | None:
return None
def main() -> int:
if len(sys.argv) not in (2, 3) or sys.argv[1] not in ("0", "1", "id"):
print(f"Usage: {sys.argv[0]} 0|1|id [serial_port]", file=sys.stderr)
if len(sys.argv) != 2 or sys.argv[1] not in ("0", "1", "id"):
print(f"Usage: {sys.argv[0]} 0|1|id", file=sys.stderr)
return 1
command = sys.argv[1]
serial_cmd = "?" if command == "id" else command
if len(sys.argv) == 3:
port = sys.argv[2]
else:
port = find_relay_port()
if port is None:
print("ERROR: no relay module found on any USB serial port",
file=sys.stderr)
return 1
print(f"Using {port}", file=sys.stderr)
port = find_relay_port()
if port is None:
print("ERROR: no relay module found — is the Wemos D1 connected?",
file=sys.stderr)
return 1
print(f"Using {port}", file=sys.stderr)
try:
# dtr/rts expliciet uit vóór open: voorkomt (of beperkt) de CH340
# auto-reset (DTR->RST) bij het openen van de poort. pyserial 3.5