1111from pymodbus .utilities import hexlify_packets
1212from pymodbus .exceptions import ConnectionException
1313from pymodbus .factory import ClientDecoder
14- from pymodbus .client .sync_tcp import BaseModbusClient
14+ from pymodbus .client .sync_tcp import BaseOldModbusClient
1515from pymodbus .transaction import ModbusSocketFramer
1616from pymodbus .constants import Defaults
1717
1818_logger = logging .getLogger (__name__ )
1919
2020
21- class BaseAsyncModbusClient (BaseModbusClient ):
22- """This represents the base ModbusAsyncClient."""
23-
24- def __init__ (self , framer = None , timeout = 2 , ** kwargs ):
25- """Initialize framer module
26-
27- :param framer: The framer to use for the protocol. Default:
28- ModbusSocketFramer
29- :type framer: pymodbus.transaction.ModbusSocketFramer
30- """
31- self ._connected = False
32- self ._timeout = timeout
33-
34- super ().__init__ (framer or ModbusSocketFramer (ClientDecoder ()), ** kwargs )
35-
21+ class ModbusClientProtocol (
22+ BaseOldModbusClient ,
23+ asyncio .Protocol ,
24+ asyncio .DatagramProtocol
25+ ):
26+ """Asyncio specific implementation of asynchronous modbus client protocol."""
3627
37- class AsyncModbusClientMixin (BaseAsyncModbusClient ):
38- """Async Modbus client mixing for UDP and TCP clients."""
28+ #: Factory that created this instance.
29+ factory = None
30+ transport = None
31+ use_udp = False
3932
4033 def __init__ (
4134 self ,
@@ -44,6 +37,7 @@ def __init__(
4437 framer = None ,
4538 source_address = None ,
4639 timeout = None ,
40+ use_udp = False ,
4741 ** kwargs
4842 ):
4943 """Initialize a Modbus TCP/UDP asynchronous client
@@ -55,20 +49,15 @@ def __init__(
5549 :param timeout: Timeout in seconds
5650 :param kwargs: Extra arguments
5751 """
58- super ().__init__ (framer = framer , ** kwargs )
52+ self .use_udp = use_udp
53+ self ._connected = False
54+ super ().__init__ (framer or ModbusSocketFramer (ClientDecoder ()), ** kwargs )
55+
5956 self .host = host
6057 self .port = port
6158 self .source_address = source_address or ("" , 0 )
6259 self ._timeout = timeout if timeout is not None else Defaults .Timeout
6360
64-
65- class BaseModbusAsyncClientProtocol (AsyncModbusClientMixin ):
66- """Asyncio specific implementation of asynchronous modbus client protocol."""
67-
68- #: Factory that created this instance.
69- factory = None
70- transport = None
71-
7261 async def execute (self , request = None ): # pylint: disable=invalid-overridden-method
7362 """Execute requests asynchronously.
7463
@@ -93,7 +82,7 @@ def connection_made(self, transport):
9382 self ._connection_made ()
9483
9584 if self .factory :
96- self .factory .protocol_made_connection (self )
85+ self .factory .protocol_made_connection (self ) # pylint: disable=no-member,useless-suppression
9786
9887 def connection_lost (self , reason ):
9988 """Call when the connection is lost or closed.
@@ -106,7 +95,7 @@ def connection_lost(self, reason):
10695 self ._connection_lost (reason )
10796
10897 if self .factory :
109- self .factory .protocol_lost_connection (self )
98+ self .factory .protocol_lost_connection (self ) # pylint: disable=no-member,useless-suppression
11099
111100 def data_received (self , data ):
112101 """Call when some data is received.
@@ -165,6 +154,8 @@ def connected(self):
165154
166155 def write_transport (self , packet ):
167156 """Write transport."""
157+ if self .use_udp :
158+ return self .transport .sendto (packet )
168159 return self .transport .write (packet )
169160
170161 def _execute (self , request , ** kwargs ): # pylint: disable=unused-argument
@@ -218,19 +209,6 @@ def close(self):
218209 self .transport .close ()
219210 self ._connected = False
220211
221-
222- class ModbusClientProtocol (BaseModbusAsyncClientProtocol , asyncio .Protocol ):
223- """Asyncio specific implementation of asynchronous modbus client protocol."""
224-
225- #: Factory that created this instance.
226- factory = None
227- transport = None
228-
229- def data_received (self , data ):
230- """Call when some data is received.
231-
232- data is a non-empty bytes object containing the incoming data.
233-
234- :param data:
235- """
212+ def datagram_received (self , data , addr ):
213+ """Receive datagram."""
236214 self ._data_received (data )
0 commit comments