diff --git a/Sharp7.Rx.Tests/S7ValueConverterTests.cs b/Sharp7.Rx.Tests/S7ValueConverterTests.cs index 0cdf048..a1eb657 100644 --- a/Sharp7.Rx.Tests/S7ValueConverterTests.cs +++ b/Sharp7.Rx.Tests/S7ValueConverterTests.cs @@ -20,7 +20,7 @@ public class S7ValueConverterTests [TestCase((char) 18, "DB0.DBB0", new byte[] {0x12})] [TestCase((short) 4660, "DB0.INT0", new byte[] {0x12, 0x34})] [TestCase((short) -3532, "DB0.INT0", new byte[] {0xF2, 0x34})] - [TestCase(62004, "DB0.INT0", new byte[] {0xF2, 0x34})] + [TestCase(-3532, "DB0.INT0", new byte[] {0xF2, 0x34})] [TestCase(305419879, "DB0.DINT0", new byte[] {0x12, 0x34, 0x56, 0x67})] [TestCase(-231451033, "DB0.DINT0", new byte[] {0xF2, 0x34, 0x56, 0x67})] [TestCase(1311768394163015151L, "DB0.dul0", new byte[] {0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF})] diff --git a/Sharp7.Rx/S7ValueConverter.cs b/Sharp7.Rx/S7ValueConverter.cs index 04d8965..d97fa31 100644 --- a/Sharp7.Rx/S7ValueConverter.cs +++ b/Sharp7.Rx/S7ValueConverter.cs @@ -16,41 +16,40 @@ namespace Sharp7.Rx if (typeof(TValue) == typeof(int)) { if (address.Length == 2) - return (TValue) (object) BinaryPrimitives.ReadInt16BigEndian(buffer); + return (TValue) (object) (int) BinaryPrimitives.ReadInt16BigEndian(buffer); if (address.Length == 4) - { return (TValue) (object) BinaryPrimitives.ReadInt32BigEndian(buffer); - } throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}"); } 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) || typeof(TValue) == typeof(char)) - { + if (typeof(TValue) == typeof(byte)) return (TValue) (object) buffer[0]; - } + if (typeof(TValue) == typeof(char)) + return (TValue) (object) (char)buffer[0]; if (typeof(TValue) == typeof(byte[])) - { return (TValue) (object) buffer; + + if (typeof(TValue) == typeof(double)) + { + var d = new UInt32SingleMap + { + UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer) + }; + return (TValue) (object) (double)d.Single; } - if (typeof(TValue) == typeof(double) || typeof(TValue) == typeof(float)) + if (typeof(TValue) == typeof(float)) { var d = new UInt32SingleMap { @@ -71,9 +70,7 @@ namespace Sharp7.Rx return (TValue) (object) Encoding.ASCII.GetString(buffer, 2, length); } else - { return (TValue) (object) Encoding.ASCII.GetString(buffer).Trim(); - } throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue))); }