Skip to content

Let the framer decide the next transaction id. - #3101

Merged
janiversen merged 3 commits into
pymodbus-dev:devfrom
daveharsh38:refactor/framer-next-transaction-id
Nov 25, 2026
Merged

janiversen merged 3 commits into
pymodbus-dev:devfrom
daveharsh38:refactor/framer-next-transaction-id

Conversation

@daveharsh38

@daveharsh38 daveharsh38 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

TransactionManager.getNextTID() checks isinstance(self.framer, (FramerAscii, FramerRTU)). As a result, the transaction manager imports concrete framer classes, and a new framer type would need an edit there. This PR moves the decision to the framers:

  • FramerBase.next_transaction_id() returns 0, the default for frames without a transaction id (RTU, ASCII).
  • FramerSocket.next_transaction_id() returns 1..65000 and wraps; FramerTLS inherits it (Cleanup TLS framer. #3102).
  • getNextTID() becomes a single call, and transaction.py only imports FramerBase.

Behaviour is identical for all four framers. test_next_transaction_id covers every framer, including the wrap-around. check_ci.sh passes with coverage at 100%.

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.

@janiversen janiversen left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but the preference is to have tid=0 in base.py.

Next_tranaction_id with tid != 0 goes into socket.

Comment thread pymodbus/framer/ascii.py Outdated
continue
return used_len, dev_id, 0, msg[1:]

def next_transaction_id(self, transaction_id: int) -> int:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more logical to have this in the base, and the exception (where TID is actually used) in socket.py (remark tls should really depend on socket, as there are no special implementation).

The default implementation is not to use transaction_id. Mid term, the idea is to move the whole transaction handling into socket.py, and simplify transaction.py

Comment thread pymodbus/framer/base.py Resolved
Comment thread pymodbus/framer/rtu.py Outdated
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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove, it covered in base.py

@janiversen

Copy link
Copy Markdown
Collaborator

TLS have been cleaned up in #3102

janiversen and others added 2 commits November 25, 2026 11:04
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 (pymodbus-dev#3102). The RTU and ASCII
overrides are no longer needed and are removed.

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks.

@janiversen
janiversen merged commit 9d18fb5 into pymodbus-dev:dev Nov 25, 2026
16 checks passed
@janiversen

Copy link
Copy Markdown
Collaborator

If you want to have a challenge for a future PR:

We want to move the transaction_id handling from transaction.py to socket.py. Transaction.py probably still needs access to the received/sent transaction_id.

When client.py calls transaction.py it should send a 0 as transaction_id to socket and socket should assign the next id.
When server.py calls transaction.py it should send request transaction_id to socket and socket should simply use that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants