Exemple #1
0
        /// <summary>
        /// Start or stop the serial comm
        /// </summary>
        private void StartStop_Click(object sender, EventArgs e)
        {
            StartStop.Enabled = false;
            // If serial started, stop it
            if (_serialStarted)
            {
                // Note that stopped
                _serialStarted = false;
                // Stop serial
                if (_serialComm != null)
                {
                    _serialComm.Stop();
                }
                // Change control
                StartStop.Text      = "Click to Enable Serial";
                StartStop.BackColor = Color.LightCoral;
            }
            // If serial stopped, start it
            else
            {
                var selectedComPort  = SerialPortList.SelectedItem.ToString().ToUpper();
                var comPortNumberStr = selectedComPort.Replace("COM", string.Empty);
                int comPortNumber;
                var retVal = int.TryParse(comPortNumberStr, out comPortNumber);
                Console.Write("console is " + comPortNumberStr);

                /**
                 *              if (!retVal) {
                 *                      MessageBox.Show(string.Format("Cannot get port number from selected item {0}", selectedComPort), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 *                      return;
                 *              }
                 *              if (comPortNumber > MaxSerialPortNumber) {
                 *                      MessageBox.Show(string.Format("Port numbers above {0} probably can't be opened", MaxSerialPortNumber), "Warning", MessageBoxButtons.OK,
                 *                              MessageBoxIcon.Warning);
                 *              }
                 */
                var portName = selectedComPort;
                _serialComm = new SerialComm(portName, ProcessInput);
                // Try to start. If cannot open, give error message.
                if (!_serialComm.Start())
                {
                    ErrorMessages.AppendText("Cannot open serial port " + portName + "\n");
                    StartStop.Enabled = true;
                    return;
                }
                // Note that started and change control
                _serialStarted      = true;
                StartStop.Text      = "Click to Disable Serial";
                StartStop.BackColor = Color.YellowGreen;
            }
            StartStop.Enabled = true;
        }