Skip to content

Commit fda755d

Browse files
authored
Update modbus forwarder example. (#985)
1 parent aae5c21 commit fda755d

File tree

7 files changed

+89
-60
lines changed

7 files changed

+89
-60
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
==================================================
22
Serial Forwarder Example
33
==================================================
4-
.. literalinclude:: ../../../examples/contrib/serial_forwarder.py
4+
.. literalinclude:: ../../../examples/modbus_forwarder.py

doc/source/example/modules.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Examples
1717
custom_datablock
1818
custom_message
1919
modbus_logging
20+
modbus_forwarder
2021
payload_client
2122
payload_server
2223
performance
@@ -37,5 +38,4 @@ Examples
3738
modbus_tls_client
3839
modicon_payload
3940
remote_server_context
40-
serial_forwarder
4141
thread_safe_datastore

examples/client_sync.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ def run_sync_client(modbus_calls=None):
136136
"serial": ["rtu", "/dev/ptyp0"],
137137
"tls": ["tls", 5020]
138138
}
139-
FORMAT = (
140-
"%(asctime)-15s %(threadName)-15s "
141-
"%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
142-
)
139+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
143140
logging.basicConfig(format=FORMAT)
144141
_logger = logging.getLogger()
145142

examples/client_sync_basic_calls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
"""Pymodbus Synchronous Client standard calls rxample.
2+
"""Pymodbus Synchronous Client standard calls example.
33
44
This example uses client_sync.py to handle connection, and have the same options.
55

examples/contrib/serial_forwarder.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

examples/modbus_forwarder.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
"""Pymodbus synchronous forwarder.
3+
4+
This is a repeater or converter and an example of just how powerful datastore is.
5+
6+
It consist of a server (any comm) and a client (any comm) and basically all request
7+
received by the server is sent by client and all responses received by the
8+
client is sent back by the server.
9+
10+
Both server and client are tcp based, but it can be easily modified to any server/client
11+
(see client_sync.py and server_sync.py for other communication types)
12+
"""
13+
import argparse
14+
import logging
15+
16+
from pymodbus.client.sync import ModbusTcpClient
17+
from pymodbus.server.sync import StartTcpServer
18+
from pymodbus.datastore import ModbusServerContext
19+
from pymodbus.datastore.remote import RemoteSlaveContext
20+
21+
22+
def run_forwarder():
23+
"""Run forwarder setup."""
24+
port_server, port_client = get_commandline()
25+
26+
client = ModbusTcpClient(
27+
host="localhost",
28+
port=port_client,
29+
)
30+
31+
# If required to communicate with a specified client use unit=<unit_id>
32+
# in RemoteSlaveContext
33+
# For e.g to forward the requests to slave with unit address 1 use
34+
# store = RemoteSlaveContext(client, unit=1)
35+
store = RemoteSlaveContext(client)
36+
context = ModbusServerContext(slaves=store, single=True)
37+
38+
# start forwarding client and server
39+
client.connect()
40+
StartTcpServer(context, address=("localhost", port_server))
41+
# loop forever
42+
43+
44+
# --------------------------------------------------------------------------- #
45+
# Extra code, to allow commandline parameters instead of changing the code
46+
# --------------------------------------------------------------------------- #
47+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
48+
logging.basicConfig(format=FORMAT)
49+
_logger = logging.getLogger()
50+
51+
52+
def get_commandline():
53+
"""Read and validate command line arguments"""
54+
parser = argparse.ArgumentParser(description="Command line options for examples")
55+
parser.add_argument(
56+
"--log",
57+
choices=["critical", "error", "warning", "info", "debug"],
58+
help='"critical", "error", "warning", "info" or "debug"',
59+
type=str,
60+
)
61+
parser.add_argument(
62+
"--port",
63+
help='the port to use',
64+
type=int,
65+
)
66+
parser.add_argument(
67+
"--port_client",
68+
help='the port to use',
69+
type=int,
70+
)
71+
args = parser.parse_args()
72+
73+
# set defaults
74+
_logger.setLevel(args.log.upper() if args.log else logging.INFO)
75+
if not args.port:
76+
args.port = 5020
77+
if not args.port_client:
78+
args.port_client = 5010
79+
80+
return args.port, args.port_client
81+
82+
83+
if __name__ == "__main__":
84+
run_forwarder()

examples/server_sync.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ def run_server():
235235
"serial": ("rtu", "/dev/ptyp0"),
236236
"tls": ("tls", 5020)
237237
}
238-
FORMAT = (
239-
"%(asctime)-15s %(threadName)-15s "
240-
"%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
241-
)
238+
FORMAT = "%(asctime)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
242239
logging.basicConfig(format=FORMAT)
243240
_logger = logging.getLogger()
244241

0 commit comments

Comments
 (0)