diff --git a/Sharp7.Rx.Tests/S7ValueConverterTests.cs b/Sharp7.Rx.Tests/S7ValueConverterTests.cs index 54bf85c..0cdf048 100644 --- a/Sharp7.Rx.Tests/S7ValueConverterTests.cs +++ b/Sharp7.Rx.Tests/S7ValueConverterTests.cs @@ -31,6 +31,7 @@ public class S7ValueConverterTests [TestCase(0.25f, "DB0.D0", new byte[] {0x3E, 0x80, 0x00, 0x00})] [TestCase(0.25, "DB0.D0", new byte[] {0x3E, 0x80, 0x00, 0x00})] [TestCase("ABCD", "DB0.string0.4", new byte[] {0x00, 0x04, 0x41, 0x42, 0x43, 0x44})] + [TestCase("ABCD", "DB0.string0.4", new byte[] {0x00, 0xF0, 0x41, 0x42, 0x43, 0x44})] // Clip to length in Address [TestCase("ABCD", "DB0.DBB0.4", new byte[] {0x41, 0x42, 0x43, 0x44})] public void Parse(T expected, string address, byte[] data) { diff --git a/Sharp7.Rx/S7ValueConverter.cs b/Sharp7.Rx/S7ValueConverter.cs index 9662fb3..9533809 100644 --- a/Sharp7.Rx/S7ValueConverter.cs +++ b/Sharp7.Rx/S7ValueConverter.cs @@ -63,7 +63,13 @@ namespace Sharp7.Rx if (typeof(TValue) == typeof(string)) if (address.Type == DbType.String) { - return (TValue) (object) Encoding.ASCII.GetString(buffer); + // First byte is maximal length + // Second byte is actual length + // https://cache.industry.siemens.com/dl/files/480/22506480/att_105176/v1/s7_scl_string_parameterzuweisung_e.pdf + + var length = Math.Min(address.Length, buffer[1]); + + return (TValue) (object) Encoding.ASCII.GetString(buffer, 2, length); } else {