From f0086af3172986c38fdf80f5eae21613177a920c Mon Sep 17 00:00:00 2001 From: Harsh Dave <107102025+daveharsh38@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:13:22 -0400 Subject: [PATCH 1/2] Replace framer type check in getNextTID with polymorphism. TransactionManager.getNextTID() asked isinstance(self.framer, (FramerAscii, FramerRTU)) to decide whether frames carry a transaction id. Each framer now answers this itself through next_transaction_id(): FramerBase (socket/tls) counts 1..65000 and wraps, FramerRTU/FramerAscii return 0. The transaction manager no longer imports concrete framer classes. --- pymodbus/framer/ascii.py | 5 +++++ pymodbus/framer/base.py | 10 ++++++++++ pymodbus/framer/rtu.py | 5 +++++ pymodbus/transaction/transaction.py | 9 ++------- test/framer/test_framer.py | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/pymodbus/framer/ascii.py b/pymodbus/framer/ascii.py index 53465448b..9c3c39341 100644 --- a/pymodbus/framer/ascii.py +++ b/pymodbus/framer/ascii.py @@ -79,6 +79,11 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: continue return used_len, dev_id, 0, msg[1:] + def next_transaction_id(self, transaction_id: int) -> int: + """Return 0, ASCII frames do not carry a transaction id.""" + _ = transaction_id + return 0 + def encode(self, payload: bytes, device_id: int, _tid: int) -> bytes: """Encode ADU.""" dev_id = device_id.to_bytes(1, "big") diff --git a/pymodbus/framer/base.py b/pymodbus/framer/base.py index e0d8a5690..d33eaf14a 100644 --- a/pymodbus/framer/base.py +++ b/pymodbus/framer/base.py @@ -56,6 +56,16 @@ 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. + + :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 buildFrame(self, message: ModbusPDU) -> bytes: """Create a ready to send modbus packet. diff --git a/pymodbus/framer/rtu.py b/pymodbus/framer/rtu.py index fc7fc1186..6b0a17306 100644 --- a/pymodbus/framer/rtu.py +++ b/pymodbus/framer/rtu.py @@ -102,6 +102,11 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: return data_len, dev_id, 0, data[used_len + 1 : crc_start] return 0, 0, 0, self.EMPTY + def next_transaction_id(self, transaction_id: int) -> int: + """Return 0, RTU frames do not carry a transaction id.""" + _ = transaction_id + return 0 + def encode(self, payload: bytes, device_id: int, _tid: int) -> bytes: """Encode ADU.""" frame = device_id.to_bytes(1, "big") + payload 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 3b76e1aa9..928dc4da8 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, 1), (1, 2), (64999, 65000), (65000, 1)]), + (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" From 5e2db26a27800e4fc180997dd2d7ca540dcc2c3f Mon Sep 17 00:00:00 2001 From: Harsh Dave <107102025+daveharsh38@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:10:18 -0400 Subject: [PATCH 2/2] Move the transaction id sequence into FramerSocket. As requested in review: FramerBase.next_transaction_id() now returns 0, the default for frames without a transaction id, and the 1..65000 sequence lives in FramerSocket, where the transaction id is used. FramerTLS inherits it from FramerSocket (#3102). The RTU and ASCII overrides are no longer needed and are removed. --- pymodbus/framer/ascii.py | 5 ----- pymodbus/framer/base.py | 9 +++++---- pymodbus/framer/rtu.py | 5 ----- pymodbus/framer/socket.py | 10 ++++++++++ test/framer/test_framer.py | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pymodbus/framer/ascii.py b/pymodbus/framer/ascii.py index 9c3c39341..53465448b 100644 --- a/pymodbus/framer/ascii.py +++ b/pymodbus/framer/ascii.py @@ -79,11 +79,6 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: continue return used_len, dev_id, 0, msg[1:] - def next_transaction_id(self, transaction_id: int) -> int: - """Return 0, ASCII frames do not carry a transaction id.""" - _ = transaction_id - return 0 - def encode(self, payload: bytes, device_id: int, _tid: int) -> bytes: """Encode ADU.""" dev_id = device_id.to_bytes(1, "big") diff --git a/pymodbus/framer/base.py b/pymodbus/framer/base.py index d33eaf14a..f7af67d5b 100644 --- a/pymodbus/framer/base.py +++ b/pymodbus/framer/base.py @@ -59,12 +59,13 @@ def encode(self, payload: bytes, _dev_id: int, _tid: int) -> bytes: 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: transaction id 1..65000, wrapping around to 1 + :returns: 0 """ - if transaction_id >= 65000: - return 1 - return transaction_id + 1 + _ = transaction_id + return 0 def buildFrame(self, message: ModbusPDU) -> bytes: """Create a ready to send modbus packet. diff --git a/pymodbus/framer/rtu.py b/pymodbus/framer/rtu.py index 6b0a17306..fc7fc1186 100644 --- a/pymodbus/framer/rtu.py +++ b/pymodbus/framer/rtu.py @@ -102,11 +102,6 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]: return data_len, dev_id, 0, data[used_len + 1 : crc_start] return 0, 0, 0, self.EMPTY - def next_transaction_id(self, transaction_id: int) -> int: - """Return 0, RTU frames do not carry a transaction id.""" - _ = transaction_id - return 0 - def encode(self, payload: bytes, device_id: int, _tid: int) -> bytes: """Encode ADU.""" frame = device_id.to_bytes(1, "big") + payload 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/test/framer/test_framer.py b/test/framer/test_framer.py index 4eecd37e8..9e82fed76 100644 --- a/test/framer/test_framer.py +++ b/test/framer/test_framer.py @@ -75,7 +75,7 @@ def test_crc16_table(self): @pytest.mark.parametrize( ("framer", "tids"), [ - (FramerBase, [(0, 1), (1, 2), (64999, 65000), (65000, 1)]), + (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)]),