Repository navigation
Conversation
Nested types were only measured from the current offset (PR S7NetPlus#191); the offset itself was never aligned and the nested type's length was never padded, so a UDT starting or ending on an odd byte shifted every following field (see S7NetPlus#190 / S7NetPlus#413). Only the read/size path was aligned, which left ToBytes writing at the old offsets. - GetIncreasedNumberOfBytes: IncrementToEven before a nested type and measure it with isInnerProperty: false so its length is padded - GetClassSize / FromBytes array branches: IncrementToEven after the loop - GetPropertyValue: IncrementToEven before and after nested FromBytes - SetBytesFromProperty / ToBytes: same alignment on the write path Behavior change: GetClassSize(TestClassWithNestedClass) 6 -> 10; the DB4 fixtures/assertions in the integration tests reflect the old layout.
The nested members of TestClassWithNestedClass now sit on even offsets (DBB2 / DBB4 / DBX6 / DBW8) and the class is 10 bytes instead of 6, so the DB4 image on the Snap7 test server has to grow and the expected size has to follow. Verified with the full unit test suite (224 passed).
mycroes
approved these changes
Oct 24, 2026
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.
Problem
Nested UDT/STRUCT members and arrays of STRUCTs are serialized at the wrong offsets.
GetClassSize/FromBytesonly align the outermost class; an inner UDT starts at thecurrent offset and its length is never padded, so a struct that starts or ends on an odd
byte shifts every following field. Arrays of structs have the same problem, and
ToByteswas never aligned at all, so writing a class used different offsets than reading it.
Related: #190, #413. The earlier fix (#191) only made the inner type be measured from the
current offset; it did not align it.
Reproduced on a real S7-1500 (TIA Portal, non-optimized DB). The DB has 10 elements of
UDT { Bool, Bool, Bool, UInt, Array[0..4] of Bool }, each occupying 6 bytes in the DB(5 bytes of data + 1 byte of padding). With the current code the first element is written
correctly and every following element is shifted; e.g. element 2's
Switchends up in thepadding byte of element 1 and its
Frequencyis read from the address of element 2's alarmarea.
Change
GetIncreasedNumberOfBytes:IncrementToEvenbefore an inner UDT, and measure it withisInnerProperty: falseso its own length is padded to an even byte as wellGetClassSize/FromBytes: pad arrays to an even byte count after the loopGetPropertyValue: align before and after a nestedFromBytesSetBytesFromProperty/ToBytes: apply the same alignment on the write pathBehavior change
Class.GetClassSize(new TestClassWithNestedClass())is now 10 instead of 6, and its nestedmembers move to even offsets (DBB2 / DBB4 / DBX6 / DBW8). The
DB4image on the Snap7 testserver and the two expectations in
S7NetTestsSync/S7NetTestsAsyncare updated in thesecond commit to match.
Testing
dotnet test S7.Net.UnitTest(net6.0, Windows, Snap7 test server): 224 passed, 0 failedelements, values and bit positions match TIA Portal's offsets