mirror of
https://github.com/evopro-ag/Sharp7Reactive.git
synced 2025-12-15 19:32:53 +00:00
Fix String conversion
This commit is contained in:
@@ -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>(T expected, string address, byte[] data)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user