You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.4 KiB

using HybirdFrameworkCore.Const;
using HybirdFrameworkCore.Utils;
8 months ago
using log4net;
namespace HybirdFrameworkDriver.ModbusTcpMaster;
/// <summary>
///
/// </summary>
8 months ago
public static class ModbusDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
6 months ago
public static T Decode<T>(byte[] bytes) where T : class, new()
{
T t = new T();
return Decode<T>(bytes, t);
}
public static T Decode<T>(byte[] bytes, T t, EndingConst.ByteSeq byteSeq = EndingConst.ByteSeq.AB, EndingConst.WordSeq wordSeq = EndingConst.WordSeq.CD) where T : class, new()
8 months ago
{
7 months ago
var fields = t.GetType().GetProperties()
8 months ago
.Where(it => it.PropertyType.GetGenericTypeDefinition() == typeof(ModbusProperty<>))
.Select(p => p.GetValue(t)).ToList();
//startRegisterNo 获得整个对象的起始寄存器数值
7 months ago
var startRegisterNo = int.MaxValue;
foreach (var field in fields)
8 months ago
if (field != null)
{
7 months ago
var p = (IModbusProperty)field;
if (startRegisterNo > p.GetRegisterNo()) startRegisterNo = p.GetRegisterNo();
8 months ago
}
7 months ago
var decodeUseBytes = new byte[bytes.Length];
if (byteSeq == EndingConst.ByteSeq.AB)
{
decodeUseBytes = BitUtls.ReverseByteSeq(bytes);
}
if (wordSeq == EndingConst.WordSeq.CD)
{
decodeUseBytes = BitUtls.ReverseWordSeq(decodeUseBytes);
}
7 months ago
foreach (var field in fields)
8 months ago
switch (field)
{
case ModbusProperty<bool> boolProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, boolProperty, decodeUseBytes);
//SetPropertyValue(boolProperty.RegisterNo, boolProperty, decodeUseBytes);
break;
}
case ModbusProperty<byte> byteProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, byteProperty, decodeUseBytes);
//SetPropertyValue(byteProperty.RegisterNo, byteProperty, decodeUseBytes);
break;
}
case ModbusProperty<sbyte> sbyteProperty:
7 months ago
{
//SetPropertyValue(sbyteProperty.RegisterNo, sbyteProperty, decodeUseBytes);
SetPropertyValue(startRegisterNo, sbyteProperty, decodeUseBytes);
break;
}
case ModbusProperty<short> shortProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, shortProperty, decodeUseBytes);
//SetPropertyValue(shortProperty.RegisterNo, shortProperty, decodeUseBytes);
break;
}
case ModbusProperty<ushort> ushortProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, ushortProperty, decodeUseBytes);
// SetPropertyValue(ushortProperty.RegisterNo, ushortProperty, decodeUseBytes);
break;
}
case ModbusProperty<int> intProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, intProperty, decodeUseBytes);
// SetPropertyValue(intProperty.RegisterNo, intProperty, decodeUseBytes);
break;
}
case ModbusProperty<uint> uintProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, uintProperty, decodeUseBytes);
// SetPropertyValue(uintProperty.RegisterNo, uintProperty, decodeUseBytes);
break;
}
8 months ago
case ModbusProperty<float> floatProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, floatProperty, decodeUseBytes, true);
7 months ago
// SetPropertyValue(floatProperty.RegisterNo, floatProperty, decodeUseBytes);
break;
}
case ModbusProperty<double> doubleProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, doubleProperty, decodeUseBytes);
// SetPropertyValue(doubleProperty.RegisterNo, doubleProperty, decodeUseBytes);
break;
}
case ModbusProperty<string> stringProperty:
7 months ago
{
SetPropertyValue(startRegisterNo, stringProperty, decodeUseBytes);
// SetPropertyValue(stringProperty.RegisterNo, stringProperty, decodeUseBytes);
break;
}
8 months ago
}
7 months ago
8 months ago
return t;
}
private static void SetPropertyValue<T>(int startRegisterNo, ModbusProperty<T> field, byte[] bytes,
bool unSign = true)
8 months ago
{
7 months ago
var registerNo = field.RegisterNo;
var start = field.Start;
var length = field.Length;
var type = field.Type;
var scale = field.Scale;
var round = field.Round;
var offset = field.Offset;
8 months ago
start = (registerNo - startRegisterNo) * 16 + start;
7 months ago
var valueType = typeof(T);
if (typeof(T) == typeof(byte))
length = length * 8;
else
length = type switch
{
ModbusDataType.Register => length * 16,
_ => length
};
7 months ago
var value = BitUtls.Bytes2Value(bytes, valueType, start, length, scale, round, offset, unSign);
8 months ago
field.Value = (T)value;
}
}