Simulator: Add support for /dev/ttyUSB and COM

This commit is contained in:
lorenz.stechauner committed 2026-11-23 19:10:13 +01:00
1 parent 51d3a16a37
commit 2715aa029f
1 file changed
+23 -9
+23 -9
View File
@@ -1,6 +1,5 @@
#!/bin/env python3 #!/bin/env python3
import socket
import time import time
import random import random
import argparse import argparse
@@ -43,9 +42,13 @@ def send_cmd(cmd: str, data: str) -> None:
msg = f'(A01{cmd}{data}&' msg = f'(A01{cmd}{data}&'
chk = sum(c for c in msg.encode('ascii')) & 0xFF chk = sum(c for c in msg.encode('ascii')) & 0xFF
msg += f'{chk:02X})\r' msg += f'{chk:02X})\r'
time.sleep(len(msg) * 8 / 9600 * 2) if not is_real:
time.sleep(len(msg) * 8 / 9600 * 1.5)
print(f'-> {msg}') print(f'-> {msg}')
client.send(msg.encode('ascii')) if is_real:
client.write(msg.encode('ascii'))
else:
client.send(msg.encode('ascii'))
if __name__ == '__main__': if __name__ == '__main__':
@@ -53,19 +56,30 @@ if __name__ == '__main__':
parser.add_argument('socket') parser.add_argument('socket')
args = parser.parse_args() args = parser.parse_args()
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) is_real = args.socket.startswith('/dev/ttyUSB') or args.socket.startswith('COM')
client.connect(args.socket)
if is_real:
import serial
client = serial.Serial(args.socket, 9600, parity=serial.PARITY_ODD)
else:
import socket
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(args.socket)
while True: while True:
line = b'' if is_real:
while b'\r' not in line: line = client.read_until(b'\r')
line += client.recv(256) else:
line = b''
while b'\r' not in line:
line += client.recv(256)
cmds = line.split(b'\r') cmds = line.split(b'\r')
line = cmds[-1] line = cmds[-1]
cmds = [c.decode('ascii') for c in cmds[:-1]] cmds = [c.decode('ascii') for c in cmds[:-1]]
for cmd in cmds: for cmd in cmds:
print(f'<- {cmd}') print(f'<- {cmd}')
time.sleep(len(cmd) * 8 / 9600 * 2) if not is_real:
time.sleep(len(cmd) * 8 / 9600 * 1.5)
if not cmd.startswith('(A01') or not cmd.endswith(')') or len(cmd) < 7: if not cmd.startswith('(A01') or not cmd.endswith(')') or len(cmd) < 7:
send_cmd('CE', '02') send_cmd('CE', '02')
continue continue