Repository navigation
Feature/optimize 03 string write maxlength fix #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jogibear9988
merged 3 commits into
dotnetprojects:master
from
Stiwa-PohG:feature/optimize-03-string-write-maxlength-fix
Oct 7, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
41a926a
Fix thread-unsafe LinkedList access in OpenSSLConnector pending buffe…
Stiwa-PohG f65c8d9
Add PlcTag.GetValue/SetValue and POffsetInfoType.MaxStringLength
Stiwa-PohG 46c1e57
Fix TagFactory not forwarding declared PLC string length, causing STR…
Stiwa-PohG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using S7CommPlusDriver.ClientApi; | ||
| using Xunit; | ||
|
|
||
| namespace S7CommPlusDriver.Tests | ||
| { | ||
| /// <summary> | ||
| /// Verifies that <see cref="PlcTags.TagFactory"/> forwards the PLC-declared maximum string length to | ||
| /// <see cref="PlcTagString"/>, <see cref="PlcTagStringArray"/>, and <see cref="PlcTagWString"/>, and that the resulting | ||
| /// write telegram matches the declared length instead of always falling back to the built-in default of 254. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Regression test for a bug where <c>TagFactory</c> never received the declared maximum string length, so every | ||
| /// <c>String</c>/<c>WString</c> tag was created with the built-in default of 254, regardless of its actual PLC | ||
| /// declaration (e.g. <c>String[10]</c>). Writing such a tag then sent a telegram of the wrong size and with the wrong | ||
| /// declared-length header byte, which the PLC rejected. | ||
| /// </remarks> | ||
| public sealed class PlcTagStringMaxLengthTests | ||
| { | ||
| [Fact] | ||
| public void TagFactoryAppliesDeclaredMaxStringLengthToScalarStringTag() | ||
| { | ||
| var tag = PlcTags.TagFactory( | ||
| "DB.Text", | ||
| new ItemAddress("8A0E0001.F"), | ||
| Softdatatype.S7COMMP_SOFTDATATYPE_STRING, | ||
| Is1Dim: false, | ||
| maxStringLength: 10); | ||
|
|
||
| var stringTag = Assert.IsType<PlcTagString>(tag); | ||
| stringTag.Value = "0123456789"; // exactly 10 characters, must not throw | ||
|
|
||
| var writeValue = Assert.IsType<ValueUSIntArray>(GetWriteValue(stringTag)); | ||
| var bytes = writeValue.GetValue(); | ||
|
|
||
| Assert.Equal(12, bytes.Length); // declared max length (10) + 2 header bytes | ||
| Assert.Equal(10, bytes[0]); // declared max length, not the built-in default of 254 | ||
| Assert.Equal(10, bytes[1]); // actual length | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TagFactoryFallsBackToDefaultMaxStringLengthWhenNotSupplied() | ||
| { | ||
| var tag = PlcTags.TagFactory( | ||
| "DB.Text", | ||
| new ItemAddress("8A0E0001.F"), | ||
| Softdatatype.S7COMMP_SOFTDATATYPE_STRING); | ||
|
|
||
| var stringTag = Assert.IsType<PlcTagString>(tag); | ||
| stringTag.Value = "Hello"; | ||
|
|
||
| var writeValue = Assert.IsType<ValueUSIntArray>(GetWriteValue(stringTag)); | ||
| var bytes = writeValue.GetValue(); | ||
|
|
||
| Assert.Equal(256, bytes.Length); // built-in default of 254 + 2 header bytes | ||
| Assert.Equal(254, bytes[0]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TagFactoryAppliesDeclaredMaxStringLengthToStringArrayElementTag() | ||
| { | ||
| var tag = PlcTags.TagFactory( | ||
| "DB.Texts", | ||
| new ItemAddress("8A0E0001.F"), | ||
| Softdatatype.S7COMMP_SOFTDATATYPE_STRING, | ||
| Is1Dim: true, | ||
| maxStringLength: 5); | ||
|
|
||
| Assert.IsType<PlcTagStringArray>(tag); | ||
| Assert.Equal(5, tag.GetMaxStringLength()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TagFactoryAppliesDeclaredMaxStringLengthToWStringTag() | ||
| { | ||
| var tag = PlcTags.TagFactory( | ||
| "DB.UnicodeText", | ||
| new ItemAddress("8A0E0001.F"), | ||
| Softdatatype.S7COMMP_SOFTDATATYPE_WSTRING, | ||
| Is1Dim: false, | ||
| maxStringLength: 20); | ||
|
|
||
| var wstringTag = Assert.IsType<PlcTagWString>(tag); | ||
| Assert.Equal(20, wstringTag.GetMaxStringLength()); | ||
| } | ||
|
|
||
| private static PValue GetWriteValue(PlcTag tag) | ||
| { | ||
| return tag.GetWriteValue(); | ||
| } | ||
| } | ||
| } |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is not really relevant