Exemplo n.º 1
0
 private void ConnectButton_Click(object sender, EventArgs e)
 {
     try{
         if (ConnectButton.Text == "Connect")
         {
             CommunicationPort.PortName = SerialPortComboBox.SelectedItem as string;
             CommunicationPort.BaudRate = 57600;
             CommunicationPort.Open();
             log.Info("Open Com Port: " + CommunicationPort.PortName + " with baudrate " + CommunicationPort.BaudRate.ToString());
             ConnectButton.Text = "Disconnect";
         }
         else
         {
             if (CommunicationPort.IsOpen)
             {
                 CommunicationPort.Close();
             }
             ConnectButton.Text = "Connect";
         }
     }
     catch (Exception ex)
     {
         log.Error("Could not connect!", ex);
     }
 }
Exemplo n.º 2
0
        public void Close()
        {
            lock (CommunicationPortLock)
            {
                //Make sure the port is not already closed.
                if (!CommunicationPort.IsConnected)
                {
                    return;
                }

                //Make sure we are not currently updating the state and
                //stop the timer so we don't try to update the state after we close the port.
                while (RefreshInProgress)
                {
                    ManualResetEvent manualEvent = new ManualResetEvent(false);
                    manualEvent.WaitOne(100);
                    manualEvent.Dispose();
                }
                StopUpdateTimer();

                //Close the serial port.
                Log(LogLevel.Information, "Closing communicaton port");
                CommunicationPort.Close();

                //Make sure the current state is cleared.
                CurrentState.Reset();
                OnStateChanged();
            }
        }