Repository navigation
Answer ILLEGAL_FUNCTION for a request with a reserved function code - #3075
Merged
janiversen merged 2 commits intoNov 1, 2026
Merged
Conversation
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
requested changes
Oct 31, 2026
Collaborator
There was a problem hiding this comment.
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
approved these changes
Oct 31, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A request whose function code is 0x81 to 0xFF was decoded by the server as an exception response.
datastore_updatethen raisedNotImplementedException, and the server answered SLAVE_FAILURE (04):81 00 00got81 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.decoderaisesModbusIOExceptionfor 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_methodsandtest_extras.py::test_rtu_decode_exceptiondecoded an exception response with the server decoder. They now use the client decoder, the side that receives exception responses.Tests:
81 00 00gets81 01.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