Exemplo n.º 1
0
        public void StartListening()
        {
            _cancelListenerSource = new CancellationTokenSource();
            _listenerTask         = new Task(async() =>
            {
                var running = true;
                while (running)
                {
                    try
                    {
                        var readTask = _reader.ReadAsync(_readBuffer, 0, MAX_BUFFER_SIZE);
                        readTask.Wait(_cancelListenerSource.Token);
                        var bytesRead = await readTask;
                        for (var idx = 0; idx < bytesRead; ++idx)
                        {
                            var ch = _readBuffer[idx];
                            if (_immediateChars.Contains(ch))
                            {
                                ImmediateCommandReceivedEvent?.Invoke(this, ch);
                                _holdingBufferIndex   = 0;
                                _lastMessageDateStamp = DateTime.Now;
                            }
                            else if (ch == '\n')
                            {
                                _lastMessageDateStamp = DateTime.Now;
                                var cmd = new String(_holdingBuffer, 0, _holdingBufferIndex);
                                LineReceivedEvent?.Invoke(this, cmd);
                                _holdingBufferIndex = 0;
                            }
                            else
                            {
                                _holdingBuffer[_holdingBufferIndex++] = _readBuffer[idx];
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        running = false;
                        Cleanup();
                        _isOpen = false;
                    }
                    catch (Exception)
                    {
                        running = false;
                    }
                }
            });

            _listenerTask.Start();
        }
 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
 {
     if (this.StillListening)
     {
         try
         {
             string            line = serialPort1.ReadLine();
             LineReceivedEvent l    = new LineReceivedEvent(LineReceived);
             this.BeginInvoke(l, line);
         }
         catch (Exception exception)
         {
         }
     }
 }
Exemplo n.º 3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            func = new LineReceivedEvent(LineReceived);

            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.Text = comboBox1.Items[comboBox1.Items.Count - 1].ToString();
            }

            udpSENDER = new UdpClient();
            udpSENDER.Connect(ip, Int32.Parse(port));

            lsnr = new UDPListener();
            lsnr.NewMessageReceived += OnReciv;
            lsnr.StartListener(3);
            timer1.Start();
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (this.StillListening)
            {
                try
                {
                    string line = serialPort1.ReadLine();
                    LineReceivedEvent l = new LineReceivedEvent(LineReceived);
                    this.BeginInvoke(l, line);
                }
                catch (Exception exception)
                {

                }
            }
        }
Exemplo n.º 5
0
        private void Form1_Load(object sender, EventArgs e)
        {
            func = new LineReceivedEvent(LineReceived);

            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            if (comboBox1.Items.Count > 0) comboBox1.Text = comboBox1.Items[comboBox1.Items.Count - 1].ToString();

            udpSENDER = new UdpClient();
            udpSENDER.Connect(ip, Int32.Parse(port));

            lsnr = new UDPListener();
            lsnr.NewMessageReceived += OnReciv;
            lsnr.StartListener(3);
            timer1.Start();
        }