

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//Add
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using static Jdas_Mbic.Bluecell_BootProtocol;

namespace Jdas_Mbic
{
    public partial class JdasMbic : Form
    {
        const double Table_Dbm_DefaultValue = 0;//31.5;
        const int Table_DL_DefaultValue = 5;
        const int Table_UL_DefaultValue = -15;
        const int Table_Temp_DefaultValue = -10;
        Serial serial = new Serial(); // Uart Open
        enum DLUL_Category
        {
            DL_DET = 0,
            UL_DET
        };


        public JdasMbic()
        {
            InitializeComponent();
        }

        private void JdasMbic_Load(object sender, EventArgs e)
        {
            serial.Serial_Main_Form_Get(this);
            ATT_TableLayoutSetting();
            serial.Serial_Initialize(ref comboBox_Port);

        }
        
        private void ATT_TableLayoutSetting()
        {
            double TempDbmData = Table_Dbm_DefaultValue;
            string tempstr = "";
            for (int i = 0; i < 64; i++)
            {
                dataGridView_TableSetting.Rows.Add();
                tempstr = string.Format("{0:0.0}", TempDbmData);
                dataGridView_TableSetting[0, i].Value = Convert.ToString(tempstr) + " dBm"; ;
                dataGridView_TableSetting[1, i].Value = "0";
                TempDbmData = TempDbmData + 0.5;
            }

            foreach (DataGridViewRow row in this.dataGridView_TableSetting.Rows)
            {
                row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
            }
            this.dataGridView_TableSetting.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            this.dataGridView_TableSetting.TopLeftHeaderCell.Value = "No.";
            dataGridView_TableSetting.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_TableSetting.ClearSelection();
        }


        private void DET_TableLayoutSetting(DLUL_Category cat)
        {
            string tempstr = "";
            DLUL_Category tempenum = cat;

            switch (tempenum) {
                case DLUL_Category.DL_DET:
                    double Temp_DL_Data = Table_DL_DefaultValue;
                    for (int i = 0; i < 31; i++)
                    {
                        dataGridView_TableSetting.Rows.Add();
                        tempstr = string.Format("{0:0}", Temp_DL_Data);
                        dataGridView_TableSetting[0, i].Value = Convert.ToString(tempstr) + " dBm";
                        Temp_DL_Data = Temp_DL_Data - 1;
                        dataGridView_TableSetting[1, i].Value = "0";

                    }
                    break;
                case DLUL_Category.UL_DET:
                    double Temp_UL_Data = Table_UL_DefaultValue;
                    for (int i = 0; i < 46; i++)
                    {
                        dataGridView_TableSetting.Rows.Add();
                        tempstr = string.Format("{0:0}", Temp_UL_Data);
                        dataGridView_TableSetting[0, i].Value = Convert.ToString(tempstr) + " dBm";
                        Temp_UL_Data = Temp_UL_Data - 1;
                        dataGridView_TableSetting[1, i].Value = "0";
                    }
                    break;
            }
            foreach (DataGridViewRow row in this.dataGridView_TableSetting.Rows)
            {
                row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
            }
            this.dataGridView_TableSetting.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            this.dataGridView_TableSetting.TopLeftHeaderCell.Value = "No.";
            dataGridView_TableSetting.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_TableSetting.ClearSelection();

        }
        private void Temp_TableLayoutSetting()
        {
            string tempstr = "";
            
            double Temp_Data = Table_Temp_DefaultValue;
            for (int i = 0; i < 10; i++)
            {
                dataGridView_TableSetting.Rows.Add();
                tempstr = string.Format("{0:0}", Temp_Data);
                dataGridView_TableSetting[0, i].Value = Convert.ToString(tempstr) + " ℃";
                Temp_Data = Temp_Data - 5;
                dataGridView_TableSetting[1, i].Value = "0";

            }
            foreach (DataGridViewRow row in this.dataGridView_TableSetting.Rows)
            {
                row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
            }
            this.dataGridView_TableSetting.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            this.dataGridView_TableSetting.TopLeftHeaderCell.Value = "No.";
            dataGridView_TableSetting.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView_TableSetting.ClearSelection();
        }

        private void dataGridView_TableSetting_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        /*
         * 
         * TABLE Setting Area
         * 
         */
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            string tempstr = comboBox.Text;

            switch (tempstr)
            {
                case "ATT_DL1":
                case "ATT_UL1": 
                case "ATT_DL2": 
                case "ATT_UL2": 
                case "ATT_DL3": 
                case "ATT_UL3": 
                case "ATT_DL4": 
                case "ATT_UL4": 
                    dataGridView_TableSetting.Rows.Clear();
                    ATT_TableLayoutSetting();
                    break;
                case "DET_DL1": 
                case "DET_DL2": 
                case "DET_DL3": 
                case "DET_DL4":
                    dataGridView_TableSetting.Rows.Clear();
                    DET_TableLayoutSetting(DLUL_Category.DL_DET);
                    break;
                case "DET_UL1": 
                case "DET_UL2": 
                case "DET_UL3": 
                case "DET_UL4":
                    dataGridView_TableSetting.Rows.Clear();
                    DET_TableLayoutSetting(DLUL_Category.UL_DET);
                    break;
                case "TEMP_DL1": 
                case "TEMP_UL1": 
                case "TEMP_DL2": 
                case "TEMP_UL2": 
                case "TEMP_DL3": 
                case "TEMP_UL3": 
                case "TEMP_DL4": 
                case "TEMP_UL4": 
                    dataGridView_TableSetting.Rows.Clear();
                    Temp_TableLayoutSetting();
                    break;
                default:
                    MessageBox.Show("존재하지 않는 ComboBox 입니다.\r\n 확인해주세요.");
                    break;
            }

