22
33__all__ = [
44 "DictTransactionManager" ,
5+ "ModbusTransactionManager" ,
56 "ModbusSocketFramer" ,
67 "ModbusTlsFramer" ,
78 "ModbusRtuFramer" ,
1920from pymodbus .exceptions import (
2021 InvalidMessageReceivedException ,
2122 ModbusIOException ,
22- NotImplementedException ,
2323)
2424from pymodbus .framer .ascii_framer import ModbusAsciiFramer
2525from pymodbus .framer .binary_framer import ModbusBinaryFramer
@@ -47,6 +47,8 @@ class ModbusTransactionManager:
4747 while (count < 3)
4848
4949 This module helps to abstract this away from the framer and protocol.
50+
51+ Results are keyed based on the supplied transaction id.
5052 """
5153
5254 def __init__ (self , client , ** kwargs ):
@@ -62,11 +64,19 @@ def __init__(self, client, **kwargs):
6264 self .retry_on_empty = kwargs .get ("retry_on_empty" , False )
6365 self .retry_on_invalid = kwargs .get ("retry_on_invalid" , False )
6466 self .retries = kwargs .get ("retries" , 3 )
67+ self .transactions = {}
6568 self ._transaction_lock = RLock ()
6669 self ._no_response_devices = []
6770 if client :
6871 self ._set_adu_size ()
6972
73+ def __iter__ (self ):
74+ """Iterate over the current managed transactions.
75+
76+ :returns: An iterator of the managed transactions
77+ """
78+ return iter (self .transactions .keys ())
79+
7080 def _set_adu_size (self ):
7181 """Set adu size."""
7282 # base ADU size of modbus frame in bytes
@@ -422,27 +432,33 @@ def addTransaction(self, request, tid=None):
422432
423433 :param request: The request to hold on to
424434 :param tid: The overloaded transaction id to use
425- :raises NotImplementedException:
426435 """
427- raise NotImplementedException ("addTransaction" )
436+ tid = tid if tid is not None else request .transaction_id
437+ Log .debug ("Adding transaction {}" , tid )
438+ self .transactions [tid ] = request
428439
429440 def getTransaction (self , tid ):
430441 """Return a transaction matching the referenced tid.
431442
432443 If the transaction does not exist, None is returned
433444
434445 :param tid: The transaction to retrieve
435- :raises NotImplementedException:
446+
436447 """
437- raise NotImplementedException ("getTransaction" )
448+ Log .debug ("Getting transaction {}" , tid )
449+ if not tid :
450+ if self .transactions :
451+ return self .transactions .popitem ()[1 ]
452+ return None
453+ return self .transactions .pop (tid , None )
438454
439455 def delTransaction (self , tid ):
440456 """Remove a transaction matching the referenced tid.
441457
442458 :param tid: The transaction to remove
443- :raises NotImplementedException:
444459 """
445- raise NotImplementedException ("delTransaction" )
460+ Log .debug ("deleting transaction {}" , tid )
461+ self .transactions .pop (tid , None )
446462
447463 def getNextTID (self ):
448464 """Retrieve the next unique transaction identifier.
@@ -458,64 +474,7 @@ def getNextTID(self):
458474 def reset (self ):
459475 """Reset the transaction identifier."""
460476 self .tid = 0
461- self .transactions = type ( # pylint: disable=attribute-defined-outside-init
462- self .transactions
463- )()
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- """
477477 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
504478
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 )
479+ class DictTransactionManager (ModbusTransactionManager ):
480+ """Old alias for ModbusTransactionManager."""
0 commit comments