private void button_Receive_Data_Click(object sender, EventArgs e)
        {
            groupBox_Serial_Transmit.Enabled = false;

            string Port_Name    = comboBox_Available_SerialPorts.SelectedItem.ToString();
            int    Baud_Rate    = Convert.ToInt32(comboBox_Standard_Baudrate.SelectedItem);
            string ReceivedData = "";

            SerialPort COMport = new SerialPort(Port_Name, Baud_Rate);

            COMport.ReadTimeout = 3500;

            try
            {
                COMport.Open();
            }
            catch (UnauthorizedAccessException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                textBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();
            }
            catch (System.IO.IOException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                textBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();
            }
            catch (InvalidOperationException SerialException)
            {
                MessageBox.Show(SerialException.ToString());
                textBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();
            }
            catch
            {
                MessageBox.Show("ERROR in Opening Serial PORT -- UnKnown ERROR");
                COMport.Close();
            }

            try
            {
                if (COMport.IsOpen == true)
                {
                    textBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;

                    ReceivedData = COMport.ReadLine();

                    textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + Environment.NewLine + "Waiting for Data";

                    textBox_Receive_Data.Text = ReceivedData;

                    COMport.Close();

                    textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + ReceivedData + " Received from Port";

                    groupBox_Serial_Transmit.Enabled = true;
                }
                else
                {
                    groupBox_Serial_Transmit.Enabled = true;
                    MessageBox.Show("Unable to Write to COM port ");
                    groupBox_Serial_Transmit.Enabled = true;
                    COMport.Close();
                }
            }
            catch (TimeoutException SerialTimeOutException)
            {
                MessageBox.Show(COMport.ReadTimeout.ToString() + " milliSeconds Passed" + Environment.NewLine + "Operation Timed Out");
                MessageBox.Show(SerialTimeOutException.ToString());

                textBox_System_Log.Text = COMport.ReadTimeout.ToString() + " milliSeconds Passed" + Environment.NewLine + "Operation Timed Out";
                textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + SerialTimeOutException.ToString();

                COMport.Close();

                groupBox_Serial_Transmit.Enabled = true;
            }
        }
Esempio n. 2
0
        //Used for Receiving Data from Serial Port
        private void Button_Receive_Data_Click(object sender, EventArgs e)
        {
            GroupBox_Serial_Transmit.Enabled = false; // Disable the Transmit Groupbox
            #region
            //Local Variables
            string Port_Name    = ComboBox_Available_SerialPorts.SelectedItem.ToString();    // PortName
            int    Baud_Rate    = Convert.ToInt32(ComboBox_Standard_Baudrates.SelectedItem); // BaudRate
            string ReceivedData = "";                                                        //                                                    // String Containing Received Data


            SerialPort COMport = new SerialPort(Port_Name, Baud_Rate);
            #endregion
            COMport.ReadTimeout = 3500; //Setting ReadTimeout =3500 ms or 3.5 seconds

            //Open the Serial Port
            #region
            try
            {
                COMport.Open();
            }
            catch (UnauthorizedAccessException SerialException) //exception that is thrown when the operating system denies access
            {
                MessageBox.Show(SerialException.ToString());
                TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();                                  // Close the Port
            }

            catch (System.IO.IOException SerialException)     // An attempt to set the state of the underlying port failed
            {
                MessageBox.Show(SerialException.ToString());
                TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();                                  // Close the Port
            }

            catch (InvalidOperationException SerialException) // The specified port on the current instance of the SerialPort is already open
            {
                MessageBox.Show(SerialException.ToString());
                TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + SerialException.ToString();
                COMport.Close();                                  // Close the Port
            }


            catch //Any other ERROR
            {
                MessageBox.Show("ERROR in Opening Serial PORT -- UnKnown ERROR");
                COMport.Close();                                  // Close the Port
            }
            #endregion

            try
            {
                //If we are able to open the port
                if (COMport.IsOpen == true)
                {
                    //MessageBox.Show("Port Opened");
                    TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;

                    ReceivedData = COMport.ReadLine();                // Wait for data reception
                    #region
                    TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + Environment.NewLine + "Waiting for Data";

                    TextBox_received_Data.Text = ReceivedData;

                    COMport.Close();                                  // Close the Port

                    TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + ReceivedData + "  Received from Port";
                    #endregion
                    GroupBox_Serial_Transmit.Enabled = true; // Enable the Transmit Groupbox
                }
                else
                {
                    GroupBox_Serial_Transmit.Enabled = true; // Enable the Transmit Groupbox
                    MessageBox.Show("Unable to Write to COM port ");
                    GroupBox_Serial_Transmit.Enabled = true; // Enable the Transmit Groupbox
                    COMport.Close();                         // Close the Port
                }
            }

            // Only catch timeout exceptions.
            catch (TimeoutException SerialTimeOutException)
            {
                #region
                // Show in a Message Box
                MessageBox.Show(COMport.ReadTimeout.ToString() + " milliSeconds Passed" + Environment.NewLine + "Operation Timed Out");
                MessageBox.Show(SerialTimeOutException.ToString());

                //Show in Log TextBox
                TextBox_System_Log.Text = COMport.ReadTimeout.ToString() + " milliSeconds Passed" + Environment.NewLine + "Operation Timed Out";
                TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + SerialTimeOutException.ToString();
                #endregion
                COMport.Close();                         // Close the Port

                GroupBox_Serial_Transmit.Enabled = true; // Enable the Transmit Groupbox
            }
        }