protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { //Transaction Identifier + Protocol Identifier + Length + Unit Identifier + Function Code if (input.Capacity < 2 + 2 + 2 + 1 + 1) { return; } ModbusHeader header = new ModbusHeader(input); short functionCode = input.ReadByte(); ModbusFunction function = null; if (Enum.IsDefined(typeof(ModbusCommand), functionCode)) { var command = Enum.GetName(typeof(ModbusCommand), functionCode); function = (ModbusFunction)Activator.CreateInstance(Type.GetType(string.Format(typeName, isServerMode ? "Request" : "Response", command))); } if (functionCode >= maxFunctionCode) { function = new ExceptionFunction(functionCode); } else if (function == null) { function = new ExceptionFunction(functionCode, 0x01); } function.Decode(input); ModbusFrame frame = new ModbusFrame(header, function); output.Add(frame); }
public override void FromNetworkBuffer(ModbusHeader header, byte[] modbusResponse) { StandardResponseFromNetworkBuffer(header, modbusResponse); ReferenceNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(modbusResponse, 1)); WordCount = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(modbusResponse, 3)); }
public override void FromNetworkBuffer(ModbusHeader header, byte[] modbusResponse) { StandardResponseFromNetworkBuffer(header, modbusResponse); Length = modbusResponse[1]; int len = Length; Data = new byte[len]; Buffer.BlockCopy(modbusResponse, 2, Data, 0, len); }
public override void FromNetworkBuffer(byte[] buffer) { var idx = 0; Header = ModbusHeader.FromNetworkBuffer(buffer); idx += ModbusHeader.FixedLength; UnitIdentifier = buffer[idx++]; FunctionCode = buffer[idx++]; Length = buffer[idx++]; Data = new byte[Length]; Buffer.BlockCopy(buffer, idx, Data, 0, Length); }
protected void StandardResponseFromNetworkBuffer(ModbusHeader header, byte[] modbusResponse) { Header = header; FunctionCode = modbusResponse[0]; if (FunctionCode >= 0x80) { var exceptionCode = modbusResponse[1]; var orginalFunctionCode = (byte)(FunctionCode - ModbusErrorOffset); throw new ModbusReplyException(orginalFunctionCode, exceptionCode); } }
private T ReadResponse <T>() where T : ModbusResponseBase { var headerBytes = ReadFromBuffer(ModbusHeader.FixedLength); var header = ModbusHeader.FromNetworkBuffer(headerBytes); var dataBytes = ReadFromBuffer(header.Length); var fullBuffer = headerBytes.Concat(dataBytes).ToArray(); var response = Activator.CreateInstance <T>(); response.FromNetworkBuffer(fullBuffer); return(response); }
public ushort CallModbusFunction(ModbusFunction function) { if (ConnectionState.Connected != connectionState || Channel == null) { throw new Exception("Not connected!"); } SetTransactionIdentifier(); ModbusHeader header = new ModbusHeader(transactionIdentifier, UnitIdentifier); ModbusFrame frame = new ModbusFrame(header, function); Channel.WriteAndFlushAsync(frame); return(transactionIdentifier); }
public override void FromNetworkBuffer(byte[] buffer) { var idx = 0; Header = ModbusHeader.FromNetworkBuffer(buffer); idx += ModbusHeader.FixedLength; UnitIdentifier = buffer[idx++]; FunctionCode = buffer[idx++]; ReferenceNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, idx)); idx += 2; WordCount = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, idx)); idx += 2; }
protected int StandardResponseFromNetworkBuffer(byte[] buffer) { var idx = 0; Header = ModbusHeader.FromNetworkBuffer(buffer); idx += ModbusHeader.FixedLength; UnitIdentifier = buffer[idx++]; FunctionCode = buffer[idx++]; if (FunctionCode >= 0x80) { var exceptionCode = buffer[idx]; var orginalFunctionCode = (byte)(FunctionCode - ModbusErrorOffset); throw new ModbusReplyException(orginalFunctionCode, exceptionCode); } return(idx); }
private T ReadResponse <T>() where T : ModbusResponseBase { var headerBytes = ReadFromBuffer(ModbusHeader.FixedLength); var header = ModbusHeader.FromNetworkBuffer(headerBytes); var dataBytes = ReadFromBuffer(header.Length - 1); //Ommit Unit Identifier from MBAP Header var response = Activator.CreateInstance <T>(); try { response.FromNetworkBuffer(header, dataBytes); } catch (ModbusReplyException mre) { throw new ModbusReplyException(mre, $"{ ipAddress }:{ port }"); } return(response); }
public abstract void FromNetworkBuffer(ModbusHeader header, byte[] modbusResponse);