private void TryReceiveData() { var numBytesRead = 0; var data = new byte[ReceiverFraming.FrameLength]; try { if (_streamResource.BytesToRead() == 0) { Thread.Sleep(CommunicationTasks.ReceivingTaskSleepTime); } while (numBytesRead != ReceiverFraming.FrameLength) { numBytesRead += _streamResource.Read(data, numBytesRead, ReceiverFraming.FrameLength - numBytesRead); } } catch (Exception e) { Logger.Error("Receiver task exception: " + e.Message); Stop(); // probably the serial port is not opened or not created, there is no reason to continue this task } DataReceived?.Invoke(this, new RobotDataReceivedEventArgs { Data = data }); }