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