Skip to content

Commit 660c697

Browse files
authored
Server simulator with datastore with json data. (#1157)
1 parent 5b6160e commit 660c697

File tree

5 files changed

+620
-2
lines changed

5 files changed

+620
-2
lines changed

doc/source/library/pymodbus.datastore.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ pymodbus\.datastore\.remote module
3232
:undoc-members:
3333
:show-inheritance:
3434

35+
pymodbus\.datastore\.simulator module
36+
-------------------------------------
37+
38+
.. automodule:: pymodbus.datastore.simulator
39+
:members:
40+
:member-order: bysource
41+
3542
pymodbus\.datastore\.store module
3643
---------------------------------
3744

3845
.. automodule:: pymodbus.datastore.store
3946
:members:
4047
:undoc-members:
4148
:show-inheritance:
42-
43-

examples/server_simulator.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
"""Pymodbus Simulator Example.
3+
4+
An example of using simulator datastore with json interface.
5+
6+
usage: server_simulator.py [-h] [--path JSON_FILE]
7+
[--log {critical,error,warning,info,debug}]
8+
[--port PORT]
9+
10+
Command line options for examples
11+
12+
options:
13+
-h, --help show this help message and exit
14+
--path JSON_FILE path to json device configuration file
15+
--log {critical,error,warning,info,debug}
16+
"critical", "error", "warning", "info" or "debug"
17+
--port PORT the port to use
18+
19+
The corresponding client can be started as:
20+
python3 client_sync.py
21+
"""
22+
import argparse
23+
import asyncio
24+
import logging
25+
26+
from pymodbus import pymodbus_apply_logging_config
27+
from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext
28+
from pymodbus.server import StartAsyncTcpServer
29+
from pymodbus.transaction import ModbusSocketFramer
30+
31+
32+
_logger = logging.getLogger()
33+
34+
35+
def get_commandline():
36+
"""Read and validate command line arguments"""
37+
parser = argparse.ArgumentParser(description="Run server simulator.")
38+
parser.add_argument(
39+
"--log",
40+
choices=["critical", "error", "warning", "info", "debug"],
41+
help="set log level, default is info",
42+
default="info",
43+
type=str,
44+
)
45+
parser.add_argument(
46+
"--port",
47+
help="set port",
48+
type=str,
49+
)
50+
parser.add_argument(
51+
"--json",
52+
help="path to json device configuration file",
53+
default=None,
54+
type=str,
55+
)
56+
args = parser.parse_args()
57+
58+
pymodbus_apply_logging_config()
59+
_logger.setLevel(args.log.upper())
60+
args.framer = ModbusSocketFramer
61+
args.port = int(args.port) or 5020
62+
return args
63+
64+
65+
def setup_server(args, json_dict=None):
66+
"""Run server setup."""
67+
_logger.info("### Create datastore")
68+
context = ModbusSimulatorContext()
69+
70+
if args.path:
71+
context.load_file(args.path, None)
72+
else:
73+
context.load_dict(json_dict, None)
74+
args.context = ModbusServerContext(slaves=context, single=True)
75+
return args
76+
77+
78+
async def run_server_simulator(args):
79+
"""Run server."""
80+
_logger.info("### start server simulator")
81+
await StartAsyncTcpServer(
82+
context=args.context,
83+
address=("", args.port) if args.port else None,
84+
framer=args.framer, # The framer strategy to use
85+
allow_reuse_address=True, # allow the reuse of an address
86+
)
87+
88+
89+
if __name__ == "__main__":
90+
cmd_args = get_commandline()
91+
run_args = setup_server(cmd_args)
92+
asyncio.run(run_server_simulator(run_args), debug=True)

pymodbus/datastore/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Define datastore."""
22
from pymodbus.datastore.context import ModbusServerContext, ModbusSlaveContext
3+
from pymodbus.datastore.simulator import ModbusSimulatorContext
34
from pymodbus.datastore.store import (
45
ModbusSequentialDataBlock,
56
ModbusSparseDataBlock,
@@ -14,4 +15,5 @@
1415
"ModbusSparseDataBlock",
1516
"ModbusSlaveContext",
1617
"ModbusServerContext",
18+
"ModbusSimulatorContext",
1719
]

0 commit comments

Comments
 (0)