Beispiel #1
0
        private void ConnectSerial()
        {
            if (_serial != null)
            {
                throw new Exception("Must handle exception");
            }

            _serial = new Serial(comboBox1.SelectedItem as string, 9600);
            _serial.DataRead += (s, e) =>
            {
                lock (_readDisplay)
                {
                    _readDisplay.Append(System.Text.Encoding.UTF8.GetString(e.ReadBytes));
                }
                this.Invoke((Action)UpdateDisplay);
            };

            _serial.PortError += (s, e) =>
            {
                lock (_readDisplay)
                {
                    _readDisplay.AppendLine("Exception on read: " + e.Exception.Message);
                }
                this.Invoke((Action)UpdateDisplay);
            };

            _serial.Open();
        }