@@ -163,7 +163,7 @@ async def async_execute(self, request=None):
163163 resp = b"Broadcast write sent - no response expected"
164164 break
165165 try :
166- req = self ._build_response (request .transaction_id )
166+ req = self .build_response (request .transaction_id )
167167 resp = await asyncio .wait_for (
168168 req , timeout = self .comm_params .timeout_connect
169169 )
@@ -186,14 +186,6 @@ def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
186186 self .framer .processIncomingPacket (data , self ._handle_response , slave = 0 )
187187 return len (data )
188188
189- def callback_disconnected (self , _reason : Exception | None ) -> None :
190- """Handle lost connection."""
191- for tid in list (self .transaction ):
192- self .raise_future (
193- self .transaction .getTransaction (tid ),
194- ConnectionException ("Connection lost during request" ),
195- )
196-
197189 async def connect (self ):
198190 """Connect to the modbus remote host."""
199191
@@ -212,7 +204,7 @@ def _handle_response(self, reply, **_kwargs):
212204 else :
213205 Log .debug ("Unrequested message: {}" , reply , ":str" )
214206
215- def _build_response (self , tid ):
207+ def build_response (self , tid ):
216208 """Return a deferred response for the current request."""
217209 my_future = asyncio .Future ()
218210 if not self .transport :
@@ -229,26 +221,12 @@ def send(self, request):
229221
230222 :meta private:
231223 """
232- if self .state != ModbusTransactionState .RETRYING :
233- Log .debug ('New Transaction state "SENDING"' )
234- self .state = ModbusTransactionState .SENDING
235- return request
236224
237225 def recv (self , size ):
238226 """Receive data.
239227
240228 :meta private:
241229 """
242- return size
243-
244- @classmethod
245- def _get_address_family (cls , address ):
246- """Get the correct address family."""
247- try :
248- _ = socket .inet_pton (socket .AF_INET6 , address )
249- except OSError : # not a valid ipv6 address
250- return socket .AF_INET
251- return socket .AF_INET6
252230
253231 # ----------------------------------------------------------------------- #
254232 # The magic methods
@@ -259,8 +237,7 @@ def __enter__(self):
259237 :returns: The current instance of the client
260238 :raises ConnectionException:
261239 """
262- if not self .connect ():
263- raise ConnectionException (f"Failed to connect[{ self .__str__ ()} ]" )
240+ self .connect ()
264241 return self
265242
266243 async def __aenter__ (self ):
@@ -269,8 +246,7 @@ async def __aenter__(self):
269246 :returns: The current instance of the client
270247 :raises ConnectionException:
271248 """
272- if not await self .connect ():
273- raise ConnectionException (f"Failed to connect[{ self .__str__ ()} ]" )
249+ await self .connect ()
274250 return self
275251
276252 def __exit__ (self , klass , value , traceback ):
@@ -400,11 +376,6 @@ def __init__( # pylint: disable=too-many-arguments
400376 # ----------------------------------------------------------------------- #
401377 # Client external interface
402378 # ----------------------------------------------------------------------- #
403- @property
404- def connected (self ) -> bool :
405- """Return state of connection."""
406- return self .is_active ()
407-
408379 def register (self , custom_response_class : ModbusResponse ) -> None :
409380 """Register a custom response class with the decoder (call **sync**).
410381
@@ -416,13 +387,6 @@ def register(self, custom_response_class: ModbusResponse) -> None:
416387 """
417388 self .framer .decoder .register (custom_response_class )
418389
419- def close (self , reconnect : bool = False ) -> None :
420- """Close connection."""
421- if reconnect :
422- self .connection_lost (asyncio .TimeoutError ("Server not responding" ))
423- else :
424- self .transport_close ()
425-
426390 def idle_time (self ) -> float :
427391 """Time before initiating next transaction (call **sync**).
428392
@@ -444,80 +408,6 @@ def execute(self, request: ModbusRequest | None = None) -> ModbusResponse:
444408 raise ConnectionException (f"Failed to connect[{ self !s} ]" )
445409 return self .transaction .execute (request )
446410
447- # ----------------------------------------------------------------------- #
448- # Merged client methods
449- # ----------------------------------------------------------------------- #
450- async def async_execute (self , request = None ):
451- """Execute requests asynchronously."""
452- request .transaction_id = self .transaction .getNextTID ()
453- packet = self .framer .buildPacket (request )
454-
455- count = 0
456- while count <= self .params .retries :
457- if not count or not self .no_resend_on_retry :
458- self .transport_send (packet )
459- if self .params .broadcast_enable and not request .slave_id :
460- resp = b"Broadcast write sent - no response expected"
461- break
462- try :
463- req = self ._build_response (request .transaction_id )
464- resp = await asyncio .wait_for (
465- req , timeout = self .comm_params .timeout_connect
466- )
467- break
468- except asyncio .exceptions .TimeoutError :
469- count += 1
470- if count > self .params .retries :
471- self .close (reconnect = True )
472- raise ModbusIOException (
473- f"ERROR: No response received after { self .params .retries } retries"
474- )
475-
476- return resp
477-
478- def callback_data (self , data : bytes , addr : tuple | None = None ) -> int :
479- """Handle received data.
480-
481- returns number of bytes consumed
482- """
483- self .framer .processIncomingPacket (data , self ._handle_response , slave = 0 )
484- return len (data )
485-
486- def callback_disconnected (self , _reason : Exception | None ) -> None :
487- """Handle lost connection."""
488- for tid in list (self .transaction ):
489- self .raise_future (
490- self .transaction .getTransaction (tid ),
491- ConnectionException ("Connection lost during request" ),
492- )
493-
494- async def connect (self ):
495- """Connect to the modbus remote host."""
496-
497- def raise_future (self , my_future , exc ):
498- """Set exception of a future if not done."""
499- if not my_future .done ():
500- my_future .set_exception (exc )
501-
502- def _handle_response (self , reply , ** _kwargs ):
503- """Handle the processed response and link to correct deferred."""
504- if reply is not None :
505- tid = reply .transaction_id
506- if handler := self .transaction .getTransaction (tid ):
507- if not handler .done ():
508- handler .set_result (reply )
509- else :
510- Log .debug ("Unrequested message: {}" , reply , ":str" )
511-
512- def _build_response (self , tid ):
513- """Return a deferred response for the current request."""
514- my_future = asyncio .Future ()
515- if not self .transport :
516- self .raise_future (my_future , ConnectionException ("Client is not connected" ))
517- else :
518- self .transaction .addTransaction (my_future , tid )
519- return my_future
520-
521411 # ----------------------------------------------------------------------- #
522412 # Internal methods
523413 # ----------------------------------------------------------------------- #
@@ -539,14 +429,20 @@ def recv(self, size):
539429 return size
540430
541431 @classmethod
542- def _get_address_family (cls , address ):
432+ def get_address_family (cls , address ):
543433 """Get the correct address family."""
544434 try :
545435 _ = socket .inet_pton (socket .AF_INET6 , address )
546436 except OSError : # not a valid ipv6 address
547437 return socket .AF_INET
548438 return socket .AF_INET6
549439
440+ def connect (self ):
441+ """Connect to other end, overwritten."""
442+
443+ def close (self ):
444+ """Close connection, overwritten."""
445+
550446 # ----------------------------------------------------------------------- #
551447 # The magic methods
552448 # ----------------------------------------------------------------------- #
@@ -556,28 +452,13 @@ def __enter__(self):
556452 :returns: The current instance of the client
557453 :raises ConnectionException:
558454 """
559- if not self .connect ():
560- raise ConnectionException (f"Failed to connect[{ self .__str__ ()} ]" )
561- return self
562-
563- async def __aenter__ (self ):
564- """Implement the client with enter block.
565-
566- :returns: The current instance of the client
567- :raises ConnectionException:
568- """
569- if not await self .connect ():
570- raise ConnectionException (f"Failed to connect[{ self .__str__ ()} ]" )
455+ self .connect ()
571456 return self
572457
573458 def __exit__ (self , klass , value , traceback ):
574459 """Implement the client with exit block."""
575460 self .close ()
576461
577- async def __aexit__ (self , klass , value , traceback ):
578- """Implement the client with exit block."""
579- self .close ()
580-
581462 def __str__ (self ):
582463 """Build a string representation of the connection.
583464
0 commit comments