diff --git a/Sharp7.Rx.Tests/S7ValueConverterTests/ConverterTestBase.cs b/Sharp7.Rx.Tests/S7ValueConverterTests/ConverterTestBase.cs index 273029c..a4fb1b4 100644 --- a/Sharp7.Rx.Tests/S7ValueConverterTests/ConverterTestBase.cs +++ b/Sharp7.Rx.Tests/S7ValueConverterTests/ConverterTestBase.cs @@ -30,17 +30,17 @@ internal abstract class ConverterTestBase yield return new ConverterTestCase((short) 4660, "DB99.Int5", [0x12, 0x34]); yield return new ConverterTestCase((short) -3532, "DB99.Int5", [0xF2, 0x34]); yield return new ConverterTestCase((ushort) 4660, "DB99.UInt5", [0x12, 0x34]); - yield return new ConverterTestCase((ushort) 3532, "DB99.UInt5", [0xF2, 0x34]); + yield return new ConverterTestCase((ushort) 62004, "DB99.UInt5", [0xF2, 0x34]); yield return new ConverterTestCase(305419879, "DB99.DInt5", [0x12, 0x34, 0x56, 0x67]); yield return new ConverterTestCase(-231451033, "DB99.DInt5", [0xF2, 0x34, 0x56, 0x67]); yield return new ConverterTestCase(305419879u, "DB99.UDInt5", [0x12, 0x34, 0x56, 0x67]); - yield return new ConverterTestCase(231451033u, "DB99.UDInt5", [0xF2, 0x34, 0x56, 0x67]); + yield return new ConverterTestCase(4063516263u, "DB99.UDInt5", [0xF2, 0x34, 0x56, 0x67]); yield return new ConverterTestCase(1311768394163015151L, "DB99.LInt5", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); yield return new ConverterTestCase(-994074615050678801L, "DB99.LInt5", [0xF2, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); yield return new ConverterTestCase(1311768394163015151uL, "DB99.ULInt5", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); yield return new ConverterTestCase(17452669458658872815uL, "DB99.ULInt5", [0xF2, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); yield return new ConverterTestCase(0.25f, "DB99.Real5", [0x3E, 0x80, 0x00, 0x00]); - yield return new ConverterTestCase(0.25, "DB99.LReal5", [0x3E, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + yield return new ConverterTestCase(0.25, "DB99.LReal5", [0x3F, 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); yield return new ConverterTestCase(new byte[] {0x12, 0x34, 0x56, 0x67}, "DB99.Byte5.4", [0x12, 0x34, 0x56, 0x67]); @@ -57,7 +57,6 @@ internal abstract class ConverterTestBase yield return new ConverterTestCase((byte) 18, "DB99.DBB0", [0x12]); yield return new ConverterTestCase((short) 4660, "DB99.INT0", [0x12, 0x34]); yield return new ConverterTestCase((short) -3532, "DB99.INT0", [0xF2, 0x34]); - yield return new ConverterTestCase(-3532, "DB99.INT0", [0xF2, 0x34]); yield return new ConverterTestCase(305419879, "DB99.DINT0", [0x12, 0x34, 0x56, 0x67]); yield return new ConverterTestCase(-231451033, "DB99.DINT0", [0xF2, 0x34, 0x56, 0x67]); yield return new ConverterTestCase(1311768394163015151L, "DB99.dul0", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); @@ -81,6 +80,6 @@ internal abstract class ConverterTestBase { public S7VariableAddress VariableAddress => Parser.Parse(Address); - public override string ToString() => $"{Address} {Value} ({Value.GetType().Name})"; + public override string ToString() => $"{Value.GetType().Name}, {Address}: {Value}"; } } diff --git a/Sharp7.Rx/Exceptions/S7Exception.cs b/Sharp7.Rx/Exceptions/S7Exception.cs index e9cc1d3..fb836f9 100644 --- a/Sharp7.Rx/Exceptions/S7Exception.cs +++ b/Sharp7.Rx/Exceptions/S7Exception.cs @@ -11,6 +11,44 @@ public abstract class S7Exception : Exception } } +public class DataTypeMissmatchException : S7Exception +{ + internal DataTypeMissmatchException(string message, Type type, S7VariableAddress address) : base(message) + { + Type = type; + Address = address.ToString(); + } + + internal DataTypeMissmatchException(string message, Exception innerException, Type type, S7VariableAddress address) : base(message, innerException) + { + Type = type; + Address = address.ToString(); + } + + public string Address { get; } + + public Type Type { get; } +} + +public class UnsupportedS7TypeException : S7Exception +{ + internal UnsupportedS7TypeException(string message, Type type, S7VariableAddress address) : base(message) + { + Type = type; + Address = address.ToString(); + } + + internal UnsupportedS7TypeException(string message, Exception innerException, Type type, S7VariableAddress address) : base(message, innerException) + { + Type = type; + Address = address.ToString(); + } + + public string Address { get; } + + public Type Type { get; } +} + public class InvalidS7AddressException : S7Exception { public InvalidS7AddressException(string message, string input) : base(message) @@ -23,5 +61,5 @@ public class InvalidS7AddressException : S7Exception Input = input; } - public string Input { get; private set; } + public string Input { get; } } diff --git a/Sharp7.Rx/S7ValueConverter.cs b/Sharp7.Rx/S7ValueConverter.cs index 9618a70..90b21d8 100644 --- a/Sharp7.Rx/S7ValueConverter.cs +++ b/Sharp7.Rx/S7ValueConverter.cs @@ -7,60 +7,92 @@ namespace Sharp7.Rx; internal static class S7ValueConverter { + private static readonly Dictionary> 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(byte[] buffer, S7VariableAddress address) { - if (typeof(TValue) == typeof(bool)) - return (TValue) (object) (((buffer[0] >> address.Bit) & 1) > 0); + // Todo: Change to Span 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(Span 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; + } } diff --git a/Sharp7.Rx/S7VariableAddress.cs b/Sharp7.Rx/S7VariableAddress.cs index 0172c06..f04bd34 100644 --- a/Sharp7.Rx/S7VariableAddress.cs +++ b/Sharp7.Rx/S7VariableAddress.cs @@ -19,4 +19,14 @@ internal class S7VariableAddress DbType.WString => (ushort) (Length * 2 + 4), _ => Length }; + + public override string ToString() => + Type switch + { + DbType.Bit => $"{Operand}{DbNr}.{Type}{Start}.{Bit}", + DbType.String => $"{Operand}{DbNr}.{Type}{Start}.{Length}", + DbType.WString => $"{Operand}{DbNr}.{Type}{Start}.{Length}", + DbType.Byte => Length == 1 ? $"{Operand}{DbNr}.{Type}{Start}" : $"{Operand}{DbNr}.{Type}{Start}.{Length}", + _ => $"{Operand}{DbNr}.{Type}{Start}", + }; }