Skip to content

Commit 20af8d3

Browse files
authored
Async client framer= not working. (#998)
1 parent 033a00d commit 20af8d3

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

examples/common/async_asyncio_client.py

100644100755
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# from pymodbus.client.asynchronous.udp import (
1515
# AsyncModbusUDPClient as ModbusClient)
16+
from pymodbus.transaction import ModbusSocketFramer
1617

1718
# ----------------------------------------------------------------------- #
1819
# Import the required asynchronous client
@@ -131,7 +132,7 @@ def run_with_not_running_loop():
131132
assert not loop.is_running() # nosec
132133
asyncio.set_event_loop(loop)
133134
new_loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
134-
port=5020, loop=loop
135+
port=5020, loop=loop, framer=ModbusSocketFramer
135136
)
136137
loop.run_until_complete(start_async_test(client.protocol))
137138
loop.close()
@@ -164,6 +165,7 @@ def start_loop(loop):
164165
loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
165166
port=5020,
166167
loop=loop,
168+
framer=ModbusSocketFramer,
167169
)
168170
future = asyncio.run_coroutine_threadsafe(
169171
start_async_test(client.protocol), loop=loop
@@ -179,7 +181,7 @@ def start_loop(loop):
179181
def run_with_no_loop():
180182
"""Create a loop."""
181183
_logger.debug("---------------------RUN_WITH_NO_LOOP-----------------")
182-
loop, client = ModbusClient(port=5020) # pylint: disable=unpacking-non-sequence
184+
loop, client = ModbusClient(port=5020, framer=ModbusSocketFramer) # pylint: disable=unpacking-non-sequence
183185
loop.run_until_complete(start_async_test(client.protocol))
184186
loop.close()
185187
_logger.debug("--------DONE RUN_WITH_NO_LOOP-------------")

pymodbus/client/asynchronous/async_io/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from pymodbus.exceptions import ConnectionException
1313
from pymodbus.transaction import FifoTransactionManager
1414
from pymodbus.utilities import hexlify_packets
15+
from pymodbus.transaction import (
16+
ModbusRtuFramer,
17+
ModbusSocketFramer,
18+
ModbusTlsFramer,
19+
)
1520

1621
_logger = logging.getLogger(__name__)
1722

@@ -202,10 +207,11 @@ class ModbusUdpClientProtocol(BaseModbusAsyncClientProtocol, asyncio.DatagramPro
202207
#: Factory that created this instance.
203208
factory = None
204209

205-
def __init__(self, host=None, port=0, **kwargs):
210+
def __init__(self, host=None, port=0, framer=None, **kwargs):
206211
"""Initialize."""
207212
self.host = host
208213
self.port = port
214+
self.framer = framer if framer else ModbusSocketFramer
209215
super().__init__(**kwargs)
210216

211217
def datagram_received(self, data, addr):
@@ -225,7 +231,7 @@ class ReconnectingAsyncioModbusTcpClient:
225231
#: Maximum delay in milli seconds before reconnect is attempted.
226232
DELAY_MAX_MS = 1000 * 60 * 5
227233

228-
def __init__(self, protocol_class=None, loop=None, **kwargs):
234+
def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
229235
"""Initialize ReconnectingAsyncioModbusTcpClient.
230236
231237
:param protocol_class: Protocol used to talk to modbus device.
@@ -235,6 +241,7 @@ def __init__(self, protocol_class=None, loop=None, **kwargs):
235241
self.protocol_class = protocol_class or ModbusClientProtocol
236242
#: Current protocol instance.
237243
self.protocol = None
244+
self.framer = framer if framer else ModbusSocketFramer
238245
#: Event loop to use.
239246
self.loop = loop or asyncio.get_event_loop()
240247
self.host = None
@@ -334,7 +341,7 @@ async def _reconnect(self):
334341
class AsyncioModbusTcpClient:
335342
"""Client to connect to modbus device over TCP/IP."""
336343

337-
def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
344+
def __init__(self, host=None, port=502, protocol_class=None, loop=None, framer=None, **kwargs):
338345
"""Initialize Asyncio Modbus Tcp Client
339346
340347
:param host: Host IP address
@@ -346,6 +353,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs
346353
self.protocol_class = protocol_class or ModbusClientProtocol
347354
#: Current protocol instance.
348355
self.protocol = None
356+
self.framer = framer if framer else ModbusSocketFramer
349357
#: Event loop to use.
350358
self.loop = loop or asyncio.get_event_loop()
351359

@@ -417,11 +425,11 @@ def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
417425
:param protocol_class: Protocol used to talk to modbus device.
418426
:param loop: Event loop to use
419427
"""
420-
self.framer = framer
428+
self.framer = framer if framer else ModbusTlsFramer
421429
self.server_hostname = None
422430
self.sslctx = None
423431
ReconnectingAsyncioModbusTcpClient.__init__(
424-
self, protocol_class, loop, **kwargs
432+
self, protocol_class, loop, framer=self.framer, **kwargs
425433
)
426434

427435
async def start(self, host, port=802, sslctx=None, server_hostname=None):
@@ -481,7 +489,7 @@ class ReconnectingAsyncioModbusUdpClient:
481489
#: Maximum delay in milli seconds before reconnect is attempted.
482490
DELAY_MAX_MS = 1000 * 60 * 5
483491

484-
def __init__(self, protocol_class=None, loop=None, **kwargs):
492+
def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
485493
"""Initialize ReconnectingAsyncioModbusUdpClient
486494
487495
:param protocol_class: Protocol used to talk to modbus device.
@@ -491,6 +499,7 @@ def __init__(self, protocol_class=None, loop=None, **kwargs):
491499
self.protocol_class = protocol_class or ModbusUdpClientProtocol
492500
#: Current protocol instance.
493501
self.protocol = None
502+
self.framer = framer if framer else ModbusSocketFramer
494503
#: Event loop to use.
495504
self.loop = loop or asyncio.get_event_loop()
496505

@@ -598,7 +607,7 @@ async def _reconnect(self):
598607
class AsyncioModbusUdpClient:
599608
"""Client to connect to modbus device over UDP."""
600609

601-
def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
610+
def __init__(self, host=None, port=502, protocol_class=None, loop=None, framer=None, **kwargs):
602611
"""Initialize Asyncio Modbus UDP Client.
603612
604613
:param host: Host IP address
@@ -610,6 +619,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs
610619
self.protocol_class = protocol_class or ModbusUdpClientProtocol
611620
#: Current protocol instance.
612621
self.protocol = None
622+
self.framer = framer if framer else ModbusSocketFramer
613623
#: Event loop to use.
614624
self.loop = loop or asyncio.get_event_loop()
615625

@@ -709,7 +719,7 @@ def __init__(
709719
:param loop: Asyncio Event loop
710720
"""
711721
#: Protocol used to talk to modbus device.
712-
self.protocol_class = protocol_class or ModbusClientProtocol
722+
self.protocol_class = protocol_class or ModbusRtuFramer
713723
#: Current protocol instance.
714724
self.protocol = None
715725
#: Event loop to use.
@@ -719,7 +729,7 @@ def __init__(
719729
self.bytesize = bytesize
720730
self.parity = parity
721731
self.stopbits = stopbits
722-
self.framer = framer
732+
self.framer = framer if framer else ModbusSocketFramer
723733
self._extra_serial_kwargs = serial_kwargs
724734
self._connected_event = asyncio.Event()
725735

0 commit comments

Comments
 (0)