1919from pymodbus .exceptions import (
2020 InvalidMessageReceivedException ,
2121 ModbusIOException ,
22- NotImplementedException ,
2322)
2423from pymodbus .framer .ascii_framer import ModbusAsciiFramer
2524from pymodbus .framer .binary_framer import ModbusBinaryFramer
@@ -47,6 +46,8 @@ class ModbusTransactionManager:
4746 while (count < 3)
4847
4948 This module helps to abstract this away from the framer and protocol.
49+
50+ Results are keyed based on the supplied transaction id.
5051 """
5152
5253 def __init__ (self , client , ** kwargs ):
@@ -62,11 +63,19 @@ def __init__(self, client, **kwargs):
6263 self .retry_on_empty = kwargs .get ("retry_on_empty" , False )
6364 self .retry_on_invalid = kwargs .get ("retry_on_invalid" , False )
6465 self .retries = kwargs .get ("retries" , 3 )
66+ self .transactions = {}
6567 self ._transaction_lock = RLock ()
6668 self ._no_response_devices = []
6769 if client :
6870 self ._set_adu_size ()
6971
72+ def __iter__ (self ):
73+ """Iterate over the current managed transactions.
74+
75+ :returns: An iterator of the managed transactions
76+ """
77+ return iter (self .transactions .keys ())
78+
7079 def _set_adu_size (self ):
7180 """Set adu size."""
7281 # base ADU size of modbus frame in bytes
@@ -422,27 +431,33 @@ def addTransaction(self, request, tid=None):
422431
423432 :param request: The request to hold on to
424433 :param tid: The overloaded transaction id to use
425- :raises NotImplementedException:
426434 """
427- raise NotImplementedException ("addTransaction" )
435+ tid = tid if tid is not None else request .transaction_id
436+ Log .debug ("Adding transaction {}" , tid )
437+ self .transactions [tid ] = request
428438
429439 def getTransaction (self , tid ):
430440 """Return a transaction matching the referenced tid.
431441
432442 If the transaction does not exist, None is returned
433443
434444 :param tid: The transaction to retrieve
435- :raises NotImplementedException:
445+
436446 """
437- raise NotImplementedException ("getTransaction" )
447+ Log .debug ("Getting transaction {}" , tid )
448+ if not tid :
449+ if self .transactions :
450+ return self .transactions .popitem ()[1 ]
451+ return None
452+ return self .transactions .pop (tid , None )
438453
439454 def delTransaction (self , tid ):
440455 """Remove a transaction matching the referenced tid.
441456
442457 :param tid: The transaction to remove
443- :raises NotImplementedException:
444458 """
445- raise NotImplementedException ("delTransaction" )
459+ Log .debug ("deleting transaction {}" , tid )
460+ self .transactions .pop (tid , None )
446461
447462 def getNextTID (self ):
448463 """Retrieve the next unique transaction identifier.
@@ -461,61 +476,3 @@ def reset(self):
461476 self .transactions = type ( # pylint: disable=attribute-defined-outside-init
462477 self .transactions
463478 )()
464-
465-
466- class DictTransactionManager (ModbusTransactionManager ):
467- """Implements a transaction for a manager.
468-
469- Where the results are keyed based on the supplied transaction id.
470- """
471-
472- def __init__ (self , client , ** kwargs ):
473- """Initialize an instance of the ModbusTransactionManager.
474-
475- :param client: The client socket wrapper
476- """
477- self .transactions = {}
478- super ().__init__ (client , ** kwargs )
479-
480- def __iter__ (self ):
481- """Iterate over the current managed transactions.
482-
483- :returns: An iterator of the managed transactions
484- """
485- return iter (self .transactions .keys ())
486-
487- def addTransaction (self , request , tid = None ):
488- """Add a transaction to the handler.
489-
490- This holds the requests in case it needs to be resent.
491- After being sent, the request is removed.
492-
493- :param request: The request to hold on to
494- :param tid: The overloaded transaction id to use
495- """
496- tid = tid if tid is not None else request .transaction_id
497- Log .debug ("Adding transaction {}" , tid )
498- self .transactions [tid ] = request
499-
500- def getTransaction (self , tid ):
501- """Return a transaction matching the referenced tid.
502-
503- If the transaction does not exist, None is returned
504-
505- :param tid: The transaction to retrieve
506-
507- """
508- Log .debug ("Getting transaction {}" , tid )
509- if not tid :
510- if self .transactions :
511- return self .transactions .popitem ()[1 ]
512- return None
513- return self .transactions .pop (tid , None )
514-
515- def delTransaction (self , tid ):
516- """Remove a transaction matching the referenced tid.
517-
518- :param tid: The transaction to remove
519- """
520- Log .debug ("deleting transaction {}" , tid )
521- self .transactions .pop (tid , None )
0 commit comments