            button_FileLoad_Click(null,null);
        }
        private List<int> DataGridViewIndexOutput()
        {
            List<int> Tmplist = new List<int>();

            Int32 selectedCellCount =
            dataGridView_TableSetting.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                    System.Text.StringBuilder sb =
                        new System.Text.StringBuilder();

                    //for (int i = 0;i < selectedCellCount; i++)
                    for (int i = 0; i < selectedCellCount; i += 2)
                    {
                        sb.Append("Row: ");
                        sb.Append(dataGridView_TableSetting.SelectedCells[i].RowIndex.ToString());
                        Tmplist.Add(dataGridView_TableSetting.SelectedCells[i].RowIndex);
                        /*sb.Append(", Column: ");
                        sb.Append(dataGridView_TableSetting.SelectedCells[i].ColumnIndex.ToString());*/
                        sb.Append(Environment.NewLine);
                    }

                    // sb.Append("Total: " + selectedCellCount.ToString());
                   // MessageBox.Show(sb.ToString(), "Selected Cells");
            }
            return Tmplist;
        }
        private void button_MultiSet_Click(object sender, EventArgs e)
        {
            List<int> TmpList = new List<int>();
            TmpList = DataGridViewIndexOutput();
            for(int i = 0; i < TmpList.Count; i++)
            {
                dataGridView_TableSetting[1, TmpList[i]].Value = numericUpDown_MultiSet.Value.ToString();
            }
        }

        private void button_Expansion_Click(object sender, EventArgs e)
        {
            List<int> TmpList = new List<int>();
            int Count = 0;
            double MaxVal = 0, MinVal = 0,Result = 0;
            double Tmpdata = 0;

            TmpList = DataGridViewIndexOutput();
            TmpList.Sort();

            Count = (TmpList[TmpList.Count - 1] - TmpList[0]);
            MaxVal = Convert.ToDouble(dataGridView_TableSetting[1,TmpList[TmpList.Count - 1]].Value);
            MinVal = Convert.ToDouble(dataGridView_TableSetting[1, TmpList[0]].Value);

            Result = (MaxVal - MinVal) / Count;

            for (int i = 0; i < (Count  - 1); i++)
            {
                Tmpdata = Convert.ToDouble(dataGridView_TableSetting[1, TmpList[0] + i].Value);
                if (comboBox_TableName.Text.Contains("DET") == true)
                {
                    dataGridView_TableSetting[1, TmpList[0] + (i + 1)].Value = string.Format("{0:0.000}", Math.Round(Tmpdata + Result, 3));
                }
                else
                {
                    dataGridView_TableSetting[1, TmpList[0] + (i + 1)].Value = string.Format("{0:0.0}", Math.Round(Tmpdata + Result, 1));
                }
            }

           /* for (int i = 0; i < TmpList.Count; i++)
            {
                dataGridView_TableSetting[1, TmpList[i]].Value = numericUpDown_MultiSet.Value.ToString();
            }*/
        }

        private void button_Initalize_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView_TableSetting.RowCount; i++)
            {
                dataGridView_TableSetting[1, i].Value = 0;
            }

        }

        private void button_CurrentOutput_Click(object sender, EventArgs e)
        {
            int columnIndex = dataGridView_TableSetting.CurrentCell.ColumnIndex;
            int rowIndex = dataGridView_TableSetting.CurrentCell.RowIndex;

            dataGridView_TableSetting[1, rowIndex].Value 
                = numericUpDown_CurrentOutput.Value;
        }

        private void button_MultiOffset_Click(object sender, EventArgs e)
        {
            List<int> TmpList = new List<int>();
            TmpList = DataGridViewIndexOutput();
            double CurrentVal = 0, SumVal = 0;

            for (int i = 0; i < TmpList.Count; i++)
            {
                CurrentVal = Convert.ToDouble(dataGridView_TableSetting[1, TmpList[i]].Value);
                SumVal = Convert.ToDouble(numericUpDown_MultiOffset.Value);
                dataGridView_TableSetting[1, TmpList[i]].Value = Convert.ToString(CurrentVal + SumVal);
            }
        }
     //   Bluecell_Serial bluecellserial = new Bluecell_Serial();

        private void button_PortOpen_Click(object sender, EventArgs e)
        {
            bool ret = false;
            ret = Serial_connectiondisable(serial.Serial_PortOpen(ref button_PortOpen, ref comboBox_Port));
            if (ret == false && timer_JdasMbic.Enabled == false)
            {
                timer_JdasMbic.Start(); //타이머를 발동시킨다.
            }
            else if (timer_JdasMbic.Enabled == true)
            {
                timer_JdasMbic.Stop(); //타이머를 멈춘다.
            }
            else
            {
                /*NOP*/
            }
            serial.Serial_Main_Form_Get(this);
        }

        private void button_terminal_Click(object sender, EventArgs e)
        {
            serial.Serial_TerminalOpen(serial);
        }
        
       
        public bool Serial_connectiondisable(Boolean on_off)
        {
            comboBox_Port.Enabled = on_off;
            comboBox_baudrate.Enabled = on_off;
            return on_off;
        }
        public bool TX_RX_Light = false;
        private delegate void StringSend(object label, String str);
        private delegate void SWVisible(object label, bool set);
        private delegate void GridviewSend(ref object gridview, string Text);
        private delegate void ByteSend(byte[] Text);

        int offset = 0;
        byte[] rxBuffer;
#if false
        private void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string startStr = "V";
            string endStr = "\n";

            try
            {

                if (offset == 0)
                {
                    rxBuffer = new byte[4096];
                }

                string rxString = "";
                int intRecSize = serialPort_JdasMbic.BytesToRead;

                if (intRecSize != 0)
                {




                    /* if (panel_GpsTest.Visible == true)
                     {
                         byte[] btdata = new byte[intRecSize];
                         serialPort_JdasMbic.Read(btdata, 0, intRecSize);
                         string data = Encoding.Default.GetString(rxBuffer);

                         this.Invoke(new StringSend(Data_Recv_Str), data);
                     }
                     else*/
                    {
                        serialPort_JdasMbic.Read(rxBuffer, offset, intRecSize);
                        offset += intRecSize;
                        for (int iTemp = 0; iTemp < offset; iTemp++)
                        {
                            rxString += Convert.ToChar(rxBuffer[iTemp]);
                        }
                        /*
                         * 
                         * Example
                         * if (panel_SensorTest.Visible == true)
                        {
                            startStr = "V";
                            endStr = "\n";
                        }

                        else if (panel_Catm1Test.Visible == true)
                        {
                            startStr = "B";
                            endStr = "\n";
                        }
                        else if (panel_GpsTest.Visible == true)
                        {
                            startStr = "$";
                            endStr = "\n";
                        }
                        
                         */
                        if (rxString.Contains(startStr))
                        {
                            rxString = rxString.Substring(rxString.IndexOf(startStr));
                            if (rxString.Contains(endStr))
                            {
                                this.Invoke(new StringSend(Data_Recv_Str), rxString);
                                //program.dataReceived(rxString, "Receive");
                                offset = 0;
                            }
                        }
                        else
                        {
                            this.Invoke(new StringSend(Data_Recv_Str), rxString);
                            offset = 0;
                        }
                    }

                }
            }
            catch { }

        }
