From 0f3125fe20485eb2836ce5de01971742045bde9b Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Thu, 21 Nov 2019 14:42:59 +0100 Subject: [PATCH] Fixed byte conversion for float and long --- Sharp7.Rx/Sharp7Plc.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index f295047..2772877 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reactive; using System.Reactive.Linq; using System.Reactive.Subjects; @@ -84,19 +85,27 @@ namespace Sharp7.Rx if (address.Length == 4) { Array.Reverse(b); - return (TValue)(object)Convert.ToInt32(b); + return (TValue)(object)BitConverter.ToInt32(b,0); } throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}"); } + if (typeof(TValue) == typeof(long)) + { + var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); + token.ThrowIfCancellationRequested(); + Array.Reverse(b); + return (TValue)(object)BitConverter.ToInt64(b,0); + } + if (typeof(TValue) == typeof(ulong)) { var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); token.ThrowIfCancellationRequested(); Array.Reverse(b); - return (TValue)(object)Convert.ToUInt64(b); + return (TValue)(object)BitConverter.ToUInt64(b, 0); } if (typeof(TValue) == typeof(short)) @@ -125,7 +134,7 @@ namespace Sharp7.Rx { var bytes = await s7Connector.ReadBytes(address.Operand, address.Start, 4, address.DbNr, token); token.ThrowIfCancellationRequested(); - var d = Convert.ToSingle(bytes); + var d = BitConverter.ToSingle(bytes.Reverse().ToArray(),0); return (TValue)(object)d; }