Beispiel #1
0
 private void Button_Disconnect_Click(object sender, EventArgs e)
 {
     COMport.Close();
     RichTextBox_Log.Text             = RichTextBox_Log.Text + Environment.NewLine + COMport.PortName + " closed";
     GroupBox_SeriaL_Receive.Enabled  = false;
     GroupBox_Serial_Transmit.Enabled = false;
 }
Beispiel #2
0
        private void Button_Connect_Click(object sender, EventArgs e)
        {
            string Port_Name = ComboBox_Available_SerialPorts.SelectedItem.ToString();
            int    Baud_Rate = Convert.ToInt32(ComboBox_Standard_Baudrates.SelectedItem);
            string Data      = "Neki";

            COMport.BaudRate = Baud_Rate;
            COMport.PortName = Port_Name;

            try
            {
                COMport.Open();
            }
            catch (System.IO.IOException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                COMport.Close();
            }
            catch (UnauthorizedAccessException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                COMport.Close();
            }
            catch (InvalidOperationException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                COMport.Close();
            }
            catch
            {
                MessageBox.Show("Unknown ERROR when opening serial port" + COMport);
                COMport.Close();
            }

            if (COMport.IsOpen == true)
            {
                Label_COM_Status.Text            = "Open";
                RichTextBox_Log.Text             = RichTextBox_Log.Text + Environment.NewLine + Port_Name + " opened at " + ComboBox_Standard_Baudrates.SelectedItem.ToString() + " baudrate";
                GroupBox_SeriaL_Receive.Enabled  = true;
                GroupBox_Serial_Transmit.Enabled = true;
            }
            else
            {
                COMport.Close();
                RichTextBox_Log.Text = RichTextBox_Log.Text + Environment.NewLine + Port_Name + " failed to open";
            }
        }