Update simulator.py
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SysTec IT3000A uses CRC16-modbus
|
||||
|
||||
import argparse
|
||||
import socket
|
||||
import datetime
|
||||
@ -24,17 +22,20 @@ def handle_systec_it3000a(req: bytes) -> bytes:
|
||||
global IDENT_NR
|
||||
|
||||
if not req.startswith(b'<') or not req.endswith(b'>'):
|
||||
return b'<01>\r\n'
|
||||
return b'<31>\r\n'
|
||||
req = req[1:-1]
|
||||
|
||||
if len(req) > 3:
|
||||
return b'<02>\r\n'
|
||||
if req in (b'OS01', b'OS02', b'OC01', b'OC02'):
|
||||
# set or clear outputs 1 or 2
|
||||
return b'<00>\r\n'
|
||||
elif len(req) > 3:
|
||||
return b'<32>\r\n'
|
||||
if req.startswith(b'RN'):
|
||||
incr = True
|
||||
elif req.startswith(b'RM'):
|
||||
incr = False
|
||||
else:
|
||||
return b'<02>\r\n'
|
||||
return b'<32>\r\n'
|
||||
|
||||
if incr:
|
||||
time.sleep(random.randint(0, 40) / 10.0)
|
||||
@ -55,6 +56,10 @@ def handle_systec_it3000a(req: bytes) -> bytes:
|
||||
return b'<' + data + f'{crc16_modbus(data):8}'.encode('ascii') + b'>\r\n'
|
||||
|
||||
|
||||
def log(msg: bytes) -> None:
|
||||
print(msg.decode('ascii'), end='', flush=True)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
s = socket.socket()
|
||||
s.bind(('0.0.0.0', 1234))
|
||||
@ -62,14 +67,14 @@ def main() -> None:
|
||||
print('Ready for connections')
|
||||
|
||||
while True:
|
||||
c, a = s.accept()
|
||||
print(f'Accepting connection from {a}')
|
||||
c, (addr, port) = s.accept()
|
||||
print(f'Accepting connection from [{addr}]:{port}')
|
||||
try:
|
||||
while True:
|
||||
req = c.recv(1024)
|
||||
print(req.decode('ascii'), end='')
|
||||
log(req)
|
||||
res = handle_systec_it3000a(req)
|
||||
print(res.decode('ascii'), end='')
|
||||
log(res)
|
||||
c.send(res)
|
||||
except:
|
||||
pass
|
||||
|
Reference in New Issue
Block a user