コード例 #1
0
        //---------------------------------- Serial ----------------------------------------

        private void btnComConnect_Click(object sender, EventArgs e)
        {
            if (!mySerial.IsOpen)
            {
                //Try to open the serial port
                try
                {
                    //serialPort.PortName = CBCom.Items[CBCom.SelectedIndex].ToString();
                    mySerial.PortName  = CBCom.Text;
                    mySerial.BaudRate  = Convert.ToInt32(CBBaud.Text);
                    mySerial.DataBits  = Convert.ToInt32(CBDataBits.Text);
                    mySerial.StopBits  = (StopBits)Enum.Parse(typeof(StopBits), CBStopBits.Text);
                    mySerial.Parity    = (Parity)Enum.Parse(typeof(Parity), CBPairity.Text);
                    mySerial.DtrEnable = ChBxDTR.Checked;
                    mySerial.RtsEnable = ChBxRTS.Checked;
                    mySerial.ReceivedBytesThreshold = 5;
                    mySerial.ReadTimeout            = 2000;
                    mySerial.WriteTimeout           = 1000;

                    mySerial.Open();
                    mySerial.ReadExisting();
                    btnComConnect.Text      = "Disconnect";
                    btnComConnect.BackColor = Color.SpringGreen;
                    //SerialToolStripLabel.Text = "Serial Status: Connected ; " + CBCom.Items[CBCom.SelectedIndex].ToString() +
                    //    ", " + serialPort.BaudRate +
                    //    ", " + serialPort.DataBits +
                    //    ", " + serialPort.StopBits +
                    //    ", " + serialPort.Parity;
                    //UpdateDebug("Serial Connected - " + CBCom.Items[CBCom.SelectedIndex].ToString() + " : " + mySerial.BaudRate + "\n");
                    Console.WriteLine("Serial Connected - " + CBCom.Items[CBCom.SelectedIndex].ToString() + " : " + mySerial.BaudRate + "\n");
                    grBCom.Enabled = false;
                }
                catch (UnauthorizedAccessException SerialException)
                {
                    MessageBox.Show(SerialException.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (System.IO.IOException SerialException)
                {
                    MessageBox.Show(SerialException.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    mySerial.Close();
                }
                catch (InvalidOperationException SerialException)
                {
                    MessageBox.Show(SerialException.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    mySerial.Close();
                }
                catch (Exception xxx)
                {
                    MessageBox.Show("Unkonown Error\n" + xxx.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    mySerial.Close();
                }
            }
            else
            {
                mySerial.Close();
                btnComConnect.Text      = "Connect";
                btnComConnect.BackColor = Color.LightCoral;
                grBCom.Enabled          = true;
            }
        }
コード例 #2
0
        private void button_Transmit_Data_Click(object sender, EventArgs e)
        {
            groupBox_Serial_Receive.Enabled = false;

            string Port_Name = comboBox_Available_SerialPorts.SelectedItem.ToString();
            int    Baud_Rate = Convert.ToInt32(comboBox_Standard_Baudrate.SelectedItem);
            string Data      = textBox_Data_Transmit.Text;

            SerialPort COMport = new SerialPort(Port_Name, Baud_Rate);

            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();
            }

            if (COMport.IsOpen == true)
            {
                COMport.WriteLine(Data);
                COMport.Close();
                groupBox_Serial_Receive.Enabled = true;

                textBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                textBox_System_Log.Text = textBox_System_Log.Text + Environment.NewLine + Data + " Written to Port";
            }
            else
            {
                groupBox_Serial_Receive.Enabled = true;
                MessageBox.Show("Unable to Write to COM port ");
                COMport.Close();
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: gcharles81/NIKON2018
        private void button1_Click(object sender, EventArgs e)
        {
            //Local Variables
            string Port_Name = ComboBox_Available_SerialPorts.SelectedItem.ToString();    // Store the selected COM port name to "Port_Name" varaiable
            int    Baud_Rate = Convert.ToInt32(ComboBox_Standard_Baudrates.SelectedItem); // Convert the string "9600" to int32 9600

            //Store the string in Textbox to variable "Data"

            //SerialPort COMport = new SerialPort(Port_Name, Baud_Rate, Parity.None, 8, StopBits.Two); //Create a new  SerialPort Object (defaullt setting -> 8N1)
            COMport.Encoding  = Encoding.ASCII;
            COMport.PortName  = Port_Name;
            COMport.BaudRate  = Baud_Rate;
            COMport.Parity    = Parity.None;
            COMport.DataBits  = 8;
            COMport.StopBits  = StopBits.Two;
            COMport.DtrEnable = true;
            COMport.RtsEnable = true;

            try
            {
                COMport.Open();
            }
            #region
            catch (UnauthorizedAccessException SerialException) //exception that is thrown when the operating system denies access
            {
                MessageBox.Show(SerialException.ToString());

                COMport.Close();
            }

            catch (System.IO.IOException SerialException)     // An attempt to set the state of the underlying port failed
            {
                MessageBox.Show(SerialException.ToString());

                COMport.Close();
            }

            catch (InvalidOperationException SerialException) // The specified port on the current instance of the SerialPort is already open
            {
                MessageBox.Show(SerialException.ToString());

                COMport.Close();
            }

            catch //Any other ERROR
            {
                MessageBox.Show("ERROR in Opening Serial PORT -- UnKnown ERROR");
                COMport.Close();
            }
            #endregion
        }
コード例 #4
0
        /// <summary>
        /// The method for turning the apparat on and off
        /// </summary>
        /// <param name="port">The port of the apparat</param>
        /// <param name="isOn">The state of the apparat (On/Off)</param>
        public string OnOff(int port, bool isOn)
        {
            //Add this to a SerialCom Class
            try
            {
                //Open Serial port
                serial_.Open();
            }
            #region Exceptions
            catch (UnauthorizedAccessException SerialException)
            {
                //Exception for when the operating system denies access
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch (System.IO.IOException SerialException)
            {
                //An attempt to set the state of the underlying port failed
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch (InvalidOperationException SerialException)
            {
                //The specified port on the current instance of the SerialPort is already open
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch
            {
                MessageBox.Show("ERROR in opening SerialPort - Unknown ERROR");
                serial_.Close();
            }
            #endregion

            if (serial_.IsOpen)
            {
                //Set the data address from the port
                string dataAddress = (port < 10 ? "0" + port.ToString() : port.ToString());
                string dataFunc    = (isOn ? "00" : "01");
                string data        = dataAddress + dataFunc;
                //Send data
                serial_.WriteLine(data);
                serial_.Close();
                return(data);
            }
            return(null);
        }
コード例 #5
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";
            }
        }
コード例 #6
0
        private void recvBtn_Click(object sender, EventArgs e)
        {
            // TODO read name and baudrate from the form
            using (SerialPort COMport = new SerialPort("COM6", 38400))
            {
                COMport.ReadTimeout = 3500;

                try
                {
                    //COMport.Parity = checkBox_parity.Checked ? Parity.Even : Parity.None;
                    COMport.Open();
                }
                catch (Exception SerialException)
                {
                    Console.WriteLine($"{SerialException.ToString()}");
                    Console.WriteLine("Error openning serial port.");
                }

                try
                {
                    if (COMport.IsOpen)
                    {
                        string l   = COMport.ReadLine();
                        int    len = Convert.ToInt32(l);
                        Console.WriteLine("Waiting...");
                        byte[] data = new byte[len];
                        int    read = 0;
                        while (read < len)
                        {
                            read += COMport.Read(data, read, len - read);
                        }
                        pictureBox.Image = bytesToImg(data, len);
                    }
                    else
                    {
                        Console.WriteLine("Can't recv. :(");
                    }
                    COMport.Close();
                }
                catch (TimeoutException)
                {
                    Console.WriteLine($"{COMport.ReadTimeout.ToString()}ms passed{Environment.NewLine}operation timed out");
                    COMport.Close();
                }
            }
        }
コード例 #7
0
ファイル: Form1.cs プロジェクト: Yngwarr/SerialPictures
        private void button_send_Click(object sender, EventArgs e)
        {
            button_send.Enabled = false;

            string portname = comboBox_port.SelectedItem.ToString();
            int    baudrate = Convert.ToInt32(comboBox_baud.SelectedItem);

            byte[] data = imgToBytes(theImage);

            // MessageBox.Show(portname);
            // MessageBox.Show(baudrate.ToString());

            using (SerialPort COMport = new SerialPort(portname, baudrate))
            {
                try {
                    COMport.Open();
                } catch (Exception SerialException) {
                    log($"{textBox_log.Text}{Environment.NewLine}{SerialException.ToString()}");
                    log("Error openning serial port.");
                }

                if (COMport.IsOpen)
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    COMport.WriteLine("INCOMING");
                    COMport.WriteLine($"{data.Length}");
                    COMport.Write(data, 0, data.Length);
                    sw.Stop();
                    log($"{data.Length} bytes sent in {sw.ElapsedMilliseconds} ms");
                }
                else
                {
                    button_send.Enabled = true;
                    log("Can't send. :(");
                }
                COMport.Close();
                button_send.Enabled = true;
            }
        }
コード例 #8
0
 public void btnOpenPort_Click(object sender, EventArgs e)
 {
     par.mP.PortName = cboPorts.Text;
     par.mP.BaudRate = Convert.ToInt32(cbobaudRate.Text);
     par.mP.DataBits = Convert.ToInt16(cboDataBits.Text);
     par.mP.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboStopBits.Text);
     try
     {
         par.mP.Open();
         par.btnConfigure.BackColor = System.Drawing.Color.Lime;
         par.btnConfigure.Enabled   = false;
         this.Close();
     }
     catch (Exception SerialException)
     {
         MessageBox.Show(SerialException.ToString());
         par.mP.Close();
     }
     if (par.mP.IsOpen)
     {
         MessageBox.Show("Port is Open");
         this.Close();
     }
 }
コード例 #9
0
        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;
            }
        }
コード例 #10
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
            }
        }
コード例 #11
0
        //Used for sending data to SerialPort
        private void Button_Transmit_Data_Click(object sender, EventArgs e)
        {
            GroupBox_Serial_Receive.Enabled = false; // Disable the Receive Groupbox

            //Local Variables
            string Port_Name = ComboBox_Available_SerialPorts.SelectedItem.ToString();    // Store the selected COM port name to "Port_Name" varaiable
            int    Baud_Rate = Convert.ToInt32(ComboBox_Standard_Baudrates.SelectedItem); // Convert the string "9600" to int32 9600
            string Data      = TextBox_Data_Transmitted.Text;                             //Store the string in Textbox to variable "Data"


            // MessageBox.Show(Port_Name);           // Debugging Purpose
            // MessageBox.Show(Baud_Rate.ToString());

            SerialPort COMport = new SerialPort(Port_Name, Baud_Rate); //Create a new  SerialPort Object (defaullt setting -> 8N1)

            //try to Open SerialPort


            try
            {
                COMport.Open();
            }
            #region
            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();
            }

            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();
            }

            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();
            }

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

            //If we are able to open the port
            if (COMport.IsOpen == true)
            {
                //MessageBox.Show("Port Opened");
                COMport.WriteLine(Data);                // Send Data
                COMport.Close();                        // Close the Port
                GroupBox_Serial_Receive.Enabled = true; // Enable the Receive Groupbox

                TextBox_System_Log.Text = Port_Name + Environment.NewLine + Baud_Rate;
                TextBox_System_Log.Text = TextBox_System_Log.Text + Environment.NewLine + Data + "  Written to Port";
            }
            else
            {
                GroupBox_Serial_Receive.Enabled = true; // Enable the Receive Groupbox
                MessageBox.Show("Unable to Write to COM port ");
                COMport.Close();
            }
        }
