5
5
import asyncio
6
6
import json
7
7
import click
8
- from pymodbus .utilities import IS_PYTHON3
8
+ from pymodbus .compat import IS_PYTHON3 , PYTHON_VERSION
9
9
from pymodbus .framer .socket_framer import ModbusSocketFramer
10
10
from pymodbus .server .reactive .main import (
11
11
ReactiveServer , DEFAULT_FRAMER , DEFUALT_HANDLERS )
12
12
from pymodbus .server .reactive .default_config import DEFUALT_CONFIG
13
13
from pymodbus .repl .server .cli import run_repl
14
14
15
+ if IS_PYTHON3 and PYTHON_VERSION > (3 , 7 ):
16
+ CANCELLED_ERROR = asyncio .exceptions .CancelledError
17
+ else :
18
+ CANCELLED_ERROR = asyncio .CancelledError
19
+
15
20
16
21
@click .group ("ReactiveModbusServer" )
17
22
@click .option ("--host" , default = "localhost" , help = "Host address" )
@@ -38,7 +43,7 @@ def server(ctx, host, web_port, broadcast_support, repl, verbose):
38
43
pymodbus_logger .setLevel (logging .ERROR )
39
44
logger .setLevel (logging .ERROR )
40
45
41
- ctx .obj = {"repl" : repl , "host" : host , "port " : web_port ,
46
+ ctx .obj = {"repl" : repl , "host" : host , "web_port " : web_port ,
42
47
"broadcast" : broadcast_support }
43
48
44
49
@@ -52,11 +57,17 @@ def server(ctx, host, web_port, broadcast_support, repl, verbose):
52
57
case_sensitive = False ),
53
58
help = "Modbus framer to use" )
54
59
@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" )
56
61
@click .option ("--modbus-config" , type = click .Path (exists = True ),
57
62
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." )
58
68
@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 ):
60
71
"""
61
72
Run Reactive Modbus server exposing REST endpoint
62
73
for response manipulation.
@@ -82,6 +93,7 @@ def run(ctx, modbus_server, modbus_framer, modbus_port, modbus_unit_id, modbus_c
82
93
handler = DEFUALT_HANDLERS .get (handler .strip ())
83
94
84
95
modbus_config ["handler" ] = handler
96
+ modbus_config ["randomize" ] = randomize
85
97
app = ReactiveServer .factory (modbus_server , framer ,
86
98
modbus_port = modbus_port ,
87
99
unit = modbus_unit_id ,
@@ -96,7 +108,7 @@ def run(ctx, modbus_server, modbus_framer, modbus_port, modbus_unit_id, modbus_c
96
108
else :
97
109
app .run ()
98
110
99
- except asyncio . exceptions . CancelledError :
111
+ except CANCELLED_ERROR :
100
112
print ("Done!!!!!" )
101
113
102
114
0 commit comments