Skip to content

Commit 6bff564

Browse files
committed
Update examples.
1 parent 877021c commit 6bff564

17 files changed

+243
-511
lines changed

README.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ Otherwise you can pull the trunk source and install from there::
203203
cd pymodbus
204204
pip install -r requirements.txt
205205

206+
Before cloning the repo, you need to install python3 (preferable 3.10)
207+
and make a virtual environment::
208+
209+
python3 -m venv /path/to/new/virtual/environment
210+
211+
To activeate the virtual environment please do::
212+
213+
source .venv/bin/activate
214+
206215

207216
To get latest release (for now v2.5.3 with Python 2.7 support)::
208217

@@ -216,8 +225,10 @@ To get a specific version:
216225

217226
git checkout tags/vX.Y.Z -b vX.Y.Z
218227

219-
Then::
228+
Then:
229+
220230
pip install -r requirements.txt
231+
221232
pip install -e .
222233

223234
This installs pymodbus in your virtual environment with pointers directly to the pymodbus directory, so any change you make is immediately available as if installed.
@@ -262,23 +273,26 @@ Development Instructions
262273
The current code base is compatible python >= 3.8.
263274
Here are some of the common commands to perform a range of activities
264275

265-
::
266276
pip install -r requirements.txt install all requirements
277+
267278
pip install -e . source directory is "release", useful for testing
268279

269280
tox -e py38 (or py39, py310, pypy38) Run pytest on source code
270281

271282
tox -e pylint Run pylint on source code
283+
272284
tox -e codespell Run codespell on source code
285+
273286
tox -e bandit Run bandit on source code
287+
274288
tox -e flake8 Run flake8 on source code
289+
275290
tox -e black Run black on source code
276291

277292
------------------------------------------------------------
278293
Generate documentation
279294
------------------------------------------------------------
280295

281-
::
282296
cd doc
283297
make clean
284298
make html
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
==================================================
22
Modbus Payload Example
33
==================================================
4-
.. literalinclude:: ../../../examples/common/payload_client.py
4+
.. literalinclude:: ../../../examples/client_payload.py

doc/source/example/modules.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ Examples
1313
client_async
1414
client_async_basic_calls
1515
client_async_extended_calls
16+
client_payload
1617
changing_framers
1718
custom_datablock
1819
custom_message
1920
dbstore_update_server
2021
modbus_logging
2122
modbus_forwarder
2223
modbus_serial_forwarder
23-
payload_client
24-
payload_server
2524
performance
2625
server_sync
2726
server_async
27+
server_payload
2828
updating_server
2929

3030
bcd_payload
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
==================================================
22
Modbus Payload Server Example
33
==================================================
4-
.. literalinclude:: ../../../examples/common/payload_server.py
4+
.. literalinclude:: ../../../examples/server_payload.py