コード例 #12
0
        /// <summary>
        /// Method for Dimming A Light/Lowering the current
        /// </summary>
        /// <param name="port">The port of the apparat</param>
        /// <param name="bar">The index of the sliding bar, 0=20%, 1=40%,...</param>
        public string Dimm(int port, int bar)
        {
            try
            {
                //Open Serial port
                serial_.Open();
            }
            #region Exceptions
            catch (UnauthorizedAccessException SerialException)
            {
                //Exception for when the operating system denies access
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch (System.IO.IOException SerialException)
            {
                //An attempt to set the state of the underlying port failed
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch (InvalidOperationException SerialException)
            {
                //The specified port on the current instance of the SerialPort is already open
                MessageBox.Show(SerialException.ToString());
                serial_.Close();
            }
            catch
            {
                MessageBox.Show("ERROR in opening SerialPort - Unknown ERROR");
                serial_.Close();
            }
            #endregion

            if (serial_.IsOpen)
            {
                //Set the data address from the port
                string dataAddress = (port < 10 ? "0" + port.ToString() : port.ToString());
                string dataFunc    = "00"; //Default to turn off
                switch (bar)
                {
                case 0:
                    dataFunc = "02";     //20% on
                    break;

                case 1:
                    dataFunc = "03";     //40% on
                    break;

                case 2:
                    dataFunc = "04";     //60% on
                    break;

                case 3:
                    dataFunc = "05";     //80% on
                    break;

                case 4:
                    dataFunc = "01";     //100% on / TurnOn code
                    break;

                default:
                    break;
                }
                //Create Data string to be sent
                string data = dataAddress + dataFunc;

                //Send data
                serial_.WriteLine(data);
                serial_.Close();
                return(data);
            }
            return(null);
        }