Fixed byte conversion for float and long

This commit is contained in:
Federico Barresi
2019-11-21 14:42:59 +01:00
parent dcac7002f9
commit 0f3125fe20

View File

@@ -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;
}