From 3df11fc9b1e9eb22d1c0dc5e35f62462d46a02ec Mon Sep 17 00:00:00 2001
From: smartwyy <645583145@qq.com>
Date: Sun, 26 May 2024 17:07:45 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E8=A7=A3=E6=9E=90=EF=BC=8Cfl?=
=?UTF-8?q?oat=E5=92=8Cdouble=E6=94=AF=E6=8C=81=E6=9C=89=E7=AC=A6=E5=8F=B7?=
=?UTF-8?q?=E5=92=8C=E6=97=A0=E7=AC=A6=E5=8F=B7=EF=BC=8C=E9=BB=98=E8=AE=A4?=
=?UTF-8?q?=E6=97=A0=E7=AC=A6=E5=8F=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HybirdFrameworkCore/Utils/BitUtls.cs | 28 ++++++++-----------
.../ModbusTcpMaster/ModbusDecoder.cs | 6 ++--
2 files changed, 14 insertions(+), 20 deletions(-)
diff --git a/HybirdFrameworkCore/Utils/BitUtls.cs b/HybirdFrameworkCore/Utils/BitUtls.cs
index 66e21a5..075bb4c 100644
--- a/HybirdFrameworkCore/Utils/BitUtls.cs
+++ b/HybirdFrameworkCore/Utils/BitUtls.cs
@@ -15,10 +15,11 @@ public static class BitUtls
/// 精度
/// 保留几位小数
/// 位偏移
+ /// 对float和double 默认无符号
///
///
public static object Bytes2Value(byte[] bytes, Type propertyType, int start, int length,
- double scale, int round, double offset)
+ double scale, int round, double offset, bool unSign = true)
{
if (propertyType == BOOLEAN) return Convert.ChangeType(Byte2Bit(bytes, start, length) == 1, propertyType);
@@ -44,11 +45,11 @@ public static class BitUtls
if (propertyType == FLOAT)
return
- Convert.ChangeType(Math.Round(Byte2Float(bytes, start, length, scale) - offset, round), propertyType);
+ Convert.ChangeType(Math.Round(Byte2Float(bytes, start, length, scale, unSign) - offset, round), propertyType);
if (propertyType == DOUBLE)
return
- Convert.ChangeType(Math.Round(Byte2Double(bytes, start, length, scale) - offset, round),
+ Convert.ChangeType(Math.Round(Byte2Double(bytes, start, length, scale, unSign) - offset, round),
propertyType);
if (propertyType == STRING)
@@ -220,46 +221,39 @@ public static class BitUtls
return list.ToArray();
}
- public static float Byte2Float(byte[] bytes, int startBit, int length, double factor)
+ public static float Byte2Float(byte[] bytes, int startBit, int length, double factor, bool unSign)
{
if (length < 17)
{
- int d = Byte2UInt16(bytes, startBit, length);
+ int d =unSign ? Byte2UInt16(bytes, startBit, length) : Byte2Int16(bytes, startBit, length);
return (float)(d * factor);
}
else
{
- var d = Byte2UInt32(bytes, startBit, length);
+ Int64 d = unSign ? Byte2UInt32(bytes, startBit, length) : Byte2Int32(bytes, startBit, length) ;
return (float)(d * factor);
}
}
- public static double Byte2Double(byte[] bytes, int startBit, int length, double factor)
+ public static double Byte2Double(byte[] bytes, int startBit, int length, double factor, bool unSign)
{
- var d = Byte2UInt32(bytes, startBit, length);
+ Int64 d = unSign ? Byte2UInt32(bytes, startBit, length) : Byte2Int32(bytes, startBit, length);
return (float)(d * factor);
}
public static ushort Byte2UInt16(byte[] bytes, int startBit, int length)
{
+ if (length < 9 || length > 16) throw new ArgumentException("length should be less then 17 and greater then 8");
var sub = Sub(bytes, startBit, length);
- if (sub.Length == 1)
- {
- return Convert.ToUInt16(sub[0]);
- }
return BitConverter.ToUInt16(sub, 0);
}
public static short Byte2Int16(byte[] bytes, int startBit, int length)
{
-
+ if (length < 9 || length > 16) throw new ArgumentException("length should be less then 17 and greater then 8");
var sub = Sub(bytes, startBit, length);
- if (sub.Length == 1)
- {
- return Convert.ToInt16(sub[0]);
- }
return BitConverter.ToInt16(sub, 0);
}
diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
index fee6009..08521d0 100644
--- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
+++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
@@ -91,7 +91,7 @@ public static class ModbusDecoder
}
case ModbusProperty floatProperty:
{
- SetPropertyValue(startRegisterNo, floatProperty, decodeUseBytes);
+ SetPropertyValue(startRegisterNo, floatProperty, decodeUseBytes, true);
// SetPropertyValue(floatProperty.RegisterNo, floatProperty, decodeUseBytes);
break;
}
@@ -112,7 +112,7 @@ public static class ModbusDecoder
return t;
}
- private static void SetPropertyValue(int startRegisterNo, ModbusProperty field, byte[] bytes)
+ private static void SetPropertyValue(int startRegisterNo, ModbusProperty field, byte[] bytes, bool unSign = true)
{
var registerNo = field.RegisterNo;
var start = field.Start;
@@ -134,7 +134,7 @@ public static class ModbusDecoder
_ => length
};
- var value = BitUtls.Bytes2Value(bytes, valueType, start, length, scale, round, offset);
+ var value = BitUtls.Bytes2Value(bytes, valueType, start, length, scale, round, offset, unSign);
field.Value = (T)value;
}
}
\ No newline at end of file