Skip to content

Commit 8ee3915

Browse files
authored
Automate examples final. (#1042)
* Correct async test_examples (and base code)
1 parent a39efbb commit 8ee3915

16 files changed

+359
-149
lines changed

examples/client_async.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444

4545

46-
def setup_client(args):
46+
def setup_client(args=None):
4747
"""Run client setup."""
4848
if not args:
4949
args = get_commandline()
@@ -68,8 +68,8 @@ def setup_client(args):
6868
)
6969
elif args.comm == "udp":
7070
client = AsyncModbusUdpClient(
71-
"localhost",
72-
# port=502,
71+
"127.0.0.1",
72+
port=args.port,
7373
# Common optional paramers:
7474
# modbus_decoder=ClientDecoder,
7575
framer=args.framer,
@@ -121,11 +121,11 @@ def setup_client(args):
121121
return client
122122

123123

124-
async def run_client(modbus_calls=None, args=None):
124+
async def run_client(client, modbus_calls=None):
125125
"""Run sync client."""
126-
_logger.info("### Client ready")
127-
client = setup_client(args)
126+
_logger.info("### Client starting")
128127
await client.aConnect()
128+
assert client.protocol
129129
if modbus_calls:
130130
await modbus_calls(client.protocol)
131131
await client.aClose()
@@ -196,4 +196,5 @@ def get_commandline():
196196

197197
if __name__ == "__main__":
198198
# Connect/disconnect no calls.
199-
asyncio.run(run_client())
199+
testclient = setup_client()
200+
asyncio.run(run_client(testclient))

examples/client_async_basic_calls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99
import asyncio
1010

11-
from examples.client_async import _logger, run_client
11+
from examples.client_async import _logger, run_client, setup_client
1212

1313
UNIT = 0x01
1414

@@ -118,5 +118,5 @@ async def demonstrate_calls(client):
118118

119119

120120
if __name__ == "__main__":
121-
# Connect/disconnect no calls.
122-
asyncio.run(run_client(demonstrate_calls))
121+
testclient = setup_client()
122+
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))

examples/client_async_extended_calls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525
import asyncio
2626

27-
from examples.client_async import _logger, run_client
27+
from examples.client_async import _logger, run_client, setup_client
2828

2929
from pymodbus.diag_message import (
3030
ChangeAsciiInputDelimiterRequest,
@@ -57,7 +57,7 @@
5757

5858
async def execute_information_requests(client):
5959
"""Execute extended information requests."""
60-
_logger.info("Running ReadDeviceInformationRequest")
60+
_logger.info("### Running ReadDeviceInformationRequest")
6161
rr = await client.execute(ReadDeviceInformationRequest(unit=UNIT))
6262
assert rr and not rr.isError() # test that calls was OK
6363

@@ -88,7 +88,7 @@ async def execute_information_requests(client):
8888

8989
async def execute_diagnostic_requests(client):
9090
"""Execute extended diagnostic requests."""
91-
_logger.info("Running ReturnQueryDataRequest")
91+
_logger.info("### Running ReturnQueryDataRequest")
9292
rr = await client.execute(ReturnQueryDataRequest(unit=UNIT))
9393
assert rr and not rr.isError() # test that calls was OK
9494
assert not rr.message[0] # test the resulting message
@@ -166,5 +166,5 @@ async def demonstrate_calls(client):
166166

167167

168168
if __name__ == "__main__":
169-
# Connect/disconnect no calls.
170-
asyncio.run(run_client(demonstrate_calls))
169+
testclient = setup_client()
170+
asyncio.run(run_client(testclient, modbus_calls=demonstrate_calls))

examples/client_sync.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
)
4242

4343

44-
def setup_client(args):
44+
def setup_client(args=None):
4545
"""Run client setup."""
4646
if not args:
4747
args = get_commandline()
@@ -118,16 +118,12 @@ def setup_client(args):
118118
return client
119119

120120

121-
def run_client(modbus_calls=None, args=None):
121+
def run_client(client, modbus_calls=None):
122122
"""Run sync client."""
123-
_logger.info("### Client ready")
124-
client = setup_client(args)
125-
try:
126-
client.connect()
127-
if modbus_calls:
128-
modbus_calls(client)
129-
except: # pylint: disable=bare-except # noqa: E722
130-
_logger.error("Got exception in client.")
123+
_logger.info("### Client starting")
124+
client.connect()
125+
if modbus_calls:
126+
modbus_calls(client)
131127
client.close()
132128
_logger.info("### End of Program")
133129

@@ -196,4 +192,5 @@ def get_commandline():
196192

197193
if __name__ == "__main__":
198194
# Connect/disconnect no calls.
199-
run_client()
195+
testclient = setup_client()
196+
run_client(testclient)

