Skip to content

Commit c19b503

Browse files
authored
Client async corrections (due to 3.1.2) (#1565)
1 parent 3fc5d37 commit c19b503

File tree

5 files changed

+46
-30
lines changed

5 files changed

+46
-30
lines changed

examples/client_async.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ async def run_async_client(client, modbus_calls=None):
121121
"""Run sync client."""
122122
_logger.info("### Client starting")
123123
await client.connect()
124+
print("jan " + str(client.connected))
124125
assert client.connected
125126
if modbus_calls:
126127
await modbus_calls(client)
127128
client.close()
128129
_logger.info("### End of Program")
129130

130131

131-
if __name__ == "__main__":
132+
async def helper():
133+
"""Combine the setup and run"""
132134
testclient = setup_async_client(description="Run asynchronous client.")
133-
asyncio.run(run_async_client(testclient), debug=True)
135+
await run_async_client(testclient)
136+
137+
138+
if __name__ == "__main__":
139+
asyncio.run(helper(), debug=True)

examples/client_calls.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ async def _handle_holding_registers(client):
177177
"read_address": 1,
178178
"read_count": 8,
179179
"write_address": 1,
180-
"write_registers": [256, 128, 100, 50, 25, 10, 5, 1],
180+
"values": [256, 128, 100, 50, 25, 10, 5, 1],
181181
}
182182
_check_call(await client.readwrite_registers(slave=SLAVE, **arguments))
183183
rr = _check_call(await client.read_holding_registers(1, 8, slave=SLAVE))
184-
assert rr.registers == arguments["write_registers"]
184+
assert rr.registers == arguments["values"]
185185

186186

187187
async def _handle_input_registers(client):
@@ -289,11 +289,14 @@ def run_sync_calls(client):
289289
template_call(client)
290290

291291

292+
async def helper():
293+
"""Combine the setup and run"""
294+
testclient = setup_async_client(description="Run asynchronous client.")
295+
await run_async_client(testclient, modbus_calls=run_async_calls)
296+
297+
292298
if __name__ == "__main__":
293-
testclient = setup_async_client(
294-
description="Run modbus calls in asynchronous client."
295-
)
296-
asyncio.run(run_async_client(testclient, modbus_calls=run_async_calls))
299+
asyncio.run(helper())
297300
testclient = setup_sync_client(
298301
description="Run modbus calls in synchronous client."
299302
)

examples/client_payload.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ async def run_payload_calls(client):
136136
print("\n")
137137

138138

139+
async def helper():
140+
"""Combine the setup and run"""
141+
testclient = setup_async_client(description="Run asynchronous client.")
142+
await run_async_client(testclient, modbus_calls=run_payload_calls)
143+
144+
139145
if __name__ == "__main__":
140-
testclient = setup_async_client(description="Run payload client.")
141-
asyncio.run(run_async_client(testclient, modbus_calls=run_payload_calls))
146+
asyncio.run(helper())

pymodbus/transport/transport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import asyncio
55
from abc import abstractmethod
6+
from contextlib import suppress
67

78
from pymodbus.framer import ModbusFramer
89
from pymodbus.logging import Log
@@ -44,7 +45,8 @@ def __init__(
4445
# properties, can be read, but may not be mingled with
4546
self.reconnect_delay_current = self.reconnect_delay
4647
self.transport: asyncio.BaseTransport = None
47-
self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
48+
with suppress(RuntimeError):
49+
self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
4850
self.reconnect_timer: asyncio.TimerHandle = None
4951
self.recv_buffer: bytes = b""
5052

test/transport/test_basic.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self):
2525
self.close = mock.MagicMock()
2626

