Skip to content

Answer ILLEGAL_FUNCTION for a request with a reserved function code - #3075

Merged
janiversen merged 2 commits into
pymodbus-dev:devfrom
carlok:reserved-function-code-request
Nov 1, 2026
Merged

janiversen merged 2 commits into
pymodbus-dev:devfrom
carlok:reserved-function-code-request

Conversation

@carlok

@carlok carlok commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

A request whose function code is 0x81 to 0xFF was decoded by the server as an exception response. datastore_update then raised NotImplementedException, and the server answered SLAVE_FAILURE (04): 81 00 00 got 81 04. The Modbus Application Protocol (V1.1b3, 4.1) reserves codes 128 to 255 for exception responses. A request with one of them is therefore an illegal function: exception 01, as for any other code the server doesn't implement.

The change: on the server side, DecodePDU.decode raises ModbusIOException for these codes. Unknown function codes already take that path, so the request handler answers ILLEGAL_FUNCTION (81 01). The client side still decodes them as exception responses.

Two existing tests changed: test_pdu.py::test_error_methods and test_extras.py::test_rtu_decode_exception decoded an exception response with the server decoder. They now use the client decoder, the side that receives exception responses.

Tests:

  • A wire test: 81 00 00 gets 81 01.
  • A decoder test for every exception code on the server side.
  • They fail on dev (b9cd6c6) and pass here. codespell, ruff, pylint, zuban and pytest (100 % coverage) pass.

Found by a differential test against libmodbus.

🤖 Generated with Claude Code

Function codes 128 to 255 are reserved for exception responses, so a
request using one is an illegal function (exception 01). The server
decoder took such a request for an exception response, then failed in
datastore_update and answered SLAVE_FAILURE (04).

Two tests decoded an exception response with the server decoder; they
now use the client decoder, which is the side that receives them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

Function codes with 0x80 set is defined in the modbus protocol as Exception Response.

The Modbus protocol do not define a Exception request, so a server cannot receive a function code > 0x80 bit generates it when the client sends something illegal.

The server should however as you state respond with ILLEGAL function.

@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.

PR will be merged, when you solve the conflict.

The conflict was in test/server/test_server_asyncio.py: pymodbus-dev#3074 and this
branch each added a server test at the same place. Both are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@janiversen
janiversen merged commit c37dd72 into pymodbus-dev:dev Nov 1, 2026
16 checks passed
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