Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"""
import argparse
import asyncio
from threading import Thread
import logging


Expand All @@ -42,7 +41,7 @@
)


async def setup_async_client(loop):
async def setup_async_client():
"""Run client setup."""
args = get_commandline()
_logger.info("### Create client object")
Expand All @@ -57,7 +56,6 @@ async def setup_async_client(loop):
retry_on_empty=False, # Is an empty response a retry
source_address=("localhost", 0), # bind socket to address
strict=True, # use strict timing, t1.5 for Modbus RTU
loop=loop,
)
elif args.comm == "udp":
client = await AsyncModbusUDPClient(
Expand All @@ -69,7 +67,6 @@ async def setup_async_client(loop):
retry_on_empty=False, # Is an empty response a retry
source_address=("localhost", 0), # bind socket to address
strict=True, # use strict timing, t1.5 for Modbus RTU
loop=loop,
)
elif args.comm == "serial":
client = await AsyncModbusSerialClient(
Expand All @@ -82,7 +79,6 @@ async def setup_async_client(loop):
handle_local_echo=False, # Handle local echo of the USB-to-RS485 adaptor
timeout=1, # waiting time for request to complete
strict=True, # use strict timing, t1.5 for Modbus RTU
loop=loop,
)
elif args.comm == "tls":
client = await AsyncModbusTLSClient(
Expand All @@ -98,44 +94,16 @@ async def setup_async_client(loop):
retry_on_empty=False, # Is an empty response a retry
source_address=("localhost", 0), # bind socket to address
strict=True, # use strict timing, t1.5 for Modbus RTU
loop=loop,
)
return client


async def run_async_client(modbus_calls=None):
"""Run sync client."""
_logger.info("### Client ready")

def done(future): # pylint: disable=unused-argument
"""Done."""
_logger.info("Done !!!")

def start_loop(loop):
"""Start Loop"""
asyncio.set_event_loop(loop)
loop.run_forever()

loop = asyncio.new_event_loop()
mythread = Thread(target=start_loop, args=[loop])
mythread.daemon = True
# Start the loop
mythread.start()
await asyncio.sleep(1)
assert loop.is_running() # nosec
asyncio.set_event_loop(loop)

client = await setup_async_client(loop)

# Run supplied modbus calls
client = await setup_async_client()
if modbus_calls:
future = asyncio.run_coroutine_threadsafe(
modbus_calls(client.protocol), loop=loop
)
future.add_done_callback(done)
while not future.done():
await asyncio.sleep(0.1)
loop.stop()
await modbus_calls(client.protocol)
_logger.info("### End of Program")


Expand Down
2 changes: 1 addition & 1 deletion pymodbus/client/asynchronous/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient as Client

# For asynchronous client use
event_loop, client = Client(port=5020)
client = Client(port=5020)
"""
12 changes: 5 additions & 7 deletions test/test_client_async_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,11 @@ async def test_factory_start_success(self):
async def test_factory_start_failing_and_retried(self, mock_async): # pylint: disable=unused-argument
"""Test factory start failing and retried."""
mock_protocol_class = mock.MagicMock()
mock_loop = mock.MagicMock()
mock_loop.create_connection = mock.MagicMock(
loop = asyncio.get_running_loop()
loop.create_connection = mock.MagicMock(
side_effect=Exception("Did not work.")
)
client = ReconnectingAsyncioModbusTcpClient(
protocol_class=mock_protocol_class, loop=mock_loop
)
client = ReconnectingAsyncioModbusTcpClient(protocol_class=mock_protocol_class)

# check whether reconnect is called upon failed connection attempt:
with mock.patch(
Expand All @@ -226,10 +224,10 @@ async def test_factory_reconnect(self, mock_sleep):
"""Test factory reconnect."""
mock_protocol_class = mock.MagicMock()
mock_sleep.side_effect = return_as_coroutine()
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
loop.create_connection = mock.MagicMock(return_value=None)
client = ReconnectingAsyncioModbusTcpClient(
protocol_class=mock_protocol_class, loop=loop
protocol_class=mock_protocol_class
)
client.delay_ms = 5000

Expand Down