Пример #1
0
        private void ComPort_ConnectBtn_Click(object sender, EventArgs e)
        {
            if (ComPort_ComboBox.SelectedItem != null)    /* Check COM Port */
            {
                if (ComPort_ConnectBtn.Text == "Connect") /* Check key status */
                {
                    ComPort_ScanBtn.Enabled = false;      /* Disable scan btn */

                    /* ----------------------------------- */

                    SerialPort_Connection.PortName = ComPort_ComboBox.SelectedItem.ToString(); /* Get port name */

                    try
                    {
                        SerialPort_Connection.Open(); /* open port for connect */

                        /* ----------------------------------- */

                        ComPort_ComboBox.Enabled = false; /* Disable combobox */

                        /* ----------------------------------- */

                        ComPort_ConnectBtn.Activecolor  = Color.FromArgb(255, 195, 75); /* Set color */
                        ComPort_ConnectBtn.Normalcolor  = Color.FromArgb(255, 195, 75); /* Set color */
                        ComPort_ConnectBtn.OnHovercolor = Color.FromArgb(255, 175, 75); /* Set color */
                        ComPort_ConnectBtn.Text         = "Disconnect";                 /* Change button text */
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message); /* Show error message */
                        SerialPort_Connection.Close();  /* Close port */
                        ComPort_ComboBox.Focus();       /* Focus on combobox */

                        ComPort_ScanBtn.Enabled = true; /* Enable scan btn */

                        /* ----------------------------------- */

                        SerialPort_Connection.Close(); /* Close port */

                        /* ----------------------------------- */

                        ComPort_ComboBox.Enabled = true; /* Enable combobox */

                        /* ----------------------------------- */

                        ComPort_ConnectBtn.Activecolor  = Color.FromArgb(0, 195, 175); /* Set color */
                        ComPort_ConnectBtn.Normalcolor  = Color.FromArgb(0, 195, 175); /* Set color */
                        ComPort_ConnectBtn.OnHovercolor = Color.FromArgb(0, 175, 175); /* Set color */
                        ComPort_ConnectBtn.Text         = "Connect";                   /* Change button text */
                    }
                }

                else if (ComPort_ConnectBtn.Text == "Disconnect") /* Check key status */
                {
                    ComPort_ScanBtn.Enabled = true;               /* Enable scan btn */

                    /* ----------------------------------- */

                    SerialPort_Connection.Close(); /* Close port */

                    /* ----------------------------------- */

                    ComPort_ComboBox.Enabled = true; /* Enable combobox */

                    /* ----------------------------------- */

                    ComPort_ConnectBtn.Activecolor  = Color.FromArgb(0, 195, 175); /* Set color */
                    ComPort_ConnectBtn.Normalcolor  = Color.FromArgb(0, 195, 175); /* Set color */
                    ComPort_ConnectBtn.OnHovercolor = Color.FromArgb(0, 175, 175); /* Set color */
                    ComPort_ConnectBtn.Text         = "Connect";                   /* Change button text */
                }

                else /* Needed */
                {
                    /* Non instruction */
                }
            }
            else
            {
                CustomForm.ShowCustomMessage(Color.White, Properties.Resources.Select, "Please Select COM Port"); /* Show message */
            }

            /* Function end */
        }
Пример #2
0
 private void BaudRate_Changed(object sender, EventArgs e)
 {
     try
     {
         if (BaudRate_600RadioButton.Checked == true)         /* Check status */
         {
             SerialPort_Connection.BaudRate = 600;            /* Set baud rate */
         }
         else if (BaudRate_1200RadioButton.Checked == true)   /* Check status */
         {
             SerialPort_Connection.BaudRate = 1200;           /* Set baud rate */
         }
         else if (BaudRate_2400RadioButton.Checked == true)   /* Check status */
         {
             SerialPort_Connection.BaudRate = 2400;           /* Set baud rate */
         }
         else if (BaudRate_4800RadioButton.Checked == true)   /* Check status */
         {
             SerialPort_Connection.BaudRate = 4800;           /* Set baud rate */
         }
         else if (BaudRate_9600RadioButton.Checked == true)   /* Check status */
         {
             SerialPort_Connection.BaudRate = 9600;           /* Set baud rate */
         }
         else if (BaudRate_14400RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 14400;          /* Set baud rate */
         }
         else if (BaudRate_19200RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 19200;          /* Set baud rate */
         }
         else if (BaudRate_28800RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 28800;          /* Set baud rate */
         }
         else if (BaudRate_38400RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 38400;          /* Set baud rate */
         }
         else if (BaudRate_56000RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 56000;          /* Set baud rate */
         }
         else if (BaudRate_57600RadioButton.Checked == true)  /* Check status */
         {
             SerialPort_Connection.BaudRate = 57600;          /* Set baud rate */
         }
         else if (BaudRate_115200RadioButton.Checked == true) /* Check status */
         {
             SerialPort_Connection.BaudRate = 115200;         /* Set baud rate */
         }
         else if (BaudRate_128000RadioButton.Checked == true) /* Check status */
         {
             SerialPort_Connection.BaudRate = 128000;         /* Set baud rate */
         }
         else if (BaudRate_256000RadioButton.Checked == true) /* Check status */
         {
             SerialPort_Connection.BaudRate = 256000;         /* Set baud rate */
         }
         else
         {
             /* No instruction */
         }
     }
     catch (Exception)
     {
         CustomForm.ShowCustomMessage(Color.White, Properties.Resources.Loading_15, "Baud rate is not available for this port"); /* Show message */
         BaudRate_9600RadioButton.Checked = true;                                                                                /* Select button */
         SerialPort_Connection.BaudRate   = 9600;                                                                                /* Set baud rate */
     }
 }
        public static void ShowCustomMessage(System.Drawing.Color color, System.Drawing.Image image, string message)
        {
            CustomForm CustomMessage = new CustomForm(color, image, message); /* Create object */

            CustomMessage.ShowDialog();                                       /* Show message */
        }