/// <summary> /// Data processing /// </summary> private void DataHandling() { while (_runingEvent.WaitOne(1)) { try { if (_quReceiveBuff.Count == 0) { SpinWait.SpinUntil(() => _quReceiveBuff.Count > 0, 10); continue; } byte[] data = _dequeueFunc(_quReceiveBuff); if (data == null || data.Length == 0) { SpinWait.SpinUntil(() => false, 10); continue; } if (_reportPredicate(data)) { DataReport?.BeginInvoke(this, new DataReportEventArgs(data), null, null); //If the data is spontaneously reported by the serial port, the DataReport event is called } else { //If the command response returned by the serial port, join the command response queue if (_quCmdRespone.Count > 0) { _quCmdRespone.Clear(); //The queue is cleared to ensure that if a command timed out does not affect subsequent command results } _quCmdRespone.Enqueue(data); _cmdResponseReset.Set(); } } catch (Exception ex) { var errorDescription = $"An error occurred in the data processing: {ex.Message}"; this.DataHandleError?.BeginInvoke(this, new DataHandleErrorEventArgs(errorDescription, new Exception(errorDescription, ex)), null, null); } } }