#endif

        int LineLimit = 500;
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
        private const int WM_SETREDRAW = 11;
        public void Data_Recv_Str(string text)
        {

            System.Windows.Forms.RichTextBox tbReceived = null;
            //tbReceived = richTextBox_Sensor;

            int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch (Exception e) { MessageBox.Show(e.StackTrace); }
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }
                    tbReceived.Lines = tempLines.ToArray();
                }
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                /* Example
                if (panel_SensorTest.Visible == true)
                {

                    Sensor_TestFunc(text);
                    tbReceived.AppendText(text);
                }
                else
                {
                    tbReceived.AppendText(text);
                }

                */
                
              tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택... 
                tbReceived.ScrollToCaret();
            }
            catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
        }
        public void Data_Recv_Str(byte[] text)
        {
            string strtext = Encoding.Default.GetString(text);

            System.Windows.Forms.RichTextBox tbReceived = null;
          
//            tbReceived = richTextBox_Sensor;

            int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch (Exception e) { MessageBox.Show(e.StackTrace); }
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }
                    tbReceived.Lines = tempLines.ToArray();
                }
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                /* Example
                if (panel_SensorTest.Visible == true)
                {
                    Sensor_TestFunc(strtext);
                    tbReceived.AppendText(strtext);
                }
                else
                {
                    tbReceived.AppendText(strtext);
                }
                */
                tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택... 
                tbReceived.ScrollToCaret();
            }
            catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
        }
        Int64 Timer_Cnt = 0;
        Int32 ReqTimer_Cnt = 0;
        Bluecell_BootProtocol bluecell_BootProtocol = new Bluecell_BootProtocol();
        private void timer1_Tick(object sender, EventArgs e)
        {
            Update_Serial update_Serial = null;
            bool req_set = false;
            if (update_Serial == null)
            {
                update_Serial = (Update_Serial)serial.FileDownloadClass_Get();
                if (update_Serial == null)
                    req_set = true;
                else if (update_Serial.Update_ready == false)
                    req_set = true;
            }
            if (req_set)
            {
                ReqTimer_Cnt++;
            }
            if (TX_RX_Light == true)
            {
                Timer_Cnt++;
            }
            if (Timer_Cnt < 1000)
            {
                if (this.pictureBox_G_TX.Visible == true)
                {
                    TX_Light_OFF();
                }
                if (this.pictureBox_G_RX.Visible == true)
                {
                    RX_Light_OFF();
                }
                Timer_Cnt = 0;
                TX_RX_Light = false;
            }
            if (ReqTimer_Cnt > 6)
            {
                bluecell_BootProtocol.Bluecell_RF_Status_Req(serial);
                ReqTimer_Cnt = 0;
            }
        }

        private void radioButton_ATT_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton radioButton = (RadioButton)sender;

            switch (radioButton.Text)
            {
                case "ATT1":
                    label_Table_ATTDL.Text = "ATT_DL1";
                    label_Table_ATTUL.Text = "ATT_UL1";
                    break;
                case "ATT2":
                    label_Table_ATTDL.Text = "ATT_DL2";
                    label_Table_ATTUL.Text = "ATT_UL2";
                    break;
                case "ATT3":
                    label_Table_ATTDL.Text = "ATT_DL3";
                    label_Table_ATTUL.Text = "ATT_UL3";
                    break;
                case "ATT4":
                    label_Table_ATTDL.Text = "ATT_DL4";
                    label_Table_ATTUL.Text = "ATT_UL4";
                    break;
               
                    
                
            }
        }

        private void button_TableDL_Click(object sender, EventArgs e)
        {

        }

        private void button_TableUL_Click(object sender, EventArgs e)
        {

        }
        Update_Serial file = new Update_Serial();
        private void button_FirmwareUpdate_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //FileDownload file = new FileDownload();
            if (setcheck_checkBox.Checked == true)
                setcheck_checkBox.Checked = false;
            file.ShowFileOpenDialog(ref serial, ofd);
        }
        public void TX_Light_ON()
        {
            this.pictureBox_G_TX.Visible = true;
            this.pictureBox_R_TX.Visible = false;
        }
        public void TX_Light_OFF()
        {
            this.pictureBox_R_TX.Visible = true;
            this.pictureBox_G_TX.Visible = false;
        }
        public void RX_Light_ON()
        {
            this.pictureBox_G_RX.Visible = true;
            this.pictureBox_R_RX.Visible = false;
        }
        public void RX_Light_OFF()
        {
            this.pictureBox_R_RX.Visible = true;
            this.pictureBox_G_RX.Visible = false;
        }
        const byte BLUECELL_RESET_LENGTH = 7;
        public const byte BLUECELL_HEADER = 0xBE;
        public const byte BLUECELL_TAILER = 0xEB;


      

        const byte BLUECELL_ATTEN_LENGTH = 32 + 5;
        private void button_ATT_Click_1(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[BLUECELL_ATTEN_LENGTH];
            Button btn = (Button)sender;
            byte datatype = (byte)Bluecell_BootProtocol.DATATYPE.ATTSET;
            Int16[] DL = new Int16[4];
            Int16[] UL = new Int16[4];
            Int16[] ALC_MAX = new Int16[4];
            Int16[] ALC_MIN = new Int16[4];
            byte tmp_H = 0;
            byte tmp_L = 0;
            DL[0] = Convert.ToInt16(numericUpDown_ATT_DL1.Value * 100);
            DL[1] = Convert.ToInt16(numericUpDown_ATT_DL2.Value * 100);
            DL[2] = Convert.ToInt16(numericUpDown_ATT_DL3.Value * 100);
            DL[3] = Convert.ToInt16(numericUpDown_ATT_DL4.Value * 100);

            UL[0] = Convert.ToInt16(numericUpDown_ATT_UL1.Value * 100);
            UL[1] = Convert.ToInt16(numericUpDown_ATT_UL2.Value * 100);
            UL[2] = Convert.ToInt16(numericUpDown_ATT_UL3.Value * 100);
            UL[3] = Convert.ToInt16(numericUpDown_ATT_UL4.Value * 100);

            ALC_MAX[0] = Convert.ToInt16(numericUpDown_ALC1_MAX.Value * 100);
            ALC_MAX[1] = Convert.ToInt16(numericUpDown_ALC2_MAX.Value * 100);
            ALC_MAX[2] = Convert.ToInt16(numericUpDown_ALC3_MAX.Value * 100);
            ALC_MAX[3] = Convert.ToInt16(numericUpDown_ALC4_MAX.Value * 100);

            ALC_MIN[0] = Convert.ToInt16(numericUpDown_ALC1_MIN.Value * 100);
            ALC_MIN[1] = Convert.ToInt16(numericUpDown_AGC2_MIN.Value * 100);
            ALC_MIN[2] = Convert.ToInt16(numericUpDown_AGC3_MIN.Value * 100);
            ALC_MIN[3] = Convert.ToInt16(numericUpDown_AGC4_MIN.Value * 100);


            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = Convert.ToByte(datatype);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = 6;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = 5;

            for (int i = 0; i < 4; i++)
            {
                tmp_H = Convert.ToByte((DL[i] & 0xFF00) >> 8);
                tmp_L = Convert.ToByte((DL[i] & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2] = tmp_H;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2 + 1] = tmp_L;
            }
            for (int i = 4; i < 8; i++)
            {
                tmp_H = Convert.ToByte((UL[i - 4] & 0xFF00) >> 8);
                tmp_L = Convert.ToByte((UL[i - 4] & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2] = tmp_H;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2 + 1] = tmp_L;
            }
            for (int i = 8; i < 12; i++)
            {
                tmp_H = Convert.ToByte((ALC_MAX[i - 8] & 0xFF00) >> 8);
                tmp_L = Convert.ToByte((ALC_MAX[i - 8] & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2] = tmp_H;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2 + 1] = tmp_L;
            }
            for (int i = 12; i < 16; i++)
            {
                tmp_H = Convert.ToByte((ALC_MIN[i - 12] & 0xFF00) >> 8);
                tmp_L = Convert.ToByte((ALC_MIN[i - 12] & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2] = tmp_H;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + i * 2 + 1] = tmp_L;
            }

            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 16 * 2] = 1; //CRC
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;
        }

        private void pictureBox_ATT_PATH_Ctrl_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[BLUECELL_RESET_LENGTH];
            PictureBox pictureBox = (PictureBox)sender;
            DATATYPE datatype = DATATYPE.ATTSET;
            ReqTimer_Cnt = 0;
            switch (pictureBox.Name)
            {
                case "pictureBox_DL1_PATH_OFF":
                    pictureBox_DL1_PATH_ON.Visible = true;
                    pictureBox_DL1_PATH_OFF.Visible = false;
                    datatype = DATATYPE.ATT_DL1_PATH;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    break;
                case "pictureBox_DL2_PATH_OFF":
                    pictureBox_DL2_PATH_ON.Visible = true;
                    pictureBox_DL2_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_DL2_PATH;
                    break;
                case "pictureBox_DL3_PATH_OFF":
                    pictureBox_DL3_PATH_ON.Visible = true;
                    pictureBox_DL3_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_DL3_PATH;
                    break;
                case "pictureBox_DL4_PATH_OFF":
                    pictureBox_DL4_PATH_ON.Visible = true;
                    pictureBox_DL4_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_DL4_PATH;
                    break;
                case "pictureBox_UL1_PATH_OFF":
                    pictureBox_UL1_PATH_ON.Visible = true;
                    pictureBox_UL1_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_UL1_PATH;
                    break;
                case "pictureBox_UL2_PATH_OFF":
                    pictureBox_UL2_PATH_ON.Visible = true;
                    pictureBox_UL2_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_UL2_PATH;
                    break;
                case "pictureBox_UL3_PATH_OFF":
                    pictureBox_UL3_PATH_ON.Visible = true;
                    pictureBox_UL3_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_UL3_PATH;
                    break;
                case "pictureBox_UL4_PATH_OFF":
                    pictureBox_UL4_PATH_ON.Visible = true;
                    pictureBox_UL4_PATH_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_UL4_PATH;
                    break;
                case "pictureBox_DL1_PATH_ON":
                    pictureBox_DL1_PATH_OFF.Visible = true;
                    pictureBox_DL1_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_DL1_PATH;
                    break;
                case "pictureBox_DL2_PATH_ON":
                    pictureBox_DL2_PATH_OFF.Visible = true;
                    pictureBox_DL2_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_DL2_PATH;
                    break;
                case "pictureBox_DL3_PATH_ON":
                    pictureBox_DL3_PATH_OFF.Visible = true;
                    pictureBox_DL3_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_DL3_PATH;
                    break;
                case "pictureBox_DL4_PATH_ON":
                    pictureBox_DL4_PATH_OFF.Visible = true;
                    pictureBox_DL4_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_DL4_PATH;
                    break;
                case "pictureBox_UL1_PATH_ON":
                    pictureBox_UL1_PATH_OFF.Visible = true;
                    pictureBox_UL1_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_UL1_PATH;
                    break;
                case "pictureBox_UL2_PATH_ON":
                    pictureBox_UL2_PATH_OFF.Visible = true;
                    pictureBox_UL2_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_UL2_PATH;
                    break;
                case "pictureBox_UL3_PATH_ON":
                    pictureBox_UL3_PATH_OFF.Visible = true;
                    pictureBox_UL3_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_UL3_PATH;
                    break;
                case "pictureBox_UL4_PATH_ON":
                    pictureBox_UL4_PATH_OFF.Visible = true;
                    pictureBox_UL4_PATH_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_UL4_PATH;
                    break;
                case "pictureBox_SelfTest1_ON":
                    pictureBox_SelfTest1_OFF.Visible = true;
                    pictureBox_SelfTest1_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_SelfTest1;
                break;
                case "pictureBox_SelfTest2_ON":
                    pictureBox_SelfTest2_OFF.Visible = true;
                    pictureBox_SelfTest2_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_SelfTest2;
                    break;
                case "pictureBox_SelfTest3_ON":
                    pictureBox_SelfTest3_OFF.Visible = true;
                    pictureBox_SelfTest3_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_SelfTest3;
                    break;
                case "pictureBox_SelfTest4_ON":
                    pictureBox_SelfTest4_OFF.Visible = true;
                    pictureBox_SelfTest4_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ATT_SelfTest4;
                    break;
                case "pictureBox_SelfTest1_OFF":
                    pictureBox_SelfTest1_ON.Visible = true;
                    pictureBox_SelfTest1_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_SelfTest1;
                    break;
                case "pictureBox_SelfTest2_OFF":
                    pictureBox_SelfTest2_ON.Visible = true;
                    pictureBox_SelfTest2_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_SelfTest2;
                    break;
                case "pictureBox_SelfTest3_OFF":
                    pictureBox_SelfTest3_ON.Visible = true;
                    pictureBox_SelfTest3_OFF.Visible = false ;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_SelfTest3;
                    break;
                case "pictureBox_SelfTest4_OFF":
                    pictureBox_SelfTest4_ON.Visible = true;
                    pictureBox_SelfTest4_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ATT_SelfTest4;
                    break;
                case "pictureBox_ALC1_ON":
                    pictureBox_ALC1_ON.Visible = false;
                    pictureBox_ALC1_OFF.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ALC1_EN;
                    break;
                case "pictureBox_ALC2_ON":
                    pictureBox_ALC2_ON.Visible = false;
                    pictureBox_ALC2_OFF.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ALC2_EN;
                    break;
                case "pictureBox_ALC3_ON":
                    pictureBox_ALC3_ON.Visible = false;
                    pictureBox_ALC3_OFF.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ALC3_EN;
                    break;
                case "pictureBox_ALC4_ON":
                    pictureBox_ALC4_ON.Visible = false;
                    pictureBox_ALC4_OFF.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.ALC4_EN;
                    break;
                case "pictureBox_ALC1_OFF":
                    pictureBox_ALC1_OFF.Visible = false;
                    pictureBox_ALC1_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ALC1_EN;
                    break;
                case "pictureBox_ALC2_OFF":
                    pictureBox_ALC2_OFF.Visible = false;
                    pictureBox_ALC2_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ALC2_EN;
                    break;
                case "pictureBox_ALC3_OFF":
                    pictureBox_ALC3_OFF.Visible = false;
                    pictureBox_ALC3_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ALC3_EN;
                    break;
                case "pictureBox_ALC4_OFF":
                    pictureBox_ALC4_OFF.Visible = false;
                    pictureBox_ALC4_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.ALC4_EN;
                    break;

                case "pictureBox_AGC1_ON":
                    pictureBox_AGC1_OFF.Visible = true;
                    pictureBox_AGC1_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.AGC1_EN;
                    break;
                case "pictureBox_AGC2_ON":
                    pictureBox_AGC2_OFF.Visible = true;
                    pictureBox_AGC2_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.AGC2_EN;
                    break;
                case "pictureBox_AGC3_ON":
                    pictureBox_AGC3_OFF.Visible = true;
                    pictureBox_AGC3_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.AGC3_EN;
                    break;
                case "pictureBox_AGC4_ON":
                    pictureBox_AGC4_OFF.Visible = true;
                    pictureBox_AGC4_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.AGC4_EN;
                    break;
                case "pictureBox_AGC1_OFF":
                    pictureBox_AGC1_OFF.Visible = false;
                    pictureBox_AGC1_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.AGC1_EN;
                    break;
                case "pictureBox_AGC2_OFF":
                    pictureBox_AGC2_OFF.Visible = false;
                    pictureBox_AGC2_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.AGC2_EN;
                    break;
                case "pictureBox_AGC3_OFF":
                    pictureBox_AGC3_OFF.Visible = false;
                    pictureBox_AGC3_ON.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.AGC3_EN;
                    break;
                case "pictureBox_AGC4_OFF":
                    pictureBox_AGC4_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.AGC4_EN;
                    break;
                default:
                    MessageBox.Show("잘 못된 값 입니다. 확인 부탁드립니다.");
                    return;
            }
            pictureBox.Visible = false;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = Convert.ToByte(datatype);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = 6;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = 5;
            //temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 1] = 0;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = 1; //CRC
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
        }

        private void button_ExcelSave_Click(object sender, EventArgs e)
        {
            GridViewExcel(this.dataGridView_TableSetting);
            button_ExcelKill_Click(null, null);
            //ExportExcel(false);
        }

        public void GridViewExcel(DataGridView grid)
        {
            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            //Excel.Range oRng;
            object missingType = Type.Missing;

            this.saveFileDialog1.FileName = Application.StartupPath + "\\" + comboBox_TableName.Text;
            this.saveFileDialog1.DefaultExt = "xls";
            this.saveFileDialog1.Filter = "Excel files (*.xls)|*.xls";
            this.saveFileDialog1.InitialDirectory = "{Desktop}";
            //DialogResult result = saveFileDialog1.ShowDialog();
            //if (result == DialogResult.OK)
            {
                try
                {
                    //Start Excel and get Application object.
                    oXL = new Excel.Application();
                    oXL.Visible = false;

                    //Get a new workbook.
                    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                    //oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                    oSheet = (Excel._Worksheet)oWB.ActiveSheet;
                    //oSheet = oWB.Worksheets.get_Item(1) as Excel.Worksheet;
                    //oSheet = (Excel.Worksheet)oWB.Worksheets.Item[2] as Excel._Worksheet;

                    //oWB.Worksheets.Item[1] as Excel._Worksheet;

                    //Add table headers going cell by cell.
                    int k = 0;
                    string[] colHeader = new string[grid.ColumnCount];
                    for (int i = 0; i < grid.Columns.Count; i++)
                    {
                        oSheet.Cells[1, i + 1] = grid.Columns[i].HeaderText;

                        k = i + 65;
                        colHeader[i] = Convert.ToString((char)k);
                    }
                    //Format A1:D1 as bold, vertical alignment = center.
                    oSheet.get_Range("A1", colHeader[colHeader.Length - 1] + "1").Font.Bold = true;
                    oSheet.get_Range("A1", colHeader[colHeader.Length - 1] + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    // Create an array to multiple values at once.
                    object[,] saNames = new object[grid.RowCount, grid.ColumnCount];

                    string tp;
                    for (int i = 0; i < grid.RowCount; i++)
                    {
                        for (int j = 0; j < grid.ColumnCount; j++)
                        {
                            tp = grid.Rows[i].Cells[j].ValueType.Name;
                            if (tp == "String") //2000-01-01 형태의 날짜 필터하기 위함(숫자로 변환 방지)
                                saNames[i, j] = "'" + grid.Rows[i].Cells[j].Value.ToString();
                            else
                                saNames[i, j] = grid.Rows[i].Cells[j].Value;
                        }
                    }

                    //Fill A2:B6 with an array of values (First and Last Names).
                    //oSheet.get_Range("A2", "B6").Value2 = saNames;
                    oSheet.get_Range(colHeader[0] + "2", colHeader[colHeader.Length - 1] + (grid.RowCount + 1)).Value2 = saNames;

                    oXL.Visible = false;
                    oXL.UserControl = false;
                    oWB.SaveAs(saveFileDialog1.FileName,
                    Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
                    missingType, missingType, missingType, missingType,
                    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                    missingType, missingType, missingType, missingType, missingType);
                    oWB.Close(false, missingType, missingType);
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Save Success!!!");
                }
                catch (Exception theException)
                {
                    String errorMessage;
                    errorMessage = "Error: ";
                    errorMessage = String.Concat(errorMessage, theException.Message);
                    errorMessage = String.Concat(errorMessage, " Line: ");
                    errorMessage = String.Concat(errorMessage, theException.Source);

                    MessageBox.Show(errorMessage, "Error");
                }
            }

        }
        
        public void ReadExcelData(string path)
        { // path는 Excel파일의 전체 경로입니다.
          // 예. D:\test\test.xslx
            Excel.Application excelApp = null;
            Excel.Workbook wb = null;
            Excel.Worksheet ws = null;
            try
            {
                excelApp = new Excel.Application();
                wb = excelApp.Workbooks.Open(path);
                // path 대신 문자열도 가능합니다
                // 예. Open(@"D:\test\test.xslx");
                ws = wb.Worksheets.get_Item(1) as Excel.Worksheet;
                // 첫번째 Worksheet를 선택합니다.
                Excel.Range rng = ws.UsedRange;   // '여기'
                                                  // 현재 Worksheet에서 사용된 셀 전체를 선택합니다.
                object[,] data = rng.Value;
                // 열들에 들어있는 Data를 배열 (One-based array)로 받아옵니다.
                for (int r = 1; r < data.GetLength(0); r++)
                {
                    for (int c = 1; c <= data.GetLength(1); c++)
                    {
                        if (data[r, c] == null)
                        {
                            continue;
                        }
                        //dataGridView_TableSetting.SelectedRows(r - 1).Cells(c - 1).Value = data[r + 1, c];
                        

                        // Data 빼오기
                        // data[r, c] 는 excel의 (r, c) 셀 입니다.
                        // data.GetLength(0)은 엑셀에서 사용되는 행의 수를 가져오는 것이고,
                        // data.GetLength(1)은 엑셀에서 사용되는 열의 수를 가져오는 것입니다.
                        // GetLength와 [ r, c] 의 순서를 바꿔서 사용할 수 있습니다.
                    }
                    dataGridView_TableSetting.Rows[r - 1].Cells[1].Value = data[r + 1, 2];
                }
                wb.Close(true);
                excelApp.Quit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReleaseExcelObject(ws);
                ReleaseExcelObject(wb);
                ReleaseExcelObject(excelApp);
                button_ExcelKill_Click(null,null);
            }
        }



        private void ReleaseExcelObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        
        private void button_ExcelLoad_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + "\\" + comboBox_TableName.Text + ".xls";
            ReadExcelData(path);
        }

        private void button_ExcelKill_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            foreach (System.Diagnostics.Process p in process)
            {
                if (!string.IsNullOrEmpty(p.ProcessName))
                {
                    try
                    {
                        p.Kill();
                    }
                    catch { }
                }
            }
        }

        enum BluecellReqIndex{
            bluecell_header = 0,
            bluecell_type,
            bluecell_length,
            bluecell_crcindex,
            Selftest0,
            Selftest1,
            Selftest2,
            Selftest3,
            ATT_DL1_PATH,
            ATT_DL2_PATH,
            ATT_DL3_PATH,
            ATT_DL4_PATH,
            ATT_UL1_PATH,
            ATT_UL2_PATH,
            ATT_UL3_PATH,
            ATT_UL4_PATH,
            ATT_DL1_H,
            ATT_DL1_L,
            ATT_DL2_H,
            ATT_DL2_L,
            ATT_DL3_H,
            ATT_DL3_L,
            ATT_DL4_H,
            ATT_DL4_L,
            ATT_UL1_H,
            ATT_UL1_L,
            ATT_UL2_H,
            ATT_UL2_L,
            ATT_UL3_H,
            ATT_UL3_L,
            ATT_UL4_H,
            ATT_UL4_L,
            DET_DL1_IN_H,
            DET_DL1_IN_L,
            DET_DL2_IN_H,
            DET_DL2_IN_L,
            DET_DL3_IN_H,
            DET_DL3_IN_L,
            DET_DL4_IN_H,
            DET_DL4_IN_L,
            DET_UL1_IN_H,
            DET_UL1_IN_L,
            DET_UL2_IN_H,
            DET_UL2_IN_L,
            DET_UL3_IN_H,
            DET_UL3_IN_L,
            DET_UL4_IN_H,
            DET_UL4_IN_L,
            DET_TEMP_H,
            DET_TEMP_L,
            ATT_AGC1_ONOFF,
            ATT_ALC1_ONOFF,
            ATT_AGC2_ONOFF,
            ATT_ALC2_ONOFF,
            ATT_AGC3_ONOFF,
            ATT_ALC3_ONOFF,
            ATT_AGC4_ONOFF,
            ATT_ALC4_ONOFF,
            ATT_ALC1_MAX_H,
            ATT_ALC1_MAX_L,
            ATT_ALC2_MAX_H,
            ATT_ALC2_MAX_L,
            ATT_ALC3_MAX_H,
            ATT_ALC3_MAX_L,
            ATT_ALC4_MAX_H,
            ATT_ALC4_MAX_L,
            ATT_ALC1_MIN_H,
            ATT_ALC1_MIN_L,
            ATT_ALC2_MIN_H,
            ATT_ALC2_MIN_L,
            ATT_ALC3_MIN_H,
            ATT_ALC3_MIN_L,
            ATT_ALC4_MIN_H,
            ATT_ALC4_MIN_L,
            ATT_USER_DL1_H,
            ATT_USER_DL1_L,
            ATT_USER_DL2_H,
            ATT_USER_DL2_L,
            ATT_USER_DL3_H,
            ATT_USER_DL3_L,
            ATT_USER_DL4_H,
            ATT_USER_DL4_L,
            ATT_USER_UL1_H,
            ATT_USER_UL1_L,
            ATT_USER_UL2_H,
            ATT_USER_UL2_L,
            ATT_USER_UL3_H,
            ATT_USER_UL3_L,
            ATT_USER_UL4_H,
            ATT_USER_UL4_L,
            ATT_USER_TEMP_H,
            ATT_USER_TEMP_L,
            bluecell_crc,
        };
        const byte Reqdatastartindex = 4;
        //private delegate void StringSend(object label, String str);
        private void Numeric_Text_Set(object label, String str)
        {
            NumericUpDown Temp_label = (NumericUpDown)label;
            Temp_label.Text = str;
        }
        private void TextBox_Text_Set(object label, String str)
        {
            TextBox Temp_label = (TextBox)label;
            Temp_label.Text = str;
        }
        private void String_Text_Set(object label, String str)
        {
            String Temp_label = (String)label;
            Temp_label = str;
        }
        private void Label_Text_Set(object label, String str)
        {
            Label Temp_label = (Label)label;
            Temp_label.Text = str;
        }
        private void Table_Text_Set(object label, String str)
        {
            label = str;
        }
        private void PicVisible_Text_Set(object label, bool set)
        {
            PictureBox Temp_pic = (PictureBox)label;
            Temp_pic.Visible = set;
        }
        private void CheckBoxVisible_Text_Set(object label, bool set)
        {
            CheckBox Temp_pic = (CheckBox) label;
            Temp_pic.Checked = set;
        }
        private Double ADCValueTodBmConvert(object[] RefData, object[] AdcData, object Currdata)
        {
            object data = Currdata;
            Double ret = 0xFF,Currentret = 0;
            int LastIndex = 0;
            for(int i = 0; i < AdcData.Length; i++)
            {
                Currentret = Convert.ToDouble(AdcData[i]) - Convert.ToDouble(data);
                if(Currentret < 0)
                {
                    Currentret = (Currentret * -2) + Currentret;
                }
                if (ret > Currentret)
                {
                    ret = Currentret;
                    LastIndex = i;
                }
            }
            return Convert.ToDouble(RefData[LastIndex]);
        }
        private double DET_Table_Apply(object label)
        {
            TextBox Temp_TextBox = (TextBox)label;
            double ret = 0;
            switch(Temp_TextBox.Name)
            {
                case "textBox_DET_DL1":
                    ret = ADCValueTodBmConvert(DET_DL1_RefTable, DET_DL1_Table, textBox_DET_DL1.Text);
                    break;
                case "textBox_DET_DL2":
                    ret = ADCValueTodBmConvert(DET_DL1_RefTable, DET_DL2_Table, textBox_DET_DL2.Text);
                    break;
                case "textBox_DET_DL3":
                    ret = ADCValueTodBmConvert(DET_DL1_RefTable, DET_DL3_Table, textBox_DET_DL3.Text);
                    break;
                case "textBox_DET_DL4":
                    ret = ADCValueTodBmConvert(DET_DL1_RefTable, DET_DL4_Table, textBox_DET_DL4.Text);
                    break;

                case "textBox_DET_UL1":
                    ret = ADCValueTodBmConvert(DET_UL1_RefTable, DET_UL1_Table, textBox_DET_UL1.Text);
                    break;
                case "textBox_DET_UL2":
                    ret = ADCValueTodBmConvert(DET_UL2_RefTable, DET_UL2_Table, textBox_DET_UL2.Text);
                    break;
                case "textBox_DET_UL3":
                    ret = ADCValueTodBmConvert(DET_UL3_RefTable, DET_UL3_Table, textBox_DET_UL3.Text);
                    break;
                case "textBox_DET_UL4":
                    ret = ADCValueTodBmConvert(DET_UL4_RefTable, DET_UL4_Table, textBox_DET_UL4.Text);
                    break;
            }
            return ret;
        }
        public void GUIDataStatusSet(byte[] buf)
        {
            double Volt_Calc_val = 3.3 / 4095;
            double adcdata = 0;
            double dBmdata = 0;
            if (buf[(int)BluecellReqIndex.Selftest0] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.Selftest1] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest2_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest2_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest2_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest2_ON, true);// = false;

            }

            if (buf[(int)BluecellReqIndex.Selftest2] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest3_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest3_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest3_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest3_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.Selftest3] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest4_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest4_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest4_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest4_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_DL1_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL1_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL1_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL1_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL1_PATH_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_DL2_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL2_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL2_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL2_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL2_PATH_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_DL3_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL3_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL3_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL3_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL3_PATH_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_DL4_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL4_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL4_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL4_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DL4_PATH_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_UL1_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL1_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL1_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL1_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL1_PATH_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_UL2_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL2_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL2_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL2_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL2_PATH_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_UL3_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL3_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL3_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL3_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL3_PATH_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_UL4_PATH] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL4_PATH_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL4_PATH_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL4_PATH_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL4_PATH_ON, true);// = false;
            }
            this.Invoke(new SWVisible(CheckBoxVisible_Text_Set), checkBox_Fix, IamSetting_Get());// = false;
            if (IamSetting_Get() == false)
            {
                //this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_ON, false);// = false;
                //this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_SelfTest1_OFF, true);// = false;
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_DL1_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_DL1_L] & 0x00FF);
                
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL1, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_DL2_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_DL2_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL2, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_DL3_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_DL3_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL3, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_DL4_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_DL4_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL4, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_UL1_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_UL1_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_UL1, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_UL2_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_UL2_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_UL2, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_UL3_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_UL3_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_UL3, adcdata.ToString("N2"));
                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_UL4_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_UL4_L] & 0x00FF);
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_UL4, adcdata.ToString("N2"));
                adcdata = 0;


                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC1_MIN_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC1_MIN_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC1_MIN, adcdata.ToString("N2"));



                adcdata = 0;
                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC2_MIN_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC2_MIN_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_AGC2_MIN, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC3_MIN_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC3_MIN_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_AGC3_MIN, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC4_MIN_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC4_MIN_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_AGC4_MIN, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC1_MAX_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC1_MAX_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC1_MAX, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC2_MAX_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC2_MAX_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC2_MAX, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC3_MAX_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC3_MAX_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC3_MAX, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_ALC4_MAX_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_ALC4_MAX_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC4_MAX, adcdata.ToString("N2"));
                adcdata = 0;







                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_DL1_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_DL1_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL1_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_DL2_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_DL2_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL2_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_DL3_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_DL3_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL3_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_DL4_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_DL4_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL4_User, adcdata.ToString("N2"));
                adcdata = 0;





                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_UL1_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_UL1_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL1_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_UL2_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_UL2_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL2_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_UL3_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_UL3_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL3_User, adcdata.ToString("N2"));
                adcdata = 0;

                adcdata += ((buf[(int)BluecellReqIndex.ATT_USER_UL4_H] << 8) & 0xFF00);
                adcdata += (buf[(int)BluecellReqIndex.ATT_USER_UL4_L] & 0x00FF);
                if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata /= 100;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL4_User, adcdata.ToString("N2"));
                adcdata = 0;




            }
            adcdata = 0;
            adcdata = ((buf[(int)BluecellReqIndex.DET_DL1_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_DL1_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_DL1);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1_dBm, dBmdata.ToString("N3"));
            //this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1_dBm, adcdata.ToString("N2"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_DL2_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_DL2_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL2, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_DL2);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL2_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_DL3_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_DL3_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL3, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_DL3);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL3_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_DL4_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_DL4_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL4, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_DL4);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL4_dBm, dBmdata.ToString("N3"));
            adcdata = 0;


            adcdata = ((buf[(int)BluecellReqIndex.DET_UL1_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_UL1_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL1, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_UL1);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL1_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_UL2_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_UL2_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL2, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_UL2);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL2_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_UL3_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_UL3_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL3, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_UL3);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL3_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata = ((buf[(int)BluecellReqIndex.DET_UL4_IN_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_UL4_IN_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL4, adcdata.ToString("N3"));
            dBmdata = DET_Table_Apply(textBox_DET_UL4);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL4_dBm, dBmdata.ToString("N3"));
            adcdata = 0;

            adcdata += ((buf[(int)BluecellReqIndex.DET_TEMP_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_TEMP_L] & 0x00FF);
            adcdata = adcdata * 3.0 / 4096;
            adcdata = (adcdata - 0.5) * 100;

            this.Invoke(new StringSend(Label_Text_Set), label_Temp, adcdata.ToString("N2") + "°C");
            adcdata = 0;



            if (buf[(int)BluecellReqIndex.ATT_AGC1_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC1_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC1_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC1_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC1_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_AGC2_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC2_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC2_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC2_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC2_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_AGC3_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC3_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC3_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC3_ON, true);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC3_OFF, false);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_AGC4_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC4_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC4_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC4_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC4_ON, true);// = false;
            }


            if (buf[(int)BluecellReqIndex.ATT_ALC1_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC1_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC1_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC1_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC1_ON, true);// = false;
            }
            if (buf[(int)BluecellReqIndex.ATT_ALC2_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC2_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC2_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC2_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC2_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_ALC3_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC3_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC3_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC3_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC3_ON, true);// = false;
            }

            if (buf[(int)BluecellReqIndex.ATT_ALC4_ONOFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC4_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC4_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC4_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC4_ON, true);// = false;
            }
            TableCurrentOutputSetting();

        }
        private void TableCurrentOutputSetting()
        {
            ComboBox comboBox = (ComboBox)comboBox_TableName;
            CheckForIllegalCrossThreadCalls = false;
            string tempstr = comboBox_TableName.Text;
            //this.Invoke(new StringSend(String_Text_Set), tempstr, );
            switch (tempstr)
            {
                case "DET_DL1":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_DL1.Text);
                    break;
                case "DET_DL2":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_DL2.Text);
                    break;
                case "DET_DL3":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_DL3.Text);
                    break;
                case "DET_DL4":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_DL4.Text);
                    break;
                case "DET_UL1":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_UL1.Text);
                    break;
                case "DET_UL2":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_UL2.Text);
                    break;
                case "DET_UL3":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_UL3.Text);
                    break;
                case "DET_UL4":
                    this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_CurrentOutput, textBox_DET_UL4.Text);
                    break;
               
                default:
                    //MessageBox.Show("존재하지 않는 ComboBox 입니다.\r\n 확인해주세요.");
                    break;
            }
        }
        object[] DET_DL1_Table = new object[128];
        object[] DET_DL2_Table = new object[128];
        object[] DET_DL3_Table = new object[128];
        object[] DET_DL4_Table = new object[128];

        object[] DET_UL1_Table = new object[128];
        object[] DET_UL2_Table = new object[128];
        object[] DET_UL3_Table = new object[128];
        object[] DET_UL4_Table = new object[128];

        object[] DET_DL1_RefTable = new object[128];
        object[] DET_DL2_RefTable = new object[128];
        object[] DET_DL3_RefTable = new object[128];
        object[] DET_DL4_RefTable = new object[128];

        object[] DET_UL1_RefTable = new object[128];
        object[] DET_UL2_RefTable = new object[128];
        object[] DET_UL3_RefTable = new object[128];
        object[] DET_UL4_RefTable = new object[128];
        private void DET_TableSetting(object Valueref,object Value,int index)
        {
            ComboBox comboBox = (ComboBox)comboBox_TableName;
            CheckForIllegalCrossThreadCalls = false;
            string tempstr = comboBox_TableName.Text;
            string Valuerefstr = "";
            Valuerefstr = Convert.ToString(Valueref);
            Valuerefstr = Valuerefstr.Replace(" dBm", "");
            Valueref = Valuerefstr;

            //this.Invoke(new StringSend(String_Text_Set), tempstr, );
            switch (tempstr)
            {
                case "DET_DL1":
                    DET_DL1_Table[index] = Value;
                    DET_DL1_RefTable[index] = Valueref;
                    break;
                case "DET_DL2":
                    DET_DL2_Table[index] = Value;
                    DET_DL2_RefTable[index] = Valueref;
                    break;
                case "DET_DL3":
                    DET_DL3_Table[index] = Value;
                    DET_DL3_RefTable[index] = Valueref;
                    break;
                case "DET_DL4":
                    DET_DL4_Table[index] = Value;
                    DET_DL4_RefTable[index] = Valueref;
                    break;
                case "DET_UL1":
                    DET_UL1_Table[index] = Value;
                    DET_UL1_RefTable[index] = Valueref;
                    break;
                case "DET_UL2":
                    DET_UL2_Table[index] = Value;
                    DET_UL2_RefTable[index] = Valueref;
                    break;
                case "DET_UL3":
                    DET_UL3_Table[index] = Value;
                    DET_UL3_RefTable[index] = Valueref;
                    break;
                case "DET_UL4":
                    DET_UL4_Table[index] = Value;
                    DET_UL4_RefTable[index] = Valueref;
                    break;
                default:
                    //MessageBox.Show("존재하지 않는 ComboBox 입니다.\r\n 확인해주세요.");
                    break;
            }
        }
        public void TableDataStatusSet(byte[] buf)
        {
#if true
            Int16 TableValue = 0;
            double ret = 0;
            for (int i = 0; i < (buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] - 4)/2; i++) {
                TableValue = buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1 )+ i * 2];
                TableValue <<= 8;

                TableValue += buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i * 2 + 1];
                if (comboBox_TableName.Text.Contains("DET") == true)
                    ret = Convert.ToDouble(TableValue) / 1000;
                else
                    ret = Convert.ToDouble(TableValue) / 100;
                try
                {
                    dataGridView_TableSetting[1, i].Value = ret.ToString("N3");
                    DET_TableSetting(dataGridView_TableSetting[0, i].Value, dataGridView_TableSetting[1, i].Value, i);
                }
                catch { }
                //this.Invoke(new StringSend(Table_Text_Set), dataGridView_TableSetting[1, i].Value, TableValue.ToString("N2"));
            }

            /*dataGridView_TableSetting[1, i].Value
            this.Invoke(new StringSend(Table_Text_Set), dataGridView_TableSetting, adcdata.ToString("N2"));*/


    
