From 5b677ccb4e5291eb1d21f9ef13fd1da221a23956 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 14 Aug 2023 19:04:35 +0200 Subject: [PATCH] Update simulator.py --- waagen/simulator.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/waagen/simulator.py b/waagen/simulator.py index 87b59f0..193d431 100755 --- a/waagen/simulator.py +++ b/waagen/simulator.py @@ -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