mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Fix write stringex (#162)
* Add StringEx.ToByteArray(...) * Add Serialization.SerializeDataItem(DataItem) Supports StringEx VarType or offloads to SerializeValue method. * Use SerializeDataItem in S7WriteMultiple * Assume string length without header in StringEx.ToByteArray VarTypeToByteLength already assumed that StringEx declared count for the number of characters without the header, this now matches that behavior. * Add unit tests for StringEx conversions * Fix incorrect value passed to Encoding.GetBytes The length must actually be within string limits.
This commit is contained in:
committed by
Raphael Schlameuß
parent
a1b69a5c5a
commit
2204ab360c
@@ -11,6 +11,16 @@ namespace S7.Net.Protocol
|
||||
return (ushort)((buf[index] << 8) + buf[index]);
|
||||
}
|
||||
|
||||
public static byte[] SerializeDataItem(DataItem dataItem)
|
||||
{
|
||||
if (dataItem.Value is string s)
|
||||
return dataItem.VarType == VarType.StringEx
|
||||
? StringEx.ToByteArray(s, dataItem.Count)
|
||||
: Types.String.ToByteArray(s);
|
||||
|
||||
return SerializeValue(dataItem.Value);
|
||||
}
|
||||
|
||||
public static byte[] SerializeValue(object value)
|
||||
{
|
||||
switch (value.GetType().Name)
|
||||
|
||||
Reference in New Issue
Block a user