33
44An example of using simulator datastore with json interface.
55
6- usage: server_simulator.py [-h] [--path JSON_FILE]
6+ usage: server_simulator.py [-h]
77 [--log {critical,error,warning,info,debug}]
88 [--port PORT]
99
1010Command line options for examples
1111
1212options:
1313 -h, --help show this help message and exit
14- --json JSON_FILE path to json device configuration file
1514 --log {critical,error,warning,info,debug}
1615 "critical", "error", "warning", "info" or "debug"
1716 --port PORT the port to use
3130
3231_logger = logging .getLogger ()
3332
33+ demo_config = {
34+ "setup" : {
35+ "co size" : 100 ,
36+ "di size" : 150 ,
37+ "hr size" : 200 ,
38+ "ir size" : 250 ,
39+ "shared blocks" : True ,
40+ "type exception" : False ,
41+ "defaults" : {
42+ "value" : {
43+ "bits" : 0x0708 ,
44+ "uint16" : 1 ,
45+ "uint32" : 45000 ,
46+ "float32" : 127.4 ,
47+ "string" : "X" ,
48+ },
49+ "action" : {
50+ "bits" : None ,
51+ "uint16" : None ,
52+ "uint32" : None ,
53+ "float32" : None ,
54+ "string" : None ,
55+ },
56+ },
57+ },
58+ "invalid" : [
59+ 1 ,
60+ [3 , 4 ],
61+ ],
62+ "write" : [
63+ 5 ,
64+ [7 , 8 ],
65+ [16 , 18 ],
66+ [21 , 26 ],
67+ [31 , 36 ],
68+ ],
69+ "bits" : [
70+ 5 ,
71+ [7 , 8 ],
72+ {"addr" : 10 , "value" : 0x81 },
73+ {"addr" : [11 , 12 ], "value" : 0x04342 },
74+ {"addr" : 13 , "action" : "reset" },
75+ {"addr" : 14 , "value" : 15 , "action" : "reset" },
76+ ],
77+ "uint16" : [
78+ {"addr" : 16 , "value" : 3124 },
79+ {"addr" : [17 , 18 ], "value" : 5678 },
80+ {"addr" : [19 , 20 ], "value" : 14661 , "action" : "increment" },
81+ ],
82+ "uint32" : [
83+ {"addr" : 21 , "value" : 3124 },
84+ {"addr" : [23 , 25 ], "value" : 5678 },
85+ {"addr" : [27 , 29 ], "value" : 345000 , "action" : "increment" },
86+ ],
87+ "float32" : [
88+ {"addr" : 31 , "value" : 3124.17 },
89+ {"addr" : [33 , 35 ], "value" : 5678.19 },
90+ {"addr" : [37 , 39 ], "value" : 345000.18 , "action" : "increment" },
91+ ],
92+ "string" : [
93+ {"addr" : [41 , 42 ], "value" : "Str" },
94+ {"addr" : [43 , 44 ], "value" : "Strxyz" },
95+ ],
96+ "repeat" : [{"addr" : [0 , 45 ], "to" : [46 , 138 ]}],
97+ }
98+
99+
100+ def custom_action1 (_inx , _cell ):
101+ """Test action."""
102+
103+
104+ def custom_action2 (_inx , _cell ):
105+ """Test action."""
106+
107+
108+ demo_actions = {
109+ "custom1" : custom_action1 ,
110+ "custom2" : custom_action2 ,
111+ }
112+
34113
35114def get_commandline ():
36115 """Read and validate command line arguments"""
@@ -43,12 +122,6 @@ def get_commandline():
43122 type = str ,
44123 )
45124 parser .add_argument ("--port" , help = "set port" , type = str , default = "5020" )
46- parser .add_argument (
47- "--json" ,
48- help = "path to json device configuration file" ,
49- default = None ,
50- type = str ,
51- )
52125 args = parser .parse_args ()
53126
54127 pymodbus_apply_logging_config ()
@@ -58,15 +131,14 @@ def get_commandline():
58131 return args
59132
60133
61- def setup_simulator (args , json_dict = None ):
134+ def setup_simulator (args , setup = None , actions = None ):
62135 """Run server setup."""
63136 _logger .info ("### Create datastore" )
64- context = ModbusSimulatorContext ()
65-
66- if args .json :
67- context .load_file (args .json , None )
68- else :
69- context .load_dict (json_dict , None )
137+ if not setup :
138+ setup = demo_config
139+ if not actions :
140+ actions = demo_actions
141+ context = ModbusSimulatorContext (setup , actions )
70142 args .context = ModbusServerContext (slaves = context , single = True )
71143 return args
72144
0 commit comments