Skip to content

Answer ILLEGAL_VALUE for a Write Single Coil value other than 0x0000/0xFF00 - #3074

Merged
janiversen merged 1 commit into
pymodbus-dev:devfrom
carlok:write-single-coil-illegal-value
Oct 31, 2026
Merged

janiversen merged 1 commit into
pymodbus-dev:devfrom
carlok:write-single-coil-illegal-value

Conversation

@carlok

@carlok carlok commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

A Write Single Coil request (0x05) with a value other than 0xFF00 or 0x0000 was carried out. Any non-zero value turned the coil ON, and the response echoed 0xFF00, a value the client didn't send. The Modbus Application Protocol (V1.1b3, 6.5) says "All other values are illegal and will not affect the output", and its state diagram answers exception 03.

The change: WriteSingleCoilRequest keeps the decoded value, and datastore_update answers ILLEGAL_VALUE for any other value before it touches the datastore. That is the same place #3066 checks counts. A request built through the API (no decoded value) is unchanged.

Tests:

  • On the TCP server, 05 0000 0001 now gets 85 03. Before, it got 05 0000 ff00 and the coil was turned ON.
  • Unit tests cover three illegal values (the datastore isn't touched) and the two legal ones.
  • The new tests fail on dev (b9cd6c6) and pass here. codespell, ruff, pylint, zuban and pytest (100 % coverage) pass.

Found by a differential test against libmodbus, which answers exception 03 to the same request.

🤖 Generated with Claude Code

…0xFF00

The specification (6.5) allows only 0xFF00 (ON) and 0x0000 (OFF); any
other value is illegal, must not affect the coil, and gets exception
03. The server took any non-zero value as ON and echoed 0xFF00.

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.

Your change is correct and according to the protocol documentation !

However many devices hace a more relaxed implementación, that says 0x00 is OFF and all other values are ON.

@janiversen
janiversen merged commit e0460de into pymodbus-dev:dev Oct 31, 2026
16 checks passed
@janiversen

Copy link
Copy Markdown
Collaborator

Feel cree to open issues or make more PR, if you find other differences that also are not in the standard.

@janiversen

Copy link
Copy Markdown
Collaborator

Refactored and added to PR #3068 (test from this PR is kept original to verify, it is implemented correctly).

carlok added a commit to carlok/pymodbus that referenced this pull request Nov 1, 2026
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>
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