Skip to content

Commit d9fdc5a

Browse files
authored
Rectify sync/async client parameters. (#1013)
1 parent 35c99a6 commit d9fdc5a

18 files changed

+1091
-959
lines changed
Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,55 @@
1-
pymodbus\.client package
2-
========================
1+
pymodbus\.client
2+
================
33

4-
Pymodbus offers a :mod:`synchronous client <pymodbus.client>`, and async clients based on :mod:`asyncio <pymodbus.client.asynchronous>`.
4+
Pymodbus offers a :mod:`synchronous client <pymodbus.client>` and a :mod:`client based on asyncio <pymodbus.client.asynchronous>`.
55

6-
Each client shares a :mod:`client mixin <pymodbus.client.helper_sync>` which offers simple methods for reading and writing.
6+
In general each transports (Serial, TCP, TLS and UDP) have its own class.
7+
However the actual implementation is highly shared.
78

8-
Submodules
9-
----------
9+
AsyncModbusSerialClient class
10+
-----------------------------
1011

11-
pymodbus\.client\.helper_sync module
12-
------------------------------------
13-
14-
.. automodule:: pymodbus.client.helper_sync
12+
.. automodule:: pymodbus.client.async_serial
1513
:members:
16-
:undoc-members:
17-
:show-inheritance:
1814

19-
pymodbus\.client\.ModbusSerialClient module
20-
-------------------------------------------
15+
ModbusSerialClient class
16+
------------------------
2117

2218
.. automodule:: pymodbus.client.sync_serial
2319
:members:
24-
:undoc-members:
25-
:show-inheritance:
26-
27-
pymodbus\.client\.ModbusTCPClient module
28-
----------------------------------------
29-
30-
.. automodule:: pymodbus.client.sync_tcp
31-
:members:
32-
:undoc-members:
33-
:show-inheritance:
3420

35-
pymodbus\.client\.ModbusTLSClient module
36-
----------------------------------------
21+
AsyncModbusTcpClient class
22+
--------------------------
3723

38-
.. automodule:: pymodbus.client.sync_tls
24+
.. automodule:: pymodbus.client.async_tcp
3925
:members:
40-
:undoc-members:
41-
:show-inheritance:
4226

43-
pymodbus\.client\.ModbusUDPClient module
44-
----------------------------------------
27+
ModbusTcpClient class
28+
---------------------
4529

46-
.. automodule:: pymodbus.client.sync_udp
30+
.. automodule:: pymodbus.client.sync_tcp
4731
:members:
48-
:undoc-members:
49-
:show-inheritance:
5032

51-
pymodbus\.client\.AsyncModbusUDPClient module
52-
---------------------------------------------
33+
AsyncModbusTlsClient class
34+
--------------------------
5335

54-
.. automodule:: pymodbus.client.async_udp
36+
.. automodule:: pymodbus.client.async_tls
5537
:members:
56-
:undoc-members:
57-
:show-inheritance:
5838

59-
pymodbus\.client\.AsyncModbusTLSClient module
60-
---------------------------------------------
39+
ModbusTlsClient class
40+
---------------------
6141

62-
.. automodule:: pymodbus.client.async_tls
42+
.. automodule:: pymodbus.client.sync_tls
6343
:members:
64-
:undoc-members:
65-
:show-inheritance:
6644

67-
pymodbus\.client\.AsyncModbusSerialClient module
68-
------------------------------------------------
45+
AsyncModbusUdpClient class
46+
--------------------------
6947

70-
.. automodule:: pymodbus.client.async_serial
48+
.. automodule:: pymodbus.client.async_udp
7149
:members:
72-
:undoc-members:
73-
:show-inheritance:
74-
7550

76-
pymodbus\.client\.AsyncModbusTCPClient module
77-
---------------------------------------------
51+
ModbusUdpClient class
52+
---------------------
7853

79-
.. automodule:: pymodbus.client.async_tcp
54+
.. automodule:: pymodbus.client.sync_udp
8055
:members:
81-
:undoc-members:
82-
:show-inheritance:

examples/client_async.py

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
# import the various client implementations
3030
# --------------------------------------------------------------------------- #
3131
from pymodbus.client import (
32-
AsyncModbusUDPClient,
33-
AsyncModbusTLSClient,
3432
AsyncModbusSerialClient,
35-
AsyncModbusTCPClient,
33+
AsyncModbusTcpClient,
34+
AsyncModbusTlsClient,
35+
AsyncModbusUdpClient,
3636
)
3737
from pymodbus.transaction import (
3838
ModbusAsciiFramer,
@@ -49,54 +49,75 @@ async def setup_async_client():
4949
_logger.info("### Create client object")
5050

5151
if args.comm == "tcp":
52-
client = AsyncModbusTCPClient(
53-
"127.0.0.1", # define tcp address where to connect to.
52+
client = AsyncModbusTcpClient(
53+
"127.0.0.1",
5454
port=args.port, # on which port
55-
framer=ModbusSocketFramer, # how to interpret the messages
56-
timeout=1, # waiting time for request to complete
57-
retries=3, # retries per transaction
58-
retry_on_empty=False, # Is an empty response a retry
59-
source_address=("localhost", 0), # bind socket to address
60-
strict=True, # use strict timing, t1.5 for Modbus RTU
55+
# Common optional paramers:
56+
# protocol_class=ModbusClientProtocol,
57+
# modbus_decoder=ClientDecoder,
58+
framer=args.framer,
59+
# timeout=10,
60+
# retries=3,
61+
# retry_on_empty=False,
62+
# close_comm_on_error=False,
63+
# strict=True,
64+
# TCP setup parameters
65+
# source_address=("localhost", 0),
6166
)
6267
elif args.comm == "udp":
63-
client = AsyncModbusUDPClient(
64-
"localhost", # define tcp address where to connect to.
65-
port=args.port, # on which port
66-
framer=args.framer, # how to interpret the messages
67-
timeout=1, # waiting time for request to complete
68-
retries=3, # retries per transaction
69-
retry_on_empty=False, # Is an empty response a retry
70-
source_address=("localhost", 0), # bind socket to address
71-
strict=True, # use strict timing, t1.5 for Modbus RTU
68+
client = AsyncModbusUdpClient(
69+
"localhost",
70+
# port=502,
71+
# Common optional paramers:
72+
# protocol_class=ModbusClientProtocol,
73+
# modbus_decoder=ClientDecoder,
74+
framer=args.framer,
75+
# timeout=10,
76+
# retries=3,
77+
# retry_on_empty=False,
78+
# close_comm_on_error=False,
79+
# strict=True,
80+
# UDP setup parameters
81+
# source_address=None,
7282
)
7383
elif args.comm == "serial":
7484
client = AsyncModbusSerialClient(
75-
args.port, # serial port
76-
framer=args.framer, # how to interpret the messages
77-
stopbits=1, # The number of stop bits to use
78-
bytesize=7, # The bytesize of the serial messages
79-
parity="even", # Which kind of parity to use
80-
baudrate=9600, # The baud rate to use for the serial device
81-
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
82-
timeout=1, # waiting time for request to complete
83-
strict=True, # use strict timing, t1.5 for Modbus RTU
85+
args.port,
86+
# Common optional paramers:
87+
# protocol_class=ModbusClientProtocol,
88+
# modbus_decoder=ClientDecoder,
89+
# framer=ModbusRtuFramer,
90+
# timeout=10,
91+
# retries=3,
92+
# retry_on_empty=False,
93+
# close_comm_on_error=False,
94+
# strict=True,
95+
# Serial setup parameters
96+
# baudrate=9600,
97+
# bytesize=8,
98+
# parity="N",
99+
# stopbits=1,
100+
# handle_local_echo=False,
84101
)
85102
elif args.comm == "tls":
86-
client = AsyncModbusTLSClient(
87-
host="localhost", # define tcp address where to connect to.
88-
port=args.port, # on which port
89-
sslctx=None, # ssl control
90-
certfile=None, # certificate file
91-
keyfile=None, # key file
92-
password=None, # pass phrase
93-
framer=args.framer, # how to interpret the messages
94-
timeout=1, # waiting time for request to complete
95-
retries=3, # retries per transaction
96-
retry_on_empty=False, # Is an empty response a retry
97-
source_address=("localhost", 0), # bind socket to address
98-
server_hostname="localhost", # used for cert verification
99-
strict=True, # use strict timing, t1.5 for Modbus RTU
103+
client = AsyncModbusTlsClient(
104+
"localhost",
105+
port=args.port,
106+
# Common optional paramers:
107+
# protocol_class=None,
108+
# modbus_decoder=ClientDecoder,
109+
framer=args.framer,
110+
# timeout=10,
111+
# retries=3,
112+
# retry_on_empty=False,
113+
# close_comm_on_error=False,
114+
# strict=True,
115+
# TLS setup parameters
116+
# sslctx=None,
117+
# certfile=None,
118+
# keyfile=None,
119+
# password=None,
120+
# server_hostname="localhost",
100121
)
101122
await client.start()
102123
return client

examples/client_sync.py

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,74 @@ def setup_sync_client():
4848
_logger.info("### Create client object")
4949
if args.comm == "tcp":
5050
client = ModbusTcpClient(
51-
host="127.0.0.1", # define tcp address where to connect to.
52-
port=args.port, # on which port
53-
framer=args.framer, # how to interpret the messages
54-
timeout=1, # waiting time for request to complete
55-
retries=3, # retries per transaction
56-
retry_on_empty=False, # Is an empty response a retry
57-
source_address=("localhost", 0), # bind socket to address
58-
strict=True, # use strict timing, t1.5 for Modbus RTU
51+
"127.0.0.1",
52+
port=args.port,
53+
# Common optional paramers:
54+
# protocol_class=None,
55+
# modbus_decoder=ClientDecoder,
56+
framer=args.framer,
57+
# timeout=10,
58+
# retries=3,
59+
# retry_on_empty=False,y
60+
# close_comm_on_error=False,
61+
# strict=True,
62+
# TCP setup parameters
63+
# source_address=("localhost", 0),
5964
)
6065
elif args.comm == "udp":
6166
client = ModbusUdpClient(
62-
host="localhost", # define tcp address where to connect to.
63-
port=args.port, # on which port
64-
framer=args.framer, # how to interpret the messages
65-
timeout=1, # waiting time for request to complete
66-
retries=3, # retries per transaction
67-
retry_on_empty=False, # Is an empty response a retry
68-
source_address=("localhost", 0), # bind socket to address
69-
strict=True, # use strict timing, t1.5 for Modbus RTU
67+
"localhost",
68+
# port=502,
69+
# Common optional paramers:
70+
# protocol_class=ModbusClientProtocol,
71+
# modbus_decoder=ClientDecoder,
72+
framer=args.framer,
73+
# timeout=10,
74+
# retries=3,
75+
# retry_on_empty=False,
76+
# close_comm_on_error=False,
77+
# strict=True,
78+
# UDP setup parameters
79+
# source_address=None,
7080
)
7181
elif args.comm == "serial":
7282
client = ModbusSerialClient(
7383
port=args.port, # serial port
74-
framer=args.framer, # how to interpret the messages
75-
stopbits=1, # The number of stop bits to use
76-
bytesize=7, # The bytesize of the serial messages
77-
parity="E", # Which kind of parity to use
78-
baudrate=9600, # The baud rate to use for the serial device
79-
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
80-
timeout=1, # waiting time for request to complete
81-
strict=True, # use strict timing, t1.5 for Modbus RTU
84+
# Common optional paramers:
85+
# protocol_class=ModbusClientProtocol,
86+
# modbus_decoder=ClientDecoder,
87+
# framer=ModbusRtuFramer,
88+
# timeout=10,
89+
# retries=3,
90+
# retry_on_empty=False,
91+
# close_comm_on_error=False,.
92+
# strict=True,
93+
# Serial setup parameters
94+
# baudrate=9600,
95+
# bytesize=8,
96+
# parity="N",
97+
# stopbits=1,
98+
# handle_local_echo=False,
8299
)
83100
elif args.comm == "tls":
84101
client = ModbusTlsClient(
85-
host="localhost", # define tcp address where to connect to.
86-
port=args.port, # on which port
87-
sslctx=None, # ssl control
88-
certfile=None, # certificate file
89-
keyfile=None, # key file
90-
password=None, # pass phrase
91-
framer=args.framer, # how to interpret the messages
92-
timeout=1, # waiting time for request to complete
93-
retries=3, # retries per transaction
94-
retry_on_empty=False, # Is an empty response a retry
95-
source_address=("localhost", 0), # bind socket to address
96-
strict=True, # use strict timing, t1.5 for Modbus RTU
102+
"localhost",
103+
port=args.port,
104+
# Common optional paramers:
105+
# protocol_class=None,
106+
# modbus_decoder=ClientDecoder,
107+
framer=args.framer,
108+
# timeout=10,
109+
# retries=3,
110+
# retry_on_empty=False,
111+
# close_comm_on_error=False,
112+
# strict=True,
113+
# TLS setup parameters
114+
# sslctx=None,
115+
# certfile=None,
116+
# keyfile=None,
117+
# password=None,
118+
# server_hostname="localhost",
97119
)
98120
return client, args.comm != "udp"
99121

examples/contrib/asynchronous_asyncio_modbus_tls_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import ssl
1212
import asyncio
1313

14-
from pymodbus.client import AsyncModbusTLSClient
14+
from pymodbus.client import AsyncModbusTlsClient
1515

1616
# -------------------------------------------------------------------------- #
1717
# the TLS detail security can be set in SSLContext which is the context here
@@ -38,7 +38,7 @@ async def start_async_test(client):
3838
# ----------------------------------------------------------------------- #
3939
# pass SSLContext which is the context here to ModbusTcpClient()
4040
# ----------------------------------------------------------------------- #
41-
new_client = AsyncModbusTLSClient( # pylint: disable=unpacking-non-sequence
41+
new_client = AsyncModbusTlsClient( # pylint: disable=unpacking-non-sequence
4242
"test.host.com",
4343
8020,
4444
sslctx=sslctx,

pymodbus/client/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
import external classes, to make them easier to use:
44
"""
5-
from pymodbus.client.async_udp import AsyncModbusUDPClient
6-
from pymodbus.client.async_tls import AsyncModbusTLSClient
75
from pymodbus.client.async_serial import AsyncModbusSerialClient
8-
from pymodbus.client.async_tcp import AsyncModbusTCPClient
6+
from pymodbus.client.async_tcp import AsyncModbusTcpClient
7+
from pymodbus.client.async_tls import AsyncModbusTlsClient
8+
from pymodbus.client.async_udp import AsyncModbusUdpClient
99
from pymodbus.client.sync_serial import ModbusSerialClient
1010
from pymodbus.client.sync_tcp import ModbusTcpClient
1111
from pymodbus.client.sync_tls import ModbusTlsClient
@@ -15,10 +15,10 @@
1515
# Exported symbols
1616
# ---------------------------------------------------------------------------#
1717
__all__ = [
18-
"AsyncModbusUDPClient",
19-
"AsyncModbusTLSClient",
2018
"AsyncModbusSerialClient",
21-
"AsyncModbusTCPClient",
19+
"AsyncModbusTcpClient",
20+
"AsyncModbusTlsClient",
21+
"AsyncModbusUdpClient",
2222
"ModbusSerialClient",
2323
"ModbusTcpClient",
2424
"ModbusTlsClient",

0 commit comments

Comments
 (0)