|
|
@ -1,4 +1,6 @@
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
using HslCommunication.Core;
|
|
|
|
|
|
|
|
using HslCommunication.Secs.Types;
|
|
|
|
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
using log4net;
|
|
|
|
using log4net;
|
|
|
|
|
|
|
|
|
|
|
|
namespace HybirdFrameworkDriver.ModbusTcpMaster;
|
|
|
|
namespace HybirdFrameworkDriver.ModbusTcpMaster;
|
|
|
@ -7,13 +9,12 @@ public static class ModbusDecoder
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
|
|
|
|
|
|
|
|
|
|
|
|
public static T Decode<T>(byte[] bytes) where T : class, new()
|
|
|
|
public static T Decode<T>(byte[] bytes, T t) where T : class, new()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
T t = new T();
|
|
|
|
|
|
|
|
List<object?> fields = t.GetType().GetProperties()
|
|
|
|
List<object?> fields = t.GetType().GetProperties()
|
|
|
|
.Where(it => it.PropertyType.GetGenericTypeDefinition() == typeof(ModbusProperty<>))
|
|
|
|
.Where(it => it.PropertyType.GetGenericTypeDefinition() == typeof(ModbusProperty<>))
|
|
|
|
.Select(p => p.GetValue(t)).ToList();
|
|
|
|
.Select(p => p.GetValue(t)).ToList();
|
|
|
|
|
|
|
|
//startRegisterNo 获得整个对象的起始寄存器数值
|
|
|
|
int startRegisterNo = Int32.MaxValue;
|
|
|
|
int startRegisterNo = Int32.MaxValue;
|
|
|
|
foreach (object? field in fields)
|
|
|
|
foreach (object? field in fields)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -26,29 +27,95 @@ public static class ModbusDecoder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] decodeUseBytes = new byte[bytes.Length];
|
|
|
|
|
|
|
|
switch (ModbusTcpMaster.DataFormat)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case DataFormat.ABCD:
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (i % 2 == 0)
|
|
|
|
|
|
|
|
decodeUseBytes[i + 1] = bytes[i];
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
decodeUseBytes[i - 1] = bytes[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DataFormat.BADC:
|
|
|
|
|
|
|
|
decodeUseBytes = bytes;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
foreach (object? field in fields)
|
|
|
|
foreach (object? field in fields)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
switch (field)
|
|
|
|
switch (field)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case ModbusProperty<byte> property:
|
|
|
|
case ModbusProperty<bool> boolProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, boolProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
//SetPropertyValue(boolProperty.RegisterNo, boolProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<byte> byteProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, byteProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
//SetPropertyValue(byteProperty.RegisterNo, byteProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<sbyte> sbyteProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//SetPropertyValue(sbyteProperty.RegisterNo, sbyteProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, sbyteProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<short> shortProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, shortProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
//SetPropertyValue(shortProperty.RegisterNo, shortProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<ushort> ushortProperty:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SetPropertyValue(startRegisterNo, property, bytes);
|
|
|
|
SetPropertyValue(startRegisterNo, ushortProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(ushortProperty.RegisterNo, ushortProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<int> intProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, intProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(intProperty.RegisterNo, intProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<uint> uintProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, uintProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(uintProperty.RegisterNo, uintProperty, decodeUseBytes);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ModbusProperty<float> floatProperty:
|
|
|
|
case ModbusProperty<float> floatProperty:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SetPropertyValue(startRegisterNo, floatProperty, bytes);
|
|
|
|
SetPropertyValue(startRegisterNo, floatProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(floatProperty.RegisterNo, floatProperty, decodeUseBytes);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<double> doubleProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, doubleProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(doubleProperty.RegisterNo, doubleProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ModbusProperty<string> stringProperty:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetPropertyValue(startRegisterNo, stringProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
// SetPropertyValue(stringProperty.RegisterNo, stringProperty, decodeUseBytes);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void SetPropertyValue<T>(int startRegisterNo, ModbusProperty<T> field, byte[] bytes)
|
|
|
|
private static void SetPropertyValue<T>(int startRegisterNo, ModbusProperty<T> field, byte[] bytes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
int registerNo = field.RegisterNo;
|
|
|
|
int registerNo = field.RegisterNo;
|
|
|
|
int start = field.Start;
|
|
|
|
int start = field.Start;
|
|
|
|
int length = field.Length;
|
|
|
|
int length = field.Length;
|
|
|
@ -59,15 +126,17 @@ public static class ModbusDecoder
|
|
|
|
|
|
|
|
|
|
|
|
start = (registerNo - startRegisterNo) * 16 + start;
|
|
|
|
start = (registerNo - startRegisterNo) * 16 + start;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Type valueType = typeof(T);
|
|
|
|
|
|
|
|
if (typeof(T) == typeof(byte))
|
|
|
|
|
|
|
|
length = length * 8;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
length = type switch
|
|
|
|
length = type switch
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ModbusDataType.Byte => length * 8,
|
|
|
|
|
|
|
|
ModbusDataType.Register => length * 16,
|
|
|
|
ModbusDataType.Register => length * 16,
|
|
|
|
_ => length
|
|
|
|
_ => length
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
Type valueType = typeof(T);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object value = BitUtls.Bytes2Value(bytes, valueType, start, length, scale, round, offset);
|
|
|
|
object value = BitUtls.Bytes2Value(bytes, valueType, start, length, scale, round, offset);
|
|
|
|
field.Value = (T)value;
|
|
|
|
field.Value = (T)value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|