Add optional word_len parameter to read_area/write_area#584
Open
gijzelaerr wants to merge 2 commits intomasterfrom
Open
Add optional word_len parameter to read_area/write_area#584gijzelaerr wants to merge 2 commits intomasterfrom
gijzelaerr wants to merge 2 commits intomasterfrom
Conversation
The read_area() and write_area() methods hardcoded WordLen.Byte for non-timer/counter areas, preventing users from performing bit-level reads and writes. Add an optional word_len parameter that allows callers to override the default area-based word length selection. When word_len is None (the default), the existing area-based logic is preserved. When provided, the WordLen enum value is mapped to the corresponding S7WordLen value. Fixes #559 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <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.
Summary
Fixes #559
write_area()andread_area()previously hardcodedWordLen.Bytefor all non-timer/counter areas, making it impossible to perform bit-level operations through the standard API.Root cause analysis
The S7 protocol supports multiple word lengths for area access (
S7WLBit,S7WLByte,S7WLWord, etc.), and the original Snap7 C library exposes this parameter inCli_WriteArea()/Cli_ReadArea(). However, the python-snap7 wrapper hardcoded the word length selection based solely on the area type:This meant users could not write individual bits using
WordLen.Bitwith thebyte_index*8 + bit_indexaddressing scheme described in the Snap7 documentation.Fix
Added an optional
word_len: Optional[WordLen] = Noneparameter to bothread_area()andwrite_area():word_lenisNone(the default), the existing area-based logic is preserved — fully backwards compatibleword_lenis provided, it is used directly after mapping from the publicWordLenenum to the internalS7WordLenenum (they share identical integer values)This enables bit-level writes like:
Test plan
WordLen.Bitword_lenparameter behave identically🤖 Generated with Claude Code