examples/client_async.py

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,27 @@
2020
The corresponding server must be started before e.g. as:
2121
python3 server_sync.py
2222
"""
23-
import argparse
2423
import os
2524
import asyncio
2625
import logging
2726

2827
# --------------------------------------------------------------------------- #
2928
# import the various client implementations
3029
# --------------------------------------------------------------------------- #
30+
from examples.helper import get_commandline
3131
from pymodbus.client import (
3232
AsyncModbusSerialClient,
3333
AsyncModbusTcpClient,
3434
AsyncModbusTlsClient,
3535
AsyncModbusUdpClient,
3636
)
37-
from pymodbus.transaction import (
38-
ModbusAsciiFramer,
39-
ModbusBinaryFramer,
40-
ModbusRtuFramer,
41-
ModbusSocketFramer,
42-
ModbusTlsFramer,
43-
)
4437

4538

46-
def setup_async_client(args=None):
39+
_logger = logging.getLogger()
40+
41+
42+
def setup_async_client(args):
4743
"""Run client setup."""
48-
if not args:
49-
args = get_commandline()
50-
if args.comm != "serial" and args.port:
51-
args.port = int(args.port)
5244
_logger.info("### Create client object")
5345
if args.comm == "tcp":
5446
client = AsyncModbusTcpClient(
@@ -134,69 +126,10 @@ async def run_async_client(client, modbus_calls=None):
134126
_logger.info("### End of Program")
135127

136128

137-
# --------------------------------------------------------------------------- #
138-
# Extra code, to allow commandline parameters instead of changing the code
139-
# --------------------------------------------------------------------------- #
140-
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
141-
logging.basicConfig(format=FORMAT)
142-
_logger = logging.getLogger()
143-
144-
145-
def get_commandline():
146-
"""Read and validate command line arguments"""
147-
parser = argparse.ArgumentParser(
148-
description="Connect/disconnect a synchronous client."
149-
)
150-
parser.add_argument(
151-
"--comm",
152-
choices=["tcp", "udp", "serial", "tls"],
153-
help='"serial", "tcp", "udp" or "tls"',
154-
type=str,
155-
)
156-
parser.add_argument(
157-
"--framer",
158-
choices=["ascii", "binary", "rtu", "socket", "tls"],
159-
help='"ascii", "binary", "rtu", "socket" or "tls"',
160-
type=str,
161-
)
162-
parser.add_argument(
163-
"--log",
164-
choices=["critical", "error", "warning", "info", "debug"],
165-
help='"critical", "error", "warning", "info" or "debug"',
166-
type=str,
167-
)
168-
parser.add_argument(
169-
"--port",
170-
help="the port to use",
171-
type=str,
172-
)
173-
args = parser.parse_args()
174-
175-
# set defaults
176-
comm_defaults = {
177-
"tcp": ["socket", 5020],
178-
"udp": ["socket", 5020],
179-
"serial": ["rtu", "/dev/ptyp0"],
180-
"tls": ["tls", 5020],
181-
}
182-
framers = {
183-
"ascii": ModbusAsciiFramer,
184-
"binary": ModbusBinaryFramer,
185-
"rtu": ModbusRtuFramer,
186-
"socket": ModbusSocketFramer,
187-
"tls": ModbusTlsFramer,
188-
}
189-
_logger.setLevel(args.log.upper() if args.log else logging.INFO)
190-
if not args.comm:
191-
args.comm = "tcp"
192-
if not args.framer:
193-
args.framer = comm_defaults[args.comm][0]
194-
args.port = args.port or comm_defaults[args.comm][1]
195-
args.framer = framers[args.framer]
196-
return args
197-
198-
199129
if __name__ == "__main__":
200-
# Connect/disconnect no calls.
201-
testclient = setup_async_client()
130+
cmd_args = get_commandline(
131+
server=False,
132+
description="Run asynchronous client.",
133+
)
134+
testclient = setup_async_client(cmd_args)
202135
asyncio.run(run_async_client(testclient), debug=True)

examples/client_async_basic_calls.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import asyncio
1010
import logging
1111

12+
from examples.helper import get_commandline
1213
from examples.client_async import run_async_client, setup_async_client
1314

1415

16+
_logger = logging.getLogger()
17+
1518
SLAVE = 0x01
1619

1720

@@ -118,14 +121,11 @@ async def run_async_basic_calls(client):
118121
await _handle_holding_registers(client)
119122
await _handle_input_registers(client)
120123

121-
# --------------------------------------------------------------------------- #
122-
# Extra code, to allow commandline parameters instead of changing the code
123-
# --------------------------------------------------------------------------- #
124-
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
125-
logging.basicConfig(format=FORMAT)
126-
_logger = logging.getLogger()
127-
128124

129125
if __name__ == "__main__":
130-
testclient = setup_async_client()
126+
cmd_args = get_commandline(
127+
server=False,
128+
description="Run basic calls in asynchronous client.",
129+
)
130+
testclient = setup_async_client(cmd_args)
131131
asyncio.run(run_async_client(testclient, modbus_calls=run_async_basic_calls))

examples/client_async_extended_calls.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import asyncio
2626
import logging
2727

28+
from examples.helper import get_commandline
2829
from examples.client_async import run_async_client, setup_async_client
2930

3031
from pymodbus.diag_message import (
@@ -53,6 +54,7 @@
5354
ReportSlaveIdRequest,
5455
)
5556

57+
_logger = logging.getLogger()
5658

5759
UNIT = 0x01
5860

@@ -175,5 +177,9 @@ async def run_async_ext_calls(client):
175177

176178

177179
if __name__ == "__main__":
178-
testclient = setup_async_client()
180+
cmd_args = get_commandline(
181+
server=False,
182+
description="Run extended calls in asynchronous client.",
183+
)
184+
testclient = setup_async_client(cmd_args)
179185
asyncio.run(run_async_client(testclient, modbus_calls=run_async_ext_calls))

examples/common/payload_client.py renamed to examples/client_payload.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
"""Pymodbus Client Payload Example.
23
34
This example shows how to build a client with a

