88
99from pymodbus .client .mixin import ModbusClientMixin
1010from pymodbus .constants import Defaults
11- from pymodbus .exceptions import ConnectionException , NotImplementedException
11+ from pymodbus .exceptions import ConnectionException
1212from pymodbus .factory import ClientDecoder
1313from pymodbus .framer import ModbusFramer
1414from pymodbus .logging import Log
@@ -95,8 +95,16 @@ def __init__( # pylint: disable=too-many-arguments
9595 ) -> None :
9696 """Initialize a client instance."""
9797 BaseTransport .__init__ (
98- self , "comm" , framer , reconnect_delay , reconnect_delay_max , timeout , timeout
98+ self ,
99+ "comm" ,
100+ (reconnect_delay * 1000 , reconnect_delay_max * 1000 ),
101+ timeout * 1000 ,
102+ framer ,
103+ lambda : None ,
104+ self .cb_base_connection_lost ,
105+ self .cb_base_handle_data ,
99106 )
107+ self .framer = framer
100108 self .params = self ._params ()
101109 self .params .framer = framer
102110 self .params .timeout = float (timeout )
@@ -122,12 +130,11 @@ def __init__( # pylint: disable=too-many-arguments
122130 )
123131 self .reconnect_delay = self .params .reconnect_delay
124132 self .reconnect_delay_current = self .params .reconnect_delay
125- self .use_protocol = False
133+ self .use_sync = False
126134 self .use_udp = False
127135 self .state = ModbusTransactionState .IDLE
128136 self .last_frame_end : float = 0
129137 self .silent_interval : float = 0
130- self ._reconnect_task : asyncio .Task = None
131138
132139 # Initialize mixin
133140 ModbusClientMixin .__init__ (self )
@@ -146,10 +153,6 @@ def register(self, custom_response_class: ModbusResponse) -> None:
146153 """
147154 self .framer .decoder .register (custom_response_class )
148155
149- def is_socket_open (self ) -> bool :
150- """Return whether socket/serial is open or not (call **sync**)."""
151- raise NotImplementedException
152-
153156 def idle_time (self ) -> float :
154157 """Time before initiating next transaction (call **sync**).
155158
@@ -167,13 +170,13 @@ def execute(self, request: ModbusRequest = None) -> ModbusResponse:
167170 :returns: The result of the request execution
168171 :raises ConnectionException: Check exception text.
169172 """
170- if self .use_protocol :
171- if not self .transport :
172- raise ConnectionException (f"Not connected [{ str (self )} ]" )
173- return self .async_execute (request )
174- if not self .connect () :
175- raise ConnectionException (f"Failed to connect [{ str (self )} ]" )
176- return self .transaction . execute (request )
173+ if self .use_sync :
174+ if not self .connect () :
175+ raise ConnectionException (f"Failed to connect [{ str (self )} ]" )
176+ return self .transaction . execute (request )
177+ if not self .transport :
178+ raise ConnectionException (f"Not connected [{ str (self )} ]" )
179+ return self .async_execute (request )
177180
178181 # ----------------------------------------------------------------------- #
179182 # Merged client methods
@@ -198,24 +201,16 @@ async def async_execute(self, request=None):
198201 raise
199202 return resp
200203
201- def data_received (self , data ):
202- """Call when some data is received.
203-
204- data is a non-empty bytes object containing the incoming data.
205- """
206- Log .debug ("recv: {}" , data , ":hex" )
207- self .framer .processIncomingPacket (data , self ._handle_response , slave = 0 )
208-
209- def cb_handle_data (self , _data : bytes ) -> int :
204+ def cb_base_handle_data (self , data : bytes ) -> int :
210205 """Handle received data
211206
212207 returns number of bytes consumed
213208 """
209+ Log .debug ("recv: {}" , data , ":hex" )
210+ self .framer .processIncomingPacket (data , self ._handle_response , slave = 0 )
211+ return len (data )
214212
215- def cb_connection_made (self ) -> None :
216- """Handle new connection"""
217-
218- def cb_connection_lost (self , _reason : Exception ) -> None :
213+ def cb_base_connection_lost (self , _reason : Exception ) -> None :
219214 """Handle lost connection"""
220215 for tid in list (self .transaction ):
221216 self .raise_future (
0 commit comments