コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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);
        }