Exemplo n.º 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 */
            }
        }
Exemplo n.º 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 */
        }
Exemplo n.º 3
0
        private void DataSend_ExportBtn_Click(object sender, EventArgs e)
        {
            /* --------------------- Create Variable --------------------- */

            Int32 item_counter = SerialPort_SendDataListBox.Items.Count; /* Variable for count items */

            /* --------------------- Create Object --------------------- */

            SaveFileDialog file_info = new SaveFileDialog(); /* Create object for get directory addres - "folderaddresopen" is the name of object */

            /* --------------------- Object config --------------------- */

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

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

            if (file_info.ShowDialog() == DialogResult.OK) /* Check status */
            {
                /* --------------------- Create Object --------------------- */

                TextWriter file = new StreamWriter(file_info.OpenFile()); /* Object for create and write in txt file */

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

                for (int i = 0; i < item_counter; i++)                              /* Loop for write to txt file */
                {
                    file.WriteLine(SerialPort_SendDataListBox.Items[i].ToString()); /* Write items to txt file */
                }

                file.Close(); /* Close the TextWriter Object(file) */

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

                CustomForm.ShowCustomMessage(Color.FromArgb(247, 247, 247), Properties.Resources.Ok_1, "Exported"); /* Show message */
            }
            else
            {
                CancelForm.ShowCanceledMessage(); /* Show message */
            }

            /* Function end */
        }
Exemplo n.º 4
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 */
     }
 }
Exemplo n.º 5
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 */
        }