internal override ModbusResponse DeserializeResponse(ResponseBuffer buffer, ModbusRequest request, int timeout) { ModbusResponse result = null; while (result == null || result is ModbusCommErrorResponse responseCommErrorMessage && responseCommErrorMessage.ErrorCode != ModbusCommErrorCode.ResponseTimeout) { if (buffer.Count > 0) { errorBuffer.Add(buffer[0]); buffer.RemoveAt(0); } buffer.Read(timeout); try { while (buffer[0] != 0x3a) { errorBuffer.Add(buffer[0]); buffer.RemoveAt(0); buffer.Read(timeout); } } catch { return(new ModbusCommErrorResponse(ModbusCommErrorCode.ResponseAsciiStartError, errorBuffer.Concat(buffer), request)); } result = base.DeserializeResponse(buffer, request, timeout); } if (result is ModbusCommErrorResponse responseCommError) { result = new ModbusCommErrorResponse(responseCommError.ErrorCode, errorBuffer.Concat(responseCommError.ReceivedBytes), request); } else { var asciiEnd = buffer.Read(2, timeout); if (!asciiEnd.SequenceEqual(new byte[] { 13, 10 })) { return(new ModbusCommErrorResponse(ModbusCommErrorCode.ResponseAsciiEndError, buffer, request)); } if (errorBuffer.Count > 0) { RaiseUnrecognized(buffer.Channel, errorBuffer.ToArray()); errorBuffer.Clear(); } } return(result); }
private bool IsErrorLRC(ResponseBuffer buffer, int messageLength, ModbusRequest request, int timeout, out ModbusResponse responseMessage) { byte lrc = 0; foreach (var b in Read(buffer, 0, messageLength, timeout)) { lrc += b; } lrc = (byte)(-lrc & 0xff); if (lrc != Read(buffer, messageLength, timeout)) { responseMessage = new ModbusCommErrorResponse(ModbusCommErrorCode.ErrorLRC, buffer, request); return(true); } else { responseMessage = null; return(false); } }
internal override ModbusResponse DeserializeResponse(ResponseBuffer buffer, ModbusRequest request, int timeout) { ModbusResponse result = base.DeserializeResponse(buffer, request, timeout); while (result is ModbusCommErrorResponse responseCommErrorMessage && responseCommErrorMessage.ErrorCode != ModbusCommErrorCode.ResponseTimeout) { errorBuffer.Add(buffer[0]); buffer.RemoveAt(0); result = base.DeserializeResponse(buffer, request, timeout); } if (result is ModbusCommErrorResponse responseCommError) { result = new ModbusCommErrorResponse(responseCommError.ErrorCode, errorBuffer.Concat(responseCommError.ReceivedBytes), request); } else if (errorBuffer.Count > 0) { RaiseUnrecognized(buffer.Channel, errorBuffer.ToArray()); errorBuffer.Clear(); } return(result); }