Add module identification command (relay id)
This commit is contained in:
parent
73dc2f188f
commit
cbb0d672f6
3 changed files with 10 additions and 5 deletions
11
relay
11
relay
|
|
@ -1,15 +1,16 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Toggle the USB relay via serial: ./relay 1|0 [serial_port]."""
|
||||
"""Toggle the USB relay via serial: ./relay 1|0|id [serial_port]."""
|
||||
import sys
|
||||
import serial
|
||||
|
||||
DEFAULT_PORT = "/dev/cu.usbserial-2110"
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) not in (2, 3) or sys.argv[1] not in ("0", "1"):
|
||||
print(f"Usage: {sys.argv[0]} 0|1 [serial_port]", file=sys.stderr)
|
||||
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)
|
||||
return 1
|
||||
command = sys.argv[1]
|
||||
serial_cmd = "?" if command == "id" else command
|
||||
port = sys.argv[2] if len(sys.argv) == 3 else DEFAULT_PORT
|
||||
try:
|
||||
# dtr/rts expliciet uit vóór open: voorkomt (of beperkt) de CH340
|
||||
|
|
@ -32,12 +33,12 @@ def main() -> int:
|
|||
# binnen 2 s (timeout, readline geeft b"") = geen reset: door.
|
||||
s.readline()
|
||||
s.reset_input_buffer()
|
||||
s.write(command.encode())
|
||||
s.write(serial_cmd.encode())
|
||||
s.flush()
|
||||
reply = s.readline().decode(errors="replace").strip()
|
||||
finally:
|
||||
s.close()
|
||||
if reply in ("ON", "OFF"):
|
||||
if reply in ("ON", "OFF") or (command == "id" and reply.startswith("ID ")):
|
||||
print(reply)
|
||||
return 0
|
||||
print(f"ERROR: no valid reply from board (got {reply!r})", file=sys.stderr)
|
||||
|
|
|
|||
Loading…
Reference in a new issue