/// <summary>
        /// Initializes a new instance of the class <see cref="SerialInputOutput"/>.
        /// </summary>
        public SerialInputOutput(string uartPort, int baudRate = 9600)
        {
            Bus      = new CommunicationsBus(uartPort, baudRate);
            commands = new Queue();


            /*
             * Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
             * {
             *  var onBreak = OnBreak;
             *  if (onBreak != null)
             *      onBreak(this, EventArgs.Empty);
             *
             *  // Surpise, this means DO NOT CANCEL:
             *  e.Cancel = true;
             * };*/
        }
示例#2
0
        void Setup()
        {
            PopulateComPorts();
            BtnDisconnect.Click += (a, b) => {
                if (IsConnect)
                {
                    if (bus != null)
                    {
                        bus.Dispose();
                    }
                    IsConnect      = false;
                    TxtStatus.Text = "Disconnected";
                }
            };
            BtnConnect.Click += (a, b) => {
                try
                {
                    if (bus != null)
                    {
                        bus.Dispose();
                    }
                    if (CmbPort.SelectedIndex > 0 && int.TryParse(TxtBaudRate.Text, out var baud))
                    {
                        bus = new CommunicationsBus(CmbPort.Text, baud);
                        bus.DataReceived += Bus_DataReceived;
                        TxtStatus.Text    = "Connected";
                        IsConnect         = true;
                    }
                    else
                    {
                        IsConnect = false;
                    }
                }
                catch (Exception ex)
                {
                    IsConnect = false;
                    MessageBox.Show("Error happen :" + ex.ToString());
                }
            };
            BtnClear.Click += (a, b) => {
                TxtResponse.Clear();
            };
            BtnSend.Click += (a, b) =>
            {
                if (!IsConnect)
                {
                    TxtStatus.Text = "Please connect to device first";
                    return;
                }
                Console.WriteLine("write:" + TxtMessage.Text);
                if (TxtMessage.Lines.Count() > 0)
                {
                    Task BatchTask = new Task(() =>
                    {
                        var lines = TxtMessage.Lines.ToList();
                        foreach (var line in lines)
                        {
                            bus.WriteLine(line);
                            Thread.Sleep(1000);
                        }
                    });
                    BatchTask.Start();
                }
                else
                {
                    bus.WriteLine(TxtMessage.Text);
                }
                //Thread.Sleep(100);

                /*
                 * var resp = bus.ReadExisting();
                 * if (!string.IsNullOrEmpty(resp))
                 * {
                 *  if (this.InvokeRequired)
                 *  {
                 *      this.Invoke(new MethodInvoker(() => TxtResponse.AppendText(resp + Environment.NewLine)));
                 *  }
                 *  else
                 *  {
                 *      TxtResponse.AppendText(resp + Environment.NewLine);
                 *  }
                 * }*/
            };
        }