examples/client_sync_basic_calls.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
The corresponding server must be started before e.g. as:
77
python3 server_sync.py
88
"""
9-
from examples.client_sync import _logger, run_client
9+
from examples.client_sync import _logger, run_client, setup_client
1010

1111
UNIT = 0x01
1212

@@ -116,4 +116,5 @@ def demonstrate_calls(client):
116116

117117

118118
if __name__ == "__main__":
119-
run_client(demonstrate_calls)
119+
testclient = setup_client()
120+
run_client(testclient, modbus_calls=demonstrate_calls)

examples/client_sync_extended_calls.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
The corresponding server must be started before e.g. as:
2323
python3 server_sync.py
2424
"""
25-
from examples.client_sync import _logger, run_client
25+
from examples.client_sync import _logger, run_client, setup_client
2626

2727
from pymodbus.diag_message import (
2828
ChangeAsciiInputDelimiterRequest,
@@ -55,7 +55,7 @@
5555

5656
def execute_information_requests(client):
5757
"""Execute extended information requests."""
58-
_logger.info("Running ReadDeviceInformationRequest")
58+
_logger.info("### Running ReadDeviceInformationRequest")
5959
rr = client.execute(ReadDeviceInformationRequest(unit=UNIT))
6060
assert rr and not rr.isError() # test that calls was OK
6161

@@ -86,7 +86,7 @@ def execute_information_requests(client):
8686

8787
def execute_diagnostic_requests(client):
8888
"""Execute extended diagnostic requests."""
89-
_logger.info("Running ReturnQueryDataRequest")
89+
_logger.info("### Running ReturnQueryDataRequest")
9090
rr = client.execute(ReturnQueryDataRequest(unit=UNIT))
9191
assert rr and not rr.isError() # test that calls was OK
9292
assert not rr.message[0] # test the resulting message
@@ -164,4 +164,5 @@ def demonstrate_calls(client):
164164

165165

166166
if __name__ == "__main__":
167-
run_client(demonstrate_calls)
167+
testclient = setup_client()
168+
run_client(testclient, modbus_calls=demonstrate_calls)

examples/server_async.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -148,76 +148,76 @@ async def run_server(args=None):
148148

149149
_logger.info("### start server")
150150
if server_id == "tcp":
151-
server = StartTcpServer(
151+
server = await StartTcpServer(
152152
context=store, # Data storage
153153
identity=identity, # server identify
154154
# TBD host=
155155
# TBD port=
156-
address=("", port), # listen address
157-
custom_functions=[], # allow custom handling
156+
address=("127.0.0.1", port), # listen address
157+
# custom_functions=[], # allow custom handling
158158
framer=framer, # The framer strategy to use
159-
handler=None, # handler for each session
159+
# handler=None, # handler for each session
160160
allow_reuse_address=True, # allow the reuse of an address
161-
ignore_missing_slaves=True, # ignore request to a missing slave
162-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
161+
# ignore_missing_slaves=True, # ignore request to a missing slave
162+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
163163
# TBD timeout=1, # waiting time for request to complete
164164
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
165165
defer_start=defer_start # Only define server do not activate
166166
)
167167
elif server_id == "udp":
168-
server = StartUdpServer(
168+
server = await StartUdpServer(
169169
context=store, # Data storage
170170
identity=identity, # server identify
171-
address=("", port), # listen address
172-
custom_functions=[], # allow custom handling
171+
address=("127.0.0.1", port), # listen address
172+
# custom_functions=[], # allow custom handling
173173
framer=framer, # The framer strategy to use
174-
handler=None, # handler for each session
174+
# handler=None, # handler for each session
175175
# TBD allow_reuse_address=True, # allow the reuse of an address
176-
ignore_missing_slaves=True, # ignore request to a missing slave
177-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
176+
# ignore_missing_slaves=True, # ignore request to a missing slave
177+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
178178
# TBD timeout=1, # waiting time for request to complete
179179
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
180180
defer_start=defer_start # Only define server do not activate
181181
)
182182
elif server_id == "serial":
183183
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
184184
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
185-
server = StartSerialServer(
185+
server = await StartSerialServer(
186186
context=store, # Data storage
187187
identity=identity, # server identify
188-
timeout=0.005, # waiting time for request to complete
188+
# timeout=0.005, # waiting time for request to complete
189189
port=port, # serial port
190-
custom_functions=[], # allow custom handling
190+
# custom_functions=[], # allow custom handling
191191
framer=framer, # The framer strategy to use
192-
handler=None, # handler for each session
193-
stopbits=1, # The number of stop bits to use
194-
bytesize=8, # The bytesize of the serial messages
195-
parity="N", # Which kind of parity to use
196-
baudrate=9600, # The baud rate to use for the serial device
197-
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
198-
ignore_missing_slaves=True, # ignore request to a missing slave
199-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
200-
strict=True, # use strict timing, t1.5 for Modbus RTU
192+
# handler=None, # handler for each session
193+
# stopbits=1, # The number of stop bits to use
194+
# bytesize=8, # The bytesize of the serial messages
195+
# parity="N", # Which kind of parity to use
196+
# baudrate=9600, # The baud rate to use for the serial device
197+
# handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
198+
# ignore_missing_slaves=True, # ignore request to a missing slave
199+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
200+
# strict=True, # use strict timing, t1.5 for Modbus RTU
201201
defer_start=defer_start # Only define server do not activate
202202
)
203203
elif server_id == "tls":
204-
server = StartTlsServer(
204+
server = await StartTlsServer(
205205
context=store, # Data storage
206206
host="localhost", # define tcp address where to connect to.
207-
port=port, # on which port
207+
# port=port, # on which port
208208
identity=identity, # server identify
209-
custom_functions=[], # allow custom handling
209+
# custom_functions=[], # allow custom handling
210210
address=("", 5020), # listen address
211211
framer=framer, # The framer strategy to use
212-
handler=None, # handler for each session
212+
# handler=None, # handler for each session
213213
allow_reuse_address=True, # allow the reuse of an address
214-
certfile=None, # The cert file path for TLS (used if sslctx is None)
215-
sslctx=None, # The SSLContext to use for TLS (default None and auto create)
216-
keyfile=None, # The key file path for TLS (used if sslctx is None)
217-
password=None, # The password for for decrypting the private key file
218-
reqclicert=False, # Force the sever request client"s certificate
219-
ignore_missing_slaves=True, # ignore request to a missing slave
220-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
214+
# certfile=None, # The cert file path for TLS (used if sslctx is None)
215+
# sslctx=None, # The SSLContext to use for TLS (default None and auto create)
216+
# keyfile=None, # The key file path for TLS (used if sslctx is None)
217+
# password=None, # The password for for decrypting the private key file
218+
# reqclicert=False, # Force the sever request client"s certificate
219+
# ignore_missing_slaves=True, # ignore request to a missing slave
220+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
221221
# TBD timeout=1, # waiting time for request to complete
222222
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
223223
defer_start=defer_start # Only define server do not activate

examples/server_sync.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,39 +186,39 @@ def run_server(args=None):
186186
server = StartSerialServer(
187187
context=store, # Data storage
188188
identity=identity, # server identify
189-
timeout=0.005, # waiting time for request to complete
189+
# timeout=0.005, # waiting time for request to complete
190190
port=port, # serial port
191-
custom_functions=[], # allow custom handling
191+
# custom_functions=[], # allow custom handling
192192
framer=framer, # The framer strategy to use
193-
handler=None, # handler for each session
194-
stopbits=1, # The number of stop bits to use
195-
bytesize=7, # The bytesize of the serial messages
196-
parity="E", # Which kind of parity to use
197-
baudrate=9600, # The baud rate to use for the serial device
198-
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
199-
ignore_missing_slaves=True, # ignore request to a missing slave
200-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
201-
strict=True, # use strict timing, t1.5 for Modbus RTU
193+
# handler=None, # handler for each session
194+
# stopbits=1, # The number of stop bits to use
195+
# bytesize=7, # The bytesize of the serial messages
196+
# parity="E", # Which kind of parity to use
197+
# baudrate=9600, # The baud rate to use for the serial device
198+
# handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
199+
# ignore_missing_slaves=True, # ignore request to a missing slave
200+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
201+
# strict=True, # use strict timing, t1.5 for Modbus RTU
202202
defer_start=defer_start # Only define server do not activate
203203
)
204204
elif server_id == "tls":
205205
server = StartTlsServer(
206206
context=store, # Data storage
207207
host="localhost", # define tcp address where to connect to.
208-
port=port, # on which port
208+
# port=port, # on which port
209209
identity=identity, # server identify
210-
custom_functions=[], # allow custom handling
210+
# custom_functions=[], # allow custom handling
211211
address=("", 5020), # listen address
212212
framer=framer, # The framer strategy to use
213-
handler=None, # handler for each session
214-
allow_reuse_address=True, # allow the reuse of an address
215-
certfile=None, # The cert file path for TLS (used if sslctx is None)
216-
sslctx=None, # The SSLContext to use for TLS (default None and auto create)
217-
keyfile=None, # The key file path for TLS (used if sslctx is None)
218-
password=None, # The password for for decrypting the private key file
219-
reqclicert=False, # Force the sever request client"s certificate
220-
ignore_missing_slaves=True, # ignore request to a missing slave
221-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
213+
# handler=None, # handler for each session
214+
# allow_reuse_address=True, # allow the reuse of an address
215+
# certfile=None, # The cert file path for TLS (used if sslctx is None)
216+
# sslctx=None, # The SSLContext to use for TLS (default None and auto create)
217+
# keyfile=None, # The key file path for TLS (used if sslctx is None)
218+
# password=None, # The password for for decrypting the private key file
219+
# reqclicert=False, # Force the sever request client"s certificate
220+
# ignore_missing_slaves=True, # ignore request to a missing slave
221+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
222222
# TBD timeout=1, # waiting time for request to complete
223223
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
224224
defer_start=defer_start # Only define server do not activate

pymodbus/client/async_tcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class AsyncModbusTcpClient(ModbusBaseClient):
3939
4040
:param host: (positional) Host IP address
4141
:param port: (optional default 502) The TCP port used for communication.
42+
:param framer: (optional, default ModbusSocketFramer) Framer class.
4243
:param source_address: (optional, default none) source address of client,
4344
:param \*\*kwargs: (optional) Extra experimental parameters for transport
4445
:return: client object

0 commit comments

Comments
 (0)