examples/client_sync.py

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,26 @@
2020
The corresponding server must be started before e.g. as:
2121
python3 server_sync.py
2222
"""
23-
import argparse
2423
import os
2524
import logging
2625

2726
# --------------------------------------------------------------------------- #
2827
# import the various client implementations
2928
# --------------------------------------------------------------------------- #
29+
from examples.helper import get_commandline
3030
from pymodbus.client import (
3131
ModbusSerialClient,
3232
ModbusTcpClient,
3333
ModbusTlsClient,
3434
ModbusUdpClient,
3535
)
36-
from pymodbus.transaction import (
37-
ModbusAsciiFramer,
38-
ModbusBinaryFramer,
39-
ModbusRtuFramer,
40-
ModbusSocketFramer,
41-
ModbusTlsFramer,
42-
)
4336

4437

45-
def setup_sync_client(args=None):
38+
_logger = logging.getLogger()
39+
40+
41+
def setup_sync_client(args):
4642
"""Run client setup."""
47-
if not args:
48-
args = get_commandline()
49-
if args.comm != "serial" and args.port:
50-
args.port = int(args.port)
5143
_logger.info("### Create client object")
5244
if args.comm == "tcp":
5345
client = ModbusTcpClient(
@@ -132,69 +124,10 @@ def run_sync_client(client, modbus_calls=None):
132124
_logger.info("### End of Program")
133125

134126

135-
# --------------------------------------------------------------------------- #
136-
# Extra code, to allow commandline parameters instead of changing the code
137-
# --------------------------------------------------------------------------- #
138-
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
139-
logging.basicConfig(format=FORMAT)
140-
_logger = logging.getLogger()
141-
142-
143-
def get_commandline():
144-
"""Read and validate command line arguments"""
145-
parser = argparse.ArgumentParser(
146-
description="Connect/disconnect a synchronous client."
147-
)
148-
parser.add_argument(
149-
"--comm",
150-
choices=["tcp", "udp", "serial", "tls"],
151-
help='"serial", "tcp", "udp" or "tls"',
152-
type=str,
153-
)
154-
parser.add_argument(
155-
"--framer",
156-
choices=["ascii", "binary", "rtu", "socket", "tls"],
157-
help='"ascii", "binary", "rtu", "socket" or "tls"',
158-
type=str,
159-
)
160-
parser.add_argument(
161-
"--log",
162-
choices=["critical", "error", "warning", "info", "debug"],
163-
help='"critical", "error", "warning", "info" or "debug"',
164-
type=str,
165-
)
166-
parser.add_argument(
167-
"--port",
168-
help="the port to use",
169-
type=str,
170-
)
171-
args = parser.parse_args()
172-
173-
# set defaults
174-
comm_defaults = {
175-
"tcp": ["socket", 5020],
176-
"udp": ["socket", 5020],
177-
"serial": ["rtu", "/dev/ptyp0"],
178-
"tls": ["tls", 5020],
179-
}
180-
framers = {
181-
"ascii": ModbusAsciiFramer,
182-
"binary": ModbusBinaryFramer,
183-
"rtu": ModbusRtuFramer,
184-
"socket": ModbusSocketFramer,
185-
"tls": ModbusTlsFramer,
186-
}
187-
_logger.setLevel(args.log.upper() if args.log else logging.INFO)
188-
if not args.comm:
189-
args.comm = "tcp"
190-
if not args.framer:
191-
args.framer = comm_defaults[args.comm][0]
192-
args.port = args.port or comm_defaults[args.comm][1]
193-
args.framer = framers[args.framer]
194-
return args
195-
196-
197127
if __name__ == "__main__":
198-
# Connect/disconnect no calls.
199-
testclient = setup_sync_client()
128+
cmd_args = get_commandline(
129+
server=False,
130+
description="Run synchronous client.",
131+
)
132+
testclient = setup_sync_client(cmd_args)
200133
run_sync_client(testclient)

0 commit comments

Comments
 (0)