Example #1
0
        private void DataSend_SendFileBtn_Click(object sender, EventArgs e)
        {
            /* --------------------- Create Object --------------------- */

            OpenFileDialog text_file = new OpenFileDialog(); /* Object for select text file */

            /* --------------------- Set Object --------------------- */

            text_file.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; /* Set format filter */
            text_file.FilterIndex      = 1;                                             /* Select first index */
            text_file.RestoreDirectory = true;                                          /* Change directory to the previously location before close */

            /* --------------------- Create variable --------------------- */

            string file_data;

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

            if (ComPort_ConnectBtn.Text == "Disconnect")                      /* Check key status */
            {
                if (text_file.ShowDialog() == DialogResult.OK)                /* Check status */
                {
                    TextReader file = new StreamReader(text_file.OpenFile()); /* Object for read txt file */

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

                    for (int number_of_line = 0; number_of_line < File.ReadAllLines(text_file.FileName).Length; number_of_line++) /* Loop for write data */
                    {
                        /* ------------------- Read value from text file ------------------- */

                        file_data = file.ReadLine(); /* Read value from text file */

                        /* ------------------- Show value in list box ------------------- */

                        SerialPort_SendDataListBox.Items.Add(file_data); /* Add value to list box */

                        /* ------------------- Send data to serial ------------------- */

                        if (SendWithCRCheckBox.Checked == true) /* Check status */
                        {
                            file_data += "\r";
                        }
                        if (SendWithLFCheckBox.Checked == true) /* Check status */
                        {
                            file_data += "\n";
                        }

                        SerialPort_Connection.Write(file_data); /* Send data */
                    }

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

                    file.Close(); /* Close the TextReader Object(file) */
                }
            }
            else
            {
                CustomForm.ShowCustomMessage(Color.White, Properties.Resources.Select, "Please Connect to Port"); /* Show message */
            }
        }
Example #2
0
        private void DataSend_SendBtn_Click(object sender, EventArgs e)
        {
            string data;

            if (ComPort_ConnectBtn.Text == "Disconnect") /* Check key status */
            {
                /* ------------------- Option ------------------- */

                DataSend_SendBtn.Visible = false; /* Disable button */

                /* ------------------- Send data to serial ------------------- */
                data = SerialPort_SendDataTxtBox.Text;

                if (SendWithCRCheckBox.Checked == true)
                {
                    data += "\r";
                }
                if (SendWithLFCheckBox.Checked == true)
                {
                    data += "\n";
                }

                try
                {
                    SerialPort_Connection.Write(data); /* Send data to serial port */
                }
                catch (Exception)
                {
                    throw;
                }

                SerialPort_SendDataListBox.Items.Add(SerialPort_SendDataTxtBox.Text); /* Show message sent in list box */
                SerialPort_SendDataTxtBox.Text = string.Empty;                        /* Clear text box */
                SerialPort_SendDataTxtBox.Focus();                                    /* Focus on textbox */

                /* ------------------- Receive data from serial ------------------- */

                SerialPort_Connection.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); /* Event for controll received data */

                /* ------------------- Option ------------------- */

                DataSend_SendBtn.Visible = true; /* Enable button */
            }
            else
            {
                CustomForm.ShowCustomMessage(Color.White, Properties.Resources.Select, "Please Connect to Port"); /* Show message */
            }

            /* Function end */
        }
Example #3
0
        private void Panel_ExitThinButton_Click(object sender, EventArgs e)
        {
            if (ComPort_ConnectBtn.Text == "Disconnect") /* Check key status */
            {
                SerialPort_Connection.Close();           /* Close port */

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

                ComPort_ScanBtn.Enabled  = true; /* Enable scan btn */
                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 */
            }

            ExitForm.ShowExitMessage(); /* Show exit message */
        }
Example #4
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 */
        }