Skip to content

Commit 73daed1

Browse files
authored
Update datastore_simulator example with client (#1967)
1 parent 9b67640 commit 73daed1

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

doc/source/_static/examples.tgz

979 Bytes
Binary file not shown.

doc/source/_static/examples.zip

1.47 KB
Binary file not shown.

doc/source/examples.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ Source: :github:`examples/simulator.py`
168168
:noindex:
169169

170170

171-
Simulator datastore example
172-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
173-
Source: :github:`examples/datastore_simulator.py`
171+
Simulator datastore (shared storage) example
172+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173+
Source: :github:`examples/datastore_simulator_share.py`
174174

175-
.. automodule:: examples.datastore_simulator
175+
.. automodule:: examples.datastore_simulator_share
176176
:undoc-members:
177177
:noindex:
178178

examples/datastore_simulator.py renamed to examples/datastore_simulator_share.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
44
An example of using simulator datastore with json interface.
55
6+
Detailed description of the device definition can be found at:
7+
8+
https://pymodbus.readthedocs.io/en/latest/source/library/simulator/config.html#device-entries
9+
610
usage::
711
8-
server_simulator.py [-h]
12+
datastore_simulator_share.py [-h]
913
[--log {critical,error,warning,info,debug}]
1014
[--port PORT]
15+
[--test_client]
1116
1217
-h, --help
1318
show this help message and exit
1419
-l, --log {critical,error,warning,info,debug}
1520
set log level
1621
-p, --port PORT
1722
set port to use
23+
--test_client
24+
starts a client to test the configuration
1825
1926
The corresponding client can be started as:
2027
python3 client_sync.py
@@ -25,7 +32,7 @@
2532
import asyncio
2633
import logging
2734

28-
from pymodbus import Framer, pymodbus_apply_logging_config
35+
from pymodbus import pymodbus_apply_logging_config
2936
from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext
3037
from pymodbus.device import ModbusDeviceIdentification
3138
from pymodbus.server import StartAsyncTcpServer
@@ -129,24 +136,22 @@ def get_commandline(cmdline=None):
129136
)
130137
parser.add_argument("--port", help="set port", type=str, default="5020")
131138
parser.add_argument("--host", help="set interface", type=str, default="localhost")
139+
parser.add_argument("--test_client", help="start client to test", action="store_true")
132140
args = parser.parse_args(cmdline)
133-
134141
return args
135142

136143

137144
def setup_simulator(setup=None, actions=None, cmdline=None):
138145
"""Run server setup."""
146+
if not setup:
147+
setup=demo_config
148+
if not actions:
149+
actions=demo_actions
139150
args = get_commandline(cmdline=cmdline)
140151
pymodbus_apply_logging_config(args.log.upper())
141152
_logger.setLevel(args.log.upper())
142-
args.framer = Framer.SOCKET
143153
args.port = int(args.port)
144154

145-
_logger.info("### Create datastore")
146-
if not setup:
147-
setup = demo_config
148-
if not actions:
149-
actions = demo_actions
150155
context = ModbusSimulatorContext(setup, actions)
151156
args.context = ModbusServerContext(slaves=context, single=True)
152157
args.identity = ModbusDeviceIdentification(
@@ -169,7 +174,6 @@ async def run_server_simulator(args):
169174
await StartAsyncTcpServer(
170175
context=args.context,
171176
address=(args.host, args.port),
172-
framer=args.framer,
173177
)
174178

175179

test/sub_examples/test_examples.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from examples.client_calls import template_call
1818
from examples.client_custom_msg import main as main_custom_client
1919
from examples.client_payload import main as main_payload_calls
20-
from examples.datastore_simulator import main as main_datastore_simulator
20+
from examples.datastore_simulator_share import main as main_datastore_simulator_share
2121
from examples.message_generator import generate_messages
2222
from examples.message_parser import main as main_parse_messages
2323
from examples.server_async import setup_server
@@ -82,10 +82,10 @@ async def test_updating_server(self, use_port, use_host):
8282
task.cancel()
8383
await task
8484

85-
async def test_datastore_simulator(self, use_port, use_host):
85+
async def test_datastore_simulator_share(self, use_port, use_host):
8686
"""Test server simulator."""
8787
cmdargs = ["--port", str(use_port), "--host", use_host]
88-
task = asyncio.create_task(main_datastore_simulator(cmdline=cmdargs))
88+
task = asyncio.create_task(main_datastore_simulator_share(cmdline=cmdargs))
8989
task.set_name("run main_datastore_simulator")
9090
await asyncio.sleep(0.1)
9191
testclient = setup_async_client(cmdline=cmdargs)

test/sub_server/test_simulator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ def test_simulator_invalid_config(self):
291291
exc_setup[Label.repeat][0][Label.repeat_to] = [48, 500]
292292
with pytest.raises(RuntimeError):
293293
ModbusSimulatorContext(exc_setup, None)
294+
exc_setup = copy.deepcopy(self.default_config)
295+
exc_setup[Label.type_uint16].append(0)
296+
ModbusSimulatorContext(exc_setup, None)
297+
exc_setup = copy.deepcopy(self.default_config)
298+
exc_setup[Label.type_uint16].append(250)
299+
with pytest.raises(RuntimeError):
300+
ModbusSimulatorContext(exc_setup, None)
301+
294302

295303
def test_simulator_validate_illegal(self):
296304
"""Test validation without exceptions."""

0 commit comments

Comments
 (0)