Add new types to S7ValueConverter.ReadFromBuffer

This commit is contained in:
Peter Butzhammer
2024-02-08 12:57:36 +01:00
parent 3145426432
commit 3c592c6d46
4 changed files with 141 additions and 55 deletions

View File

@@ -7,60 +7,92 @@ namespace Sharp7.Rx;
internal static class S7ValueConverter
{
private static readonly Dictionary<Type, Func<byte[], S7VariableAddress, object>> readFunctions = new()
{
{typeof(bool), (buffer, address) => (buffer[0] >> address.Bit & 1) > 0},
{typeof(byte), (buffer, address) => buffer[0]},
{typeof(byte[]), (buffer, address) => buffer},
{typeof(short), (buffer, address) => BinaryPrimitives.ReadInt16BigEndian(buffer)},
{typeof(ushort), (buffer, address) => BinaryPrimitives.ReadUInt16BigEndian(buffer)},
{typeof(int), (buffer, address) => BinaryPrimitives.ReadInt32BigEndian(buffer)},
{typeof(uint), (buffer, address) => BinaryPrimitives.ReadUInt32BigEndian(buffer)},
{typeof(long), (buffer, address) => BinaryPrimitives.ReadInt64BigEndian(buffer)},
{typeof(ulong), (buffer, address) => BinaryPrimitives.ReadUInt64BigEndian(buffer)},
{
typeof(float), (buffer, address) =>
{
// Todo: Use BinaryPrimitives when switched to newer .net
var d = new UInt32SingleMap
{
UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer)
};
return d.Single;
}
},
{
typeof(double), (buffer, address) =>
{
// Todo: Use BinaryPrimitives when switched to newer .net
var d = new UInt64DoubleMap
{
UInt64 = BinaryPrimitives.ReadUInt64BigEndian(buffer)
};
return d.Double;
}
},
{
typeof(string), (buffer, address) =>
{
return address.Type switch
{
DbType.String => ParseString(),
DbType.WString => ParseWString(),
DbType.Byte => Encoding.ASCII.GetString(buffer),
_ => throw new DataTypeMissmatchException($"Cannot read string from {address.Type}", typeof(string), address)
};
string ParseString()
{
// First byte is maximal length
// Second byte is actual length
// https://support.industry.siemens.com/cs/mdm/109747174?c=94063831435&lc=de-DE
var length = Math.Min(address.Length, buffer[1]);
return Encoding.ASCII.GetString(buffer, 2, length);
}
string ParseWString()
{
// First 2 bytes are maximal length
// Second 2 bytes are actual length
// https://support.industry.siemens.com/cs/mdm/109747174?c=94063855243&lc=de-DE
// the length of the string is two bytes per
var length = Math.Min(address.Length, BinaryPrimitives.ReadUInt16BigEndian(buffer.AsSpan(2,2))) * 2;
return Encoding.BigEndianUnicode.GetString(buffer, 4, length);
}
}
},
};
public static TValue ReadFromBuffer<TValue>(byte[] buffer, S7VariableAddress address)
{
if (typeof(TValue) == typeof(bool))
return (TValue) (object) (((buffer[0] >> address.Bit) & 1) > 0);
// Todo: Change to Span<byte> when switched to newer .net
if (typeof(TValue) == typeof(int))
{
if (address.Length == 2)
return (TValue) (object) (int) BinaryPrimitives.ReadInt16BigEndian(buffer);
if (address.Length == 4)
return (TValue) (object) BinaryPrimitives.ReadInt32BigEndian(buffer);
var type = typeof(TValue);
throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}");
}
if (!readFunctions.TryGetValue(type, out var readFunc))
throw new UnsupportedS7TypeException($"{type.Name} is not supported. {address}", type, address);
if (typeof(TValue) == typeof(long))
return (TValue) (object) BinaryPrimitives.ReadInt64BigEndian(buffer);
if (typeof(TValue) == typeof(ulong))
return (TValue) (object) BinaryPrimitives.ReadUInt64BigEndian(buffer);
if (typeof(TValue) == typeof(short))
return (TValue) (object) BinaryPrimitives.ReadInt16BigEndian(buffer);
if (typeof(TValue) == typeof(byte))
return (TValue) (object) buffer[0];
if (typeof(TValue) == typeof(byte[]))
return (TValue) (object) buffer;
if (typeof(TValue) == typeof(float))
{
var d = new UInt32SingleMap
{
UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer)
};
return (TValue) (object) d.Single;
}
if (typeof(TValue) == typeof(string))
if (address.Type == DbType.String)
{
// 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
return (TValue) (object) Encoding.ASCII.GetString(buffer).Trim();
throw new InvalidOperationException($"type '{typeof(TValue)}' not supported.");
var result = readFunc(buffer, address);
return (TValue) result;
}
public static void WriteToBuffer<TValue>(Span<byte> buffer, TValue value, S7VariableAddress address)
@@ -145,4 +177,11 @@ internal static class S7ValueConverter
[FieldOffset(0)] public uint UInt32;
[FieldOffset(0)] public float Single;
}
[StructLayout(LayoutKind.Explicit)]
private struct UInt64DoubleMap
{
[FieldOffset(0)] public ulong UInt64;
[FieldOffset(0)] public double Double;
}
}