Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Protocol/FunctionCode.cs

52 lines
2.1 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol;
[Flags]
public enum FunctionCode : byte
{
ReadCoil = 1, ReadCoilError = ReadCoil | Error ,
ReadDiscreteInputs = 2, ReadDiscreteInputsError = ReadDiscreteInputs | Error ,
ReadHoldingRegisters = 3, ReadHoldingRegistersError = ReadHoldingRegisters | Error ,
ReadInputRegisters = 4, ReadInputRegistersError = ReadInputRegisters | Error ,
WriteSingleCoil = 5, WriteSingleCoilError = WriteSingleCoil | Error ,
WriteSingleRegister = 6, WriteSingleRegisterError = WriteSingleRegister | Error ,
WriteMultipleCoils = 15, WriteMultipleCoilsError = WriteMultipleCoils | Error ,
WriteMultipleRegisters = 16, WriteMultipleRegistersError = WriteMultipleRegisters | Error ,
ReadWriteMultipleRegisters = 23, ReadWriteMultipleRegistersError = ReadWriteMultipleRegisters | Error ,
Error = 128
}
public static class FunctionCodeExtensions
{
public static Boolean IsWriteOnly(this FunctionCode fc)
{
return fc == FunctionCode.WriteSingleCoil ||
fc == FunctionCode.WriteSingleRegister ||
fc == FunctionCode.WriteMultipleCoils ||
fc == FunctionCode.WriteMultipleRegisters;
}
public static Boolean IsMultiWrite(this FunctionCode fc)
{
return fc == FunctionCode.WriteMultipleCoils ||
fc == FunctionCode.WriteMultipleRegisters ||
fc == FunctionCode.ReadWriteMultipleRegisters;
}
public static Boolean IsError(this FunctionCode fc)
{
return fc >= FunctionCode.Error;
}
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
public static FunctionCode Error(this FunctionCode fc)
{
return fc | FunctionCode.Error;
}
}