private bool TryWriteCommandAndReadResponse(IQcdmCommandRequest request, byte[] data, bool logCommands, ref IQcdmCommandResponse response) { try { _port.Write(data); var doRead = true; var counter = 30; while (doRead) { var responseData = _port.Read(); if (logCommands) { LogCommandResponseDebug(request.Command, responseData); } response = CreateResponse(responseData); doRead = response.Command != request.Command && response.Command != QcdmCommand.BadCmd && response.Command != QcdmCommand.BadLen && response.Command != QcdmCommand.BadParm; if (doRead) { ProcessLogResponse(_logger, response); } --counter; if (counter <= 0) { throw new QcdmManagerException(Strings.QcdmManyLogLines); } } return(true); } catch (HdlcEncoderException ex) { if (ex.IsInvalidCrc) { return(false); } throw ex; } }
public IQcdmCommandResponse ExecuteQcdmCommandRequest(IQcdmCommandRequest request) { if (!IsOpen) { throw new QcdmManagerException(Strings.QcdmSerialPortIsNotOpen); } var logCommands = false; //Debugger.IsAttached; var data = request.GetData(); if (logCommands) { LogCommandRequestDebug(request.Command, data); } _port.Write(data); var doRead = true; IQcdmCommandResponse response = null; var counter = 0; while (doRead) { var responseData = _port.Read(); if (logCommands) { LogCommandResponseDebug(request.Command, responseData); } response = CreateResponse(responseData); doRead = response.Command != request.Command; ++counter; if (counter > 15) { throw new QcdmManagerException(Strings.QcdmManyLogLines); } } return(response); }