Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pymodbus/framer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment thread
janiversen marked this conversation as resolved.
Resolved
"""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.

Expand Down
10 changes: 10 additions & 0 deletions pymodbus/framer/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
9 changes: 2 additions & 7 deletions pymodbus/transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions test/framer/test_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading