using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors; namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames; using Fc = FunctionCode; public class ModbusFrame { internal const Int32 MinSize = 2; public ArraySegment Data { get; } public MbByte SlaveAddress => Data.ByteAt(0); public MbByte FunctionCode => Data.ByteAt(1); internal ModbusFrame(Int32 size) { Data = new Byte[size]; } internal ModbusFrame(ArraySegment data) { if (data.Count < MinSize) throw new ArgumentException(nameof(data)); Data = data; } protected void AssertFunctionCode(FunctionCode expectedFc) { var actualFc = FunctionCode.Get(); if (actualFc == (expectedFc | Fc.Error)) throw new ErrorResponseException(ExceptionCode.IllegalDataValue, actualFc); // TODO parse exception code if (actualFc != expectedFc) throw new UnexpectedResponseFieldException(field: nameof(FunctionCode), expected: expectedFc.ToString(), actual: Enum.GetName(typeof(FunctionCode), actualFc)!); } public void AssertSlaveAddress(Byte expectedSlaveAddress) { var actualSlaveAddress = SlaveAddress.Get(); if (actualSlaveAddress != expectedSlaveAddress) throw new UnexpectedResponseFieldException(field: nameof(SlaveAddress), expected: expectedSlaveAddress, actual: actualSlaveAddress); } }