private void HandleSentences(object state) { var existingData = ((SerialPort)serialPort).ReadExisting(); var sentences = existingData.Split("$"); foreach (var sentence in sentences) { var _sentence = sentence.Replace("\r\n", string.Empty); if (string.IsNullOrEmpty(sentence)) { continue; } var message = Get(sentence); OnSentenceReceived?.Invoke(message); } }
public async Task Start() { var token = _cancellationTokenSource.Token; await Task.Factory.StartNew(async() => { var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, _port)); while (true) { var msg = await udpClient.ReceiveAsync(); OnSentenceReceived?.Invoke(this, Encoding.UTF8.GetString(msg.Buffer)); if (token.IsCancellationRequested) { udpClient.Close(); udpClient.Dispose(); break; } } }, token); }