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); }
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 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); }