diff --git a/pymodbus/framer/base.py b/pymodbus/framer/base.py index e0d8a5690..f7af67d5b 100644 --- a/pymodbus/framer/base.py +++ b/pymodbus/framer/base.py @@ -56,6 +56,17 @@ def encode(self, payload: bytes, _dev_id: int, _tid: int) -> bytes: """ return payload + def next_transaction_id(self, transaction_id: int) -> int: + """Return the transaction id to use for the next request. + + The default frame does not carry a transaction id, so this returns 0. + + :param transaction_id: transaction id of the previous request + :returns: 0 + """ + _ = transaction_id + return 0 + def buildFrame(self, message: ModbusPDU) -> bytes: """Create a ready to send modbus packet. diff --git a/pymodbus/framer/socket.py b/pymodbus/framer/socket.py index 9bd78b974..098003146 100644 --- a/pymodbus/framer/socket.py +++ b/pymodbus/framer/socket.py @@ -38,6 +38,16 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: msg_len = 9 return msg_len, dev_id, tid, data[7:msg_len] + def next_transaction_id(self, transaction_id: int) -> int: + """Return the transaction id to use for the next request. + + :param transaction_id: transaction id of the previous request + :returns: transaction id 1..65000, wrapping around to 1 + """ + if transaction_id >= 65000: + return 1 + return transaction_id + 1 + def encode(self, payload: bytes, device_id: int, tid: int) -> bytes: """Encode ADU.""" frame = ( diff --git a/pymodbus/transaction/transaction.py b/pymodbus/transaction/transaction.py index 1c6672e7c..26ada4afd 100644 --- a/pymodbus/transaction/transaction.py +++ b/pymodbus/transaction/transaction.py @@ -8,7 +8,7 @@ from time import monotonic from ..exceptions import ConnectionException, ModbusIOException -from ..framer import FramerAscii, FramerBase, FramerRTU +from ..framer import FramerBase from ..logging import Log from ..pdu import ModbusPDU from ..transport import CommParams, CommType, ModbusProtocol @@ -316,10 +316,5 @@ def callback_data(self, data: bytes, addr: tuple | None = None) -> int: def getNextTID(self) -> int: """Retrieve the next transaction identifier.""" - if isinstance(self.framer, (FramerAscii, FramerRTU)): - self.next_tid = 0 - elif self.next_tid >= 65000: - self.next_tid = 1 - else: - self.next_tid += 1 + self.next_tid = self.framer.next_transaction_id(self.next_tid) return self.next_tid diff --git a/test/framer/test_framer.py b/test/framer/test_framer.py index 3f67987ae..9e82fed76 100644 --- a/test/framer/test_framer.py +++ b/test/framer/test_framer.py @@ -72,6 +72,22 @@ def test_crc16_table(self): assert isinstance(FramerRTU.crc16_table[0], int) assert isinstance(FramerRTU.crc16_table[255], int) + @pytest.mark.parametrize( + ("framer", "tids"), + [ + (FramerBase, [(0, 0), (1, 0), (65000, 0)]), + (FramerSocket, [(0, 1), (1, 2), (64999, 65000), (65000, 1)]), + (FramerTLS, [(0, 1), (1, 2), (64999, 65000), (65000, 1)]), + (FramerRTU, [(0, 0), (1, 0), (65000, 0)]), + (FramerAscii, [(0, 0), (1, 0), (65000, 0)]), + ], + ) + def test_next_transaction_id(self, framer, tids): + """Test transaction id sequence of each framer.""" + test_framer = framer(DecodePDU(False)) + for previous, expected in tids: + assert test_framer.next_transaction_id(previous) == expected + def test_roundtrip_CRC(self): """Test combined compute/check CRC.""" data = b"\x12\x34\x23\x45\x34\x56\x45\x67"