#endif

        }


        private void ATT_Enter(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                button_ATT_Click_1(null, null);
                //to do
            }
            else if(e.KeyCode == Keys.Escape)
            {
                Activi_Change__Setting(null,null);
            }
            else
            {
                return;
            }
        }
        private bool IamSetting = false;
        private void Activi_Change__SettingComplete(object sender, EventArgs e)
        {
            IamSetting = true;
        }

        private void Activi_Change__Setting(object sender, EventArgs e)
        {
            IamSetting = false;
        }
        public bool IamSetting_Get()
        {
            return IamSetting;
        }

        private void checkBox_Fix_CheckedChanged(object sender, EventArgs e)
        {


        }

        const byte TableMaxLength = 128 + 6; // Header + Type + Length + CRCINDEX + TableIndex + .....    +  CRC

        private void button_FileSave_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[TableMaxLength];
            byte tmp_h = 0 , tmp_l = 0;
            Int16 ret = 0;
           //temp_buf[] = dataGridView_TableSetting.Rows[r - 1].Cells[1].Value
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = (byte)Bluecell_BootProtocol.DATATYPE.ATT_TableSet;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = TableMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = (byte)comboBox_TableName.SelectedIndex;

            for (int i = 0; i < dataGridView_TableSetting.RowCount; i++)
            {
                if (comboBox_TableName.Text.Contains("DET") == true)
                    ret = Convert.ToInt16(Convert.ToDouble(dataGridView_TableSetting[1, i].Value) * 1000);
                else {
                    ret = Convert.ToInt16(Convert.ToDouble(dataGridView_TableSetting[1, i].Value) * 100);
                } 
                tmp_h = Convert.ToByte((ret & 0xFF00) >> 8);
                tmp_l = Convert.ToByte((ret & 0x00FF));
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i * 2 ] = tmp_h;
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + (i * 2) + 1] = tmp_l;
            }


            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;
        }
        const byte TableGetMaxLength =  1 + 6;
        private void button_FileLoad_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[TableGetMaxLength];

            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = (byte)Bluecell_BootProtocol.DATATYPE.ATT_TableGet;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = (byte)comboBox_TableName.SelectedIndex;
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[TableGetMaxLength];

            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_StatusSave;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = (byte)1;
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;

        }

        private void numericUpDown_User_KeyDown(object sender, KeyEventArgs e)
        {
            NumericUpDown numeric = (NumericUpDown)sender;
            const byte TableGetMaxLength = 8;
            byte tmp_H = 0;
            byte tmp_L = 0;

            if (e.KeyCode == Keys.Enter)
            {

                byte[] temp_buf = new byte[TableGetMaxLength];

                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length] = TableGetMaxLength - 3;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;
                Int16 UserData = Convert.ToInt16(numeric.Value * 100);

                tmp_H = Convert.ToByte((UserData & 0xFF00) >> 8);
                tmp_L = Convert.ToByte((UserData & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = tmp_H;
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 1] = tmp_L;
                switch (numeric.Name)
                {
                    case "numericUpDown_DL1_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] 
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DL1_USER;
                        break;
                    case "numericUpDown_DL2_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DL2_USER;
                        break;
                    case "numericUpDown_DL3_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DL3_USER;
                        break;
                    case "numericUpDown_DL4_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DL4_USER;
                        break;

                    case "numericUpDown_UL1_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_UL1_USER;
                        break;
                    case "numericUpDown_UL2_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_UL2_USER;
                        break;
                    case "numericUpDown_UL3_User":

                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_UL3_USER;
                        break;
                    case "numericUpDown_UL4_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_UL4_USER;
                        break;
                    case "numericUpDown_TEMP_User":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_TEMP_USER;
                        break;
                }
                serial.Serial_DataSend(temp_buf, temp_buf.Length);
                ReqTimer_Cnt = 0;

                //to do
            }
            else if (e.KeyCode == Keys.Escape)
            {
                Activi_Change__Setting(null, null);
            }
            else
            {
                return;
            }
        }
    }
}
