Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Reflection/Attributes/ModbusAttribute.cs

32 lines
885 B
C#
Raw Normal View History

2023-05-04 07:32:35 +00:00
using static System.AttributeTargets;
namespace InnovEnergy.Lib.Protocols.Modbus.Reflection.Attributes;
2023-05-04 07:32:35 +00:00
[AttributeUsage(Field | Property)]
public class ModbusAttribute : Attribute
{
2023-05-04 07:32:35 +00:00
public UInt16 Address { get; }
public UInt16 Size { get; }
public TypeCode ModbusType { get; }
protected ModbusAttribute(UInt16 address, TypeCode modbusType)
{
Address = address;
ModbusType = modbusType;
Size = modbusType switch
{
TypeCode.Boolean => 1,
TypeCode.Int16 => 1,
TypeCode.UInt16 => 1,
TypeCode.Int32 => 2,
TypeCode.UInt32 => 2,
TypeCode.Single => 2,
TypeCode.Int64 => 4,
TypeCode.UInt64 => 4,
TypeCode.Double => 4,
_ => throw new ArgumentException(nameof(modbusType))
};
}
}