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
6 changes: 4 additions & 2 deletions examples/common/async_asyncio_client.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# from pymodbus.client.asynchronous.udp import (
# AsyncModbusUDPClient as ModbusClient)
from pymodbus.transaction import ModbusSocketFramer

# ----------------------------------------------------------------------- #
# Import the required asynchronous client
Expand Down Expand Up @@ -131,7 +132,7 @@ def run_with_not_running_loop():
assert not loop.is_running() # nosec
asyncio.set_event_loop(loop)
new_loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
port=5020, loop=loop
port=5020, loop=loop, framer=ModbusSocketFramer
)
loop.run_until_complete(start_async_test(client.protocol))
loop.close()
Expand Down Expand Up @@ -164,6 +165,7 @@ def start_loop(loop):
loop, client = ModbusClient( # pylint: disable=unpacking-non-sequence
port=5020,
loop=loop,
framer=ModbusSocketFramer,
)
future = asyncio.run_coroutine_threadsafe(
start_async_test(client.protocol), loop=loop
Expand All @@ -179,7 +181,7 @@ def start_loop(loop):
def run_with_no_loop():
"""Create a loop."""
_logger.debug("---------------------RUN_WITH_NO_LOOP-----------------")
loop, client = ModbusClient(port=5020) # pylint: disable=unpacking-non-sequence
loop, client = ModbusClient(port=5020, framer=ModbusSocketFramer) # pylint: disable=unpacking-non-sequence
loop.run_until_complete(start_async_test(client.protocol))
loop.close()
_logger.debug("--------DONE RUN_WITH_NO_LOOP-------------")
Expand Down
28 changes: 19 additions & 9 deletions pymodbus/client/asynchronous/async_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from pymodbus.exceptions import ConnectionException
from pymodbus.transaction import FifoTransactionManager
from pymodbus.utilities import hexlify_packets
from pymodbus.transaction import (
ModbusRtuFramer,
ModbusSocketFramer,
ModbusTlsFramer,
)

_logger = logging.getLogger(__name__)

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

def __init__(self, host=None, port=0, **kwargs):
def __init__(self, host=None, port=0, framer=None, **kwargs):
"""Initialize."""
self.host = host
self.port = port
self.framer = framer if framer else ModbusSocketFramer
super().__init__(**kwargs)

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

def __init__(self, protocol_class=None, loop=None, **kwargs):
def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
"""Initialize ReconnectingAsyncioModbusTcpClient.

:param protocol_class: Protocol used to talk to modbus device.
Expand All @@ -235,6 +241,7 @@ def __init__(self, protocol_class=None, loop=None, **kwargs):
self.protocol_class = protocol_class or ModbusClientProtocol
#: Current protocol instance.
self.protocol = None
self.framer = framer if framer else ModbusSocketFramer
#: Event loop to use.
self.loop = loop or asyncio.get_event_loop()
self.host = None
Expand Down Expand Up @@ -334,7 +341,7 @@ async def _reconnect(self):
class AsyncioModbusTcpClient:
"""Client to connect to modbus device over TCP/IP."""

def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
def __init__(self, host=None, port=502, protocol_class=None, loop=None, framer=None, **kwargs):
"""Initialize Asyncio Modbus Tcp Client

:param host: Host IP address
Expand All @@ -346,6 +353,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs
self.protocol_class = protocol_class or ModbusClientProtocol
#: Current protocol instance.
self.protocol = None
self.framer = framer if framer else ModbusSocketFramer
#: Event loop to use.
self.loop = loop or asyncio.get_event_loop()

Expand Down Expand Up @@ -417,11 +425,11 @@ def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
:param protocol_class: Protocol used to talk to modbus device.
:param loop: Event loop to use
"""
self.framer = framer
self.framer = framer if framer else ModbusTlsFramer
self.server_hostname = None
self.sslctx = None
ReconnectingAsyncioModbusTcpClient.__init__(
self, protocol_class, loop, **kwargs
self, protocol_class, loop, framer=self.framer, **kwargs
)

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

def __init__(self, protocol_class=None, loop=None, **kwargs):
def __init__(self, protocol_class=None, loop=None, framer=None, **kwargs):
"""Initialize ReconnectingAsyncioModbusUdpClient

:param protocol_class: Protocol used to talk to modbus device.
Expand All @@ -491,6 +499,7 @@ def __init__(self, protocol_class=None, loop=None, **kwargs):
self.protocol_class = protocol_class or ModbusUdpClientProtocol
#: Current protocol instance.
self.protocol = None
self.framer = framer if framer else ModbusSocketFramer
#: Event loop to use.
self.loop = loop or asyncio.get_event_loop()

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

def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs):
def __init__(self, host=None, port=502, protocol_class=None, loop=None, framer=None, **kwargs):
"""Initialize Asyncio Modbus UDP Client.

:param host: Host IP address
Expand All @@ -610,6 +619,7 @@ def __init__(self, host=None, port=502, protocol_class=None, loop=None, **kwargs
self.protocol_class = protocol_class or ModbusUdpClientProtocol
#: Current protocol instance.
self.protocol = None
self.framer = framer if framer else ModbusSocketFramer
#: Event loop to use.
self.loop = loop or asyncio.get_event_loop()

Expand Down Expand Up @@ -709,7 +719,7 @@ def __init__(
:param loop: Asyncio Event loop
"""
#: Protocol used to talk to modbus device.
self.protocol_class = protocol_class or ModbusClientProtocol
self.protocol_class = protocol_class or ModbusRtuFramer
#: Current protocol instance.
self.protocol = None
#: Event loop to use.
Expand All @@ -719,7 +729,7 @@ def __init__(
self.bytesize = bytesize
self.parity = parity
self.stopbits = stopbits
self.framer = framer
self.framer = framer if framer else ModbusSocketFramer
self._extra_serial_kwargs = serial_kwargs
self._connected_event = asyncio.Event()

Expand Down