Skip to content

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

Open
carlok wants to merge 1 commit into
pymodbus-dev:devfrom
carlok:write-single-coil-illegal-value
Open

carlok wants to merge 1 commit into
pymodbus-dev:devfrom
carlok:write-single-coil-illegal-value

Conversation

@carlok

@carlok carlok commented Oct 30, 2026

Copy link
Copy Markdown

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 <[email protected]>

This branch has not been deployed

No deployments
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