Skip to content

Commit 679c495

Browse files
authored
Update async example. (#1031)
1 parent 3d4f6c7 commit 679c495

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

examples/server_sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ def run_server():
147147
# TBD host=
148148
# TBD port=
149149
address=("", port), # listen address
150-
custom_functions=[], # allow custom handling
150+
# custom_functions=[], # allow custom handling
151151
framer=framer, # The framer strategy to use
152152
# TBD handler=None, # handler for each session
153153
allow_reuse_address=True, # allow the reuse of an address
154-
ignore_missing_slaves=True, # ignore request to a missing slave
155-
broadcast_enable=False, # treat unit_id 0 as broadcast address,
154+
# ignore_missing_slaves=True, # ignore request to a missing slave
155+
# broadcast_enable=False, # treat unit_id 0 as broadcast address,
156156
# TBD timeout=1, # waiting time for request to complete
157157
# TBD strict=True, # use strict timing, t1.5 for Modbus RTU
158158
)

pymodbus/client/async_tcp.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def reset_delay(self):
7474
async def aConnect(self):
7575
"""Initiate connection to start client."""
7676
# force reconnect if required:
77-
await self.aClose()
7877
self.loop = asyncio.get_running_loop()
7978

8079
txt = f"Connecting to {self.params.host}:{self.params.port}."
@@ -83,15 +82,15 @@ async def aConnect(self):
8382

8483
async def aClose(self):
8584
"""Stop client."""
86-
# prevent reconnect:
87-
self.params.host = None
88-
8985
if self.connected and self.protocol and self.protocol.transport:
9086
self.protocol.transport.close()
9187

88+
# prevent reconnect.
89+
self.params.host = None
90+
9291
def _create_protocol(self):
9392
"""Create initialized protocol instance with factory function."""
94-
protocol = ModbusClientProtocol(**self.params.kwargs)
93+
protocol = ModbusClientProtocol(framer=self.params.framer, **self.params.kwargs)
9594
protocol.factory = self
9695
return protocol
9796

pymodbus/client/async_udp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ async def aClose(self):
102102

103103
def _create_protocol(self, host=None, port=0):
104104
"""Create initialized protocol instance with factory function."""
105-
protocol = ModbusClientProtocol(use_udp=True, **self.params.kwargs)
105+
protocol = ModbusClientProtocol(
106+
use_udp=True,
107+
framer=self.params.framer,
108+
**self.params.kwargs
109+
)
106110
protocol.params.host = host
107111
protocol.port = port
108112
protocol.factory = self

pymodbus/client/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(
9595
**kwargs
9696
):
9797
"""Initialize a client instance."""
98-
self.params = self._params
98+
self.params = self._params()
9999
self.params.framer = framer
100100
self.params.timeout = timeout
101101
self.params.retries = retries

test/test_client_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_base_modbus_client(self):
118118
self.assertRaises(NotImplementedException, client.connect)
119119
self.assertRaises(NotImplementedException, client.is_socket_open)
120120
self.assertRaises(NotImplementedException, client.close)
121-
self.assertEqual("ModbusBaseClient None:502", str(client))
121+
self.assertEqual("ModbusBaseClient None:None", str(client))
122122

123123
# Test information methods
124124
client.last_frame_end = 2

test/test_examples.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test client async."""
2+
3+
4+
import logging
5+
import pytest
6+
7+
8+
_logger = logging.getLogger()
9+
10+
# ---------------------------------------------------------------------------#
11+
# Fixture
12+
# ---------------------------------------------------------------------------#
13+
14+
15+
@pytest.mark.parametrize(
16+
"sync_server, sync_client",
17+
[
18+
(True, True),
19+
(True, False),
20+
(False, True),
21+
(False, False),
22+
]
23+
)
24+
@pytest.mark.parametrize(
25+
"test_comm, test_framer",
26+
[
27+
("tcp", "socket"),
28+
("tcp", "rtu"),
29+
("tcp", "ascii"),
30+
("tcp", "binary"),
31+
("udp", "socket"),
32+
("udp", "rtu"),
33+
("udp", "ascii"),
34+
("udp", "binary"),
35+
("serial", "rtu"),
36+
("serial", "ascii"),
37+
("serial", "binary"),
38+
# TLS is not automatic testable, without a certificate
39+
]
40+
)
41+
def test_dummy(sync_server, sync_client, test_comm, test_framer):
42+
"""Dummy."""
43+
txt = f"testing: {sync_server}, {sync_client}, {test_comm}, {test_framer}"
44+
_logger.info(txt)
45+
46+
# start server
47+
48+
# run client as:
49+
# client_X.py
50+
# client_X_basic_calls.py
51+
# client_X_extended_calls.py
52+
53+
# stop client
54+
55+
# stop server

0 commit comments

Comments
 (0)