Skip to content

modbus: set the exception bit with | 0x80, not + 0x80 - #888

Open
carlok wants to merge 1 commit into
stephane:masterfrom
carlok:exception-function-byte
Open

carlok wants to merge 1 commit into
stephane:masterfrom
carlok:exception-function-byte

Conversation

@carlok

@carlok carlok commented Oct 30, 2026

Copy link
Copy Markdown

A request whose function code is 0x80 to 0xFF gets an exception response whose function byte wraps around.

  • Where: response_exception() builds that byte as sft->function + 0x80 in a byte. modbus_reply_exception() does the same.
  • The effect: 81 00 00 is answered 01 01. That reads as a Read Coils response with a byte count of 1 and no data, so the client waits for a data byte that never comes. For 0x80 the function byte becomes 0x00.
  • The rule: the Modbus Application Protocol (V1.1b3) reserves 128 to 255 for exception responses (4.1), and an exception response has the function code's high bit set (7).

The change: | 0x80 in both places. Nothing changes for valid function codes, whose high bit is clear.

The test: unit-test-client.c sends the raw request 81 00 00 00 00. It expects exception 01 with the function byte 0x81. It fails on master and passes here. tests/unit-tests.sh and make distcheck pass.

Found by a differential test against pymodbus.

🤖 Generated with Claude Code

A request whose function code is 0x80 to 0xFF (reserved for exception
responses) got an exception whose function byte wrapped around: 0x81
was answered 0x01 0x01, which reads as a Read Coils response with a
byte count of 1 and no data. The client then waits for that data byte.
Building the byte with | 0x80 keeps the exception bit set and changes
nothing for valid function codes.
@cla-bot

cla-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

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.

1 participant