55import asyncio
66import json
77import click
8- from pymodbus .utilities import IS_PYTHON3
8+ from pymodbus .compat import IS_PYTHON3 , PYTHON_VERSION
99from pymodbus .framer .socket_framer import ModbusSocketFramer
1010from pymodbus .server .reactive .main import (
1111 ReactiveServer , DEFAULT_FRAMER , DEFUALT_HANDLERS )
1212from pymodbus .server .reactive .default_config import DEFUALT_CONFIG
1313from pymodbus .repl .server .cli import run_repl
1414
15+ if IS_PYTHON3 and PYTHON_VERSION > (3 , 7 ):
16+ CANCELLED_ERROR = asyncio .exceptions .CancelledError
17+ else :
18+ CANCELLED_ERROR = asyncio .CancelledError
19+
1520
1621@click .group ("ReactiveModbusServer" )
1722@click .option ("--host" , default = "localhost" , help = "Host address" )
@@ -38,7 +43,7 @@ def server(ctx, host, web_port, broadcast_support, repl, verbose):
3843 pymodbus_logger .setLevel (logging .ERROR )
3944 logger .setLevel (logging .ERROR )
4045
41- ctx .obj = {"repl" : repl , "host" : host , "port " : web_port ,
46+ ctx .obj = {"repl" : repl , "host" : host , "web_port " : web_port ,
4247 "broadcast" : broadcast_support }
4348
4449
@@ -52,11 +57,17 @@ def server(ctx, host, web_port, broadcast_support, repl, verbose):
5257 case_sensitive = False ),
5358 help = "Modbus framer to use" )
5459@click .option ("--modbus-port" , default = "5020" , help = "Modbus port" )
55- @click .option ("--modbus-unit-id" , default = 1 , help = "Modbus unit id" )
60+ @click .option ("--modbus-unit-id" , default = [ 1 ], multiple = True , help = "Modbus unit id" )
5661@click .option ("--modbus-config" , type = click .Path (exists = True ),
5762 help = "Path to additional modbus server config" )
63+ @click .option ("-r" , "--randomize" , default = 0 , help = "Randomize every `r` reads."
64+ " 0=never, 1=always, "
65+ "2=every-second-read, "
66+ "and so on. "
67+ "Applicable IR and DI." )
5868@click .pass_context
59- def run (ctx , modbus_server , modbus_framer , modbus_port , modbus_unit_id , modbus_config ):
69+ def run (ctx , modbus_server , modbus_framer , modbus_port , modbus_unit_id ,
70+ modbus_config , randomize ):
6071 """
6172 Run Reactive Modbus server exposing REST endpoint
6273 for response manipulation.
@@ -82,6 +93,7 @@ def run(ctx, modbus_server, modbus_framer, modbus_port, modbus_unit_id, modbus_c
8293 handler = DEFUALT_HANDLERS .get (handler .strip ())
8394
8495 modbus_config ["handler" ] = handler
96+ modbus_config ["randomize" ] = randomize
8597 app = ReactiveServer .factory (modbus_server , framer ,
8698 modbus_port = modbus_port ,
8799 unit = modbus_unit_id ,
@@ -96,7 +108,7 @@ def run(ctx, modbus_server, modbus_framer, modbus_port, modbus_unit_id, modbus_c
96108 else :
97109 app .run ()
98110
99- except asyncio . exceptions . CancelledError :
111+ except CANCELLED_ERROR :
100112 print ("Done!!!!!" )
101113
102114
0 commit comments