2727
@classmethod
28-
def setup_BaseTransport(self):
28+
async def setup_BaseTransport(self):
2929
"""Create base object."""
3030
base = BaseTransport(
3131
self.base_comm_name,
@@ -40,9 +40,9 @@ def setup_BaseTransport(self):
4040
base.cb_handle_data = mock.MagicMock()
4141
return base
4242

43-
def test_properties(self):
43+
async def test_properties(self):
4444
"""Test properties."""
45-
base = self.setup_BaseTransport()
45+
base = await self.setup_BaseTransport()
4646
assert self.base_comm_name == base.comm_name
4747
assert self.base_framer == base.framer
4848
assert self.base_reconnect_delay == base.reconnect_delay
@@ -53,16 +53,16 @@ def test_properties(self):
5353

5454
async def test_magic(self):
5555
"""Test properties."""
56-
base = self.setup_BaseTransport()
56+
base = await self.setup_BaseTransport()
5757
base.close = mock.MagicMock()
5858
async with base:
5959
pass
6060
base.close.assert_called_once()
6161
assert str(base) == f"BaseTransport({self.base_comm_name})"
6262

63-
def test_connection_made(self):
63+
async def test_connection_made(self):
6464
"""Test properties."""
65-
base = self.setup_BaseTransport()
65+
base = await self.setup_BaseTransport()
6666
transport = self.dummy_transport()
6767
base.connection_made(transport)
6868
assert base.transport == transport
@@ -73,9 +73,9 @@ def test_connection_made(self):
7373
base.cb_handle_data.assert_not_called()
7474
base.close()
7575

76-
def test_connection_lost(self):
76+
async def test_connection_lost(self):
7777
"""Test properties."""
78-
base = self.setup_BaseTransport()
78+
base = await self.setup_BaseTransport()
7979
transport = self.dummy_transport()
8080
base.connection_lost(transport)
8181
assert not base.transport
@@ -86,9 +86,9 @@ def test_connection_lost(self):
8686
base.cb_handle_data.assert_not_called()
8787
base.close()
8888

89-
def test_close_simple(self):
89+
async def test_close_simple(self):
9090
"""Test properties."""
91-
base = self.setup_BaseTransport()
91+
base = await self.setup_BaseTransport()
9292
transport = self.dummy_transport()
9393
base.connection_made(transport)
9494
base.cb_connection_made.reset_mock()
@@ -105,9 +105,9 @@ def test_close_simple(self):
105105
assert not base.recv_buffer
106106
assert not base.reconnect_timer
107107

108-
def test_close_reconnect(self):
108+
async def test_close_reconnect(self):
109109
"""Test properties."""
110-
base = self.setup_BaseTransport()
110+
base = await self.setup_BaseTransport()
111111
transport = self.dummy_transport()
112112
base.connection_made(transport)
113113
base.reconnect_timer = None
@@ -116,16 +116,16 @@ def test_close_reconnect(self):
116116
base.close()
117117
assert not base.reconnect_timer
118118

119-
def test_reset_delay(self):
119+
async def test_reset_delay(self):
120120
"""Test properties."""
121-
base = self.setup_BaseTransport()
121+
base = await self.setup_BaseTransport()
122122
base.reconnect_delay_current = self.base_reconnect_delay + 1
123123
base.reset_delay()
124124
assert base.reconnect_delay_current == self.base_reconnect_delay
125125

126126
async def test_connect(self):
127127
"""Test properties."""
128-
base = self.setup_BaseTransport()
128+
base = await self.setup_BaseTransport()
129129
base.reconnect_delay_current = self.base_reconnect_delay + 1
130130
base.close(reconnect=True)
131131
base.complete_connect()
@@ -139,7 +139,7 @@ async def test_connect(self):
139139

140140
async def test_reconnect(self):
141141
"""Test properties."""
142-
base = self.setup_BaseTransport()
142+
base = await self.setup_BaseTransport()
143143
transport = self.dummy_transport()
144144
base.connection_made(transport)
145145
base.connect = mock.MagicMock()
@@ -151,15 +151,15 @@ async def test_reconnect(self):
151151

152152
async def test_datagram(self):
153153
"""Test properties."""
154-
base = self.setup_BaseTransport()
154+
base = await self.setup_BaseTransport()
155155
base.data_received = mock.MagicMock()
156156
base.datagram_received(b"abc", "127.0.0.1")
157157
base.data_received.assert_called_once()
158158
base.close()
159159

160160
async def test_receive(self):
161161
"""Test properties."""
162-
base = self.setup_BaseTransport()
162+
base = await self.setup_BaseTransport()
163163
base.cb_handle_data = mock.MagicMock(return_value=2)
164164
base.data_received(b"123456")
165165
base.cb_handle_data.assert_called_once()
@@ -170,6 +170,6 @@ async def test_receive(self):
170170

171171
async def test_send(self):
172172
"""Test properties."""
173-
base = self.setup_BaseTransport()
173+
base = await self.setup_BaseTransport()
174174
await base.send(b"abc")
175175
base.close()

0 commit comments

Comments
 (0)