|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Test script to probe DIN inputs via USB-serial connection. |
4 | | -Run this while monitoring DIN output from EGIAmpServerCLI. |
| 3 | +DIN test script: toggle USB-serial output lines while monitoring the DIN LSL stream. |
| 4 | +
|
| 5 | +This uses DTR, RTS, and TX-break as 3 independent binary outputs on a standard |
| 6 | +USB-serial adapter. Depending on how your cable maps DB-9 pins to the amplifier's |
| 7 | +DIN connector, some or all of these may register as DIN bit changes. |
| 8 | +
|
| 9 | +DB-9 output pins: |
| 10 | + Pin 4: DTR (pyserial: ser.dtr) |
| 11 | + Pin 7: RTS (pyserial: ser.rts) |
| 12 | + Pin 3: TX (pyserial: ser.break_condition -- held low when True) |
| 13 | +
|
| 14 | +Note: RS-232 voltage levels may not be compatible with the amp's TTL-level DIN |
| 15 | +inputs. If no bits change, voltage level mismatch is likely. |
| 16 | +
|
| 17 | +Requirements: |
| 18 | + pip install pyserial pylsl |
5 | 19 | """ |
6 | 20 |
|
7 | | -import serial |
| 21 | +import argparse |
| 22 | +import threading |
8 | 23 | import time |
9 | | -import sys |
10 | 24 |
|
11 | | -PORT = "/dev/cu.usbserial-210" |
12 | | -BAUD = 9600 |
| 25 | +import pylsl |
| 26 | +import serial |
| 27 | + |
| 28 | + |
| 29 | +def monitor_din(stop_event: threading.Event, stream_name: str = ""): |
| 30 | + """Background thread: resolve DIN marker stream and print changes.""" |
| 31 | + inlet = None |
| 32 | + |
| 33 | + while not stop_event.is_set(): |
| 34 | + if inlet is None: |
| 35 | + print("[DIN] Resolving DIN stream (type='Markers', name='*_DIN')...") |
| 36 | + streams = pylsl.resolve_byprop("type", "Markers", timeout=5) |
| 37 | + din_streams = [s for s in streams if s.name().endswith("_DIN")] |
| 38 | + if stream_name: |
| 39 | + din_streams = [s for s in din_streams if stream_name in s.name()] |
| 40 | + if not din_streams: |
| 41 | + names = [s.name() for s in streams] |
| 42 | + print(f"[DIN] Not found yet (available Markers: {names}). " |
| 43 | + "Is the amp linked/streaming? Retrying...") |
| 44 | + continue |
| 45 | + |
| 46 | + info = din_streams[0] |
| 47 | + print(f"[DIN] Connected to '{info.name()}'") |
| 48 | + print("[DIN] This stream only sends samples on DIN value *changes*.") |
| 49 | + print("[DIN] Waiting for events...\n") |
| 50 | + inlet = pylsl.StreamInlet(info, max_buflen=1) |
| 51 | + |
| 52 | + sample, ts = inlet.pull_sample(timeout=0.5) |
| 53 | + if sample is None: |
| 54 | + continue |
| 55 | + din = int(sample[0]) |
| 56 | + bits = f"{din:016b}" |
| 57 | + active = [i + 1 for i in range(16) if (din >> i) & 1] |
| 58 | + active_str = f" active: DIN {','.join(map(str, active))}" if active else "" |
| 59 | + print(f"[DIN] value={din:5d} bits={bits} (0x{din:04X}){active_str}") |
| 60 | + |
13 | 61 |
|
14 | 62 | def main(): |
15 | | - print(f"Opening {PORT} at {BAUD} baud...") |
16 | | - ser = serial.Serial(PORT, BAUD, timeout=1) |
17 | | - |
18 | | - print("\nSerial port opened. Current control line states:") |
19 | | - print(f" DTR: {ser.dtr}") |
20 | | - print(f" RTS: {ser.rts}") |
21 | | - print(f" CTS: {ser.cts}") |
22 | | - print(f" DSR: {ser.dsr}") |
23 | | - print(f" CD: {ser.cd}") |
24 | | - print(f" RI: {ser.ri}") |
25 | | - |
26 | | - print("\n--- Interactive Mode ---") |
| 63 | + parser = argparse.ArgumentParser(description="Test DIN inputs via USB-serial adapter") |
| 64 | + parser.add_argument("--port", default="/dev/cu.usbserial-210", |
| 65 | + help="Serial port (default: /dev/cu.usbserial-210)") |
| 66 | + parser.add_argument("--stream", default="", |
| 67 | + help="LSL stream name filter (default: match any DIN stream)") |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + stop_event = threading.Event() |
| 71 | + |
| 72 | + # Start DIN monitor in background |
| 73 | + din_thread = threading.Thread(target=monitor_din, args=(stop_event, args.stream), |
| 74 | + daemon=True) |
| 75 | + din_thread.start() |
| 76 | + |
| 77 | + # Open serial port |
| 78 | + print(f"[SER] Opening {args.port}...") |
| 79 | + try: |
| 80 | + ser = serial.Serial(args.port, 9600, timeout=1) |
| 81 | + except serial.SerialException as e: |
| 82 | + print(f"[SER] Failed to open port: {e}") |
| 83 | + stop_event.set() |
| 84 | + return |
| 85 | + |
| 86 | + # Start with all outputs in their 'idle' state |
| 87 | + ser.dtr = False |
| 88 | + ser.rts = False |
| 89 | + ser.break_condition = False |
| 90 | + time.sleep(0.5) |
| 91 | + |
| 92 | + print(f"\n[SER] Port opened. Output pins:") |
| 93 | + print(f" DTR (pin 4): {'HIGH' if ser.dtr else 'LOW'}") |
| 94 | + print(f" RTS (pin 7): {'HIGH' if ser.rts else 'LOW'}") |
| 95 | + print(f" TX (pin 3): IDLE (high)") |
| 96 | + print() |
27 | 97 | print("Commands:") |
28 | | - print(" d - Toggle DTR") |
29 | | - print(" r - Toggle RTS") |
30 | | - print(" b - Toggle BREAK") |
31 | | - print(" 0-9 - Send that digit as a byte") |
32 | | - print(" s - Send 0x00") |
33 | | - print(" f - Send 0xFF") |
34 | | - print(" a - Send all bytes 0x00-0xFF rapidly") |
35 | | - print(" t - Send test pattern (0x55, 0xAA)") |
36 | | - print(" p - Print control line states") |
37 | | - print(" q - Quit") |
| 98 | + print(" d - Toggle DTR") |
| 99 | + print(" r - Toggle RTS") |
| 100 | + print(" b - Toggle TX break (holds TX low)") |
| 101 | + print(" 1 - All outputs ON (DTR=1, RTS=1, break=1)") |
| 102 | + print(" 0 - All outputs OFF (DTR=0, RTS=0, break=0)") |
| 103 | + print(" scan - Toggle each output one at a time with pauses") |
| 104 | + print(" p - Print current output states") |
| 105 | + print(" q - Quit") |
| 106 | + print() |
| 107 | + print("Watch the [DIN] lines above for bit changes.") |
38 | 108 | print() |
39 | 109 |
|
40 | 110 | break_state = False |
41 | 111 |
|
42 | | - while True: |
| 112 | + while not stop_event.is_set(): |
43 | 113 | try: |
44 | 114 | cmd = input("> ").strip().lower() |
45 | | - except EOFError: |
| 115 | + except (EOFError, KeyboardInterrupt): |
46 | 116 | break |
47 | 117 |
|
48 | | - if cmd == 'q': |
| 118 | + if cmd == "q": |
49 | 119 | break |
50 | | - elif cmd == 'd': |
| 120 | + elif cmd == "d": |
51 | 121 | ser.dtr = not ser.dtr |
52 | | - print(f"DTR = {ser.dtr}") |
53 | | - elif cmd == 'r': |
| 122 | + print(f"[SER] DTR = {ser.dtr}") |
| 123 | + elif cmd == "r": |
54 | 124 | ser.rts = not ser.rts |
55 | | - print(f"RTS = {ser.rts}") |
56 | | - elif cmd == 'b': |
| 125 | + print(f"[SER] RTS = {ser.rts}") |
| 126 | + elif cmd == "b": |
57 | 127 | break_state = not break_state |
58 | 128 | ser.break_condition = break_state |
59 | | - print(f"BREAK = {break_state}") |
60 | | - elif cmd == 'p': |
61 | | - print(f" DTR: {ser.dtr}, RTS: {ser.rts}") |
62 | | - print(f" CTS: {ser.cts}, DSR: {ser.dsr}, CD: {ser.cd}, RI: {ser.ri}") |
63 | | - elif cmd == 's': |
64 | | - ser.write(b'\x00') |
65 | | - print("Sent 0x00") |
66 | | - elif cmd == 'f': |
67 | | - ser.write(b'\xff') |
68 | | - print("Sent 0xFF") |
69 | | - elif cmd == 'a': |
70 | | - print("Sending 0x00-0xFF...") |
71 | | - for i in range(256): |
72 | | - ser.write(bytes([i])) |
73 | | - time.sleep(0.01) |
74 | | - print("Done") |
75 | | - elif cmd == 't': |
76 | | - ser.write(b'\x55\xaa') |
77 | | - print("Sent 0x55, 0xAA") |
78 | | - elif cmd.isdigit(): |
79 | | - ser.write(cmd.encode()) |
80 | | - print(f"Sent '{cmd}' (0x{ord(cmd):02x})") |
| 129 | + print(f"[SER] TX break = {break_state}") |
| 130 | + elif cmd == "1": |
| 131 | + ser.dtr = True |
| 132 | + ser.rts = True |
| 133 | + break_state = True |
| 134 | + ser.break_condition = True |
| 135 | + print("[SER] All ON: DTR=1, RTS=1, break=1") |
| 136 | + elif cmd == "0": |
| 137 | + ser.dtr = False |
| 138 | + ser.rts = False |
| 139 | + break_state = False |
| 140 | + ser.break_condition = False |
| 141 | + print("[SER] All OFF: DTR=0, RTS=0, break=0") |
| 142 | + elif cmd == "scan": |
| 143 | + print("[SER] Scanning each output (2s per toggle)...") |
| 144 | + for name, on, off in [ |
| 145 | + ("DTR", lambda: setattr(ser, 'dtr', True), |
| 146 | + lambda: setattr(ser, 'dtr', False)), |
| 147 | + ("RTS", lambda: setattr(ser, 'rts', True), |
| 148 | + lambda: setattr(ser, 'rts', False)), |
| 149 | + ("TX break", lambda: setattr(ser, 'break_condition', True), |
| 150 | + lambda: setattr(ser, 'break_condition', False)), |
| 151 | + ]: |
| 152 | + print(f"[SER] {name} ON") |
| 153 | + on() |
| 154 | + time.sleep(2) |
| 155 | + print(f"[SER] {name} OFF") |
| 156 | + off() |
| 157 | + time.sleep(2) |
| 158 | + break_state = False |
| 159 | + print("[SER] Scan complete.") |
| 160 | + elif cmd == "p": |
| 161 | + print(f"[SER] DTR={ser.dtr}, RTS={ser.rts}, break={break_state}") |
81 | 162 | elif cmd: |
82 | | - # Send arbitrary string |
83 | | - ser.write(cmd.encode()) |
84 | | - print(f"Sent: {cmd!r}") |
| 163 | + print(f"Unknown command: {cmd}") |
85 | 164 |
|
| 165 | + stop_event.set() |
86 | 166 | ser.close() |
87 | | - print("Closed.") |
| 167 | + print("Done.") |
| 168 | + |
88 | 169 |
|
89 | 170 | if __name__ == "__main__": |
90 | 171 | main() |
0 commit comments