

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;
using System.Threading;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Jdas_Mbic
{

    
    public partial class JdasMbic : Form
    {
        const double Table_Dbm_DefaultValue = 0;//31.5;
        const int Table_DL_DefaultValue = 7;
        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
        };
        enum MBIC_TableNumber
        {
            DLI_P1_Level_Table_Number = 0x00,
            DLI_P2_Level_Table_Number = 0x01,
            DLI_P3_Level_Table_Number = 0x02,
            DLI_P4_Level_Table_Number = 0x03,
            ULO_P1_Level_Table_Number = 0x10,
            ULO_P2_Level_Table_Number = 0x11,
            ULO_P3_Level_Table_Number = 0x12,
            ULO_P4_Level_Table_Number = 0x13,
            DLI_P1_ATT_Temp_guarantee_Table_Number = 0x20,
            DLI_P2_ATT_Temp_guarantee_Table_Number = 0x21,
            DLI_P3_ATT_Temp_guarantee_Table_Number = 0x22,
            DLI_P4_ATT_Temp_guarantee_Table_Number = 0x23,
            ULO_P1_ATT_Temp_guarantee_Table_Number = 0x30,
            ULO_P2_ATT_Temp_guarantee_Table_Number = 0x31,
            ULO_P3_ATT_Temp_guarantee_Table_Number = 0x32,
            ULO_P4_ATT_Temp_guarantee_Table_Number = 0x33,
            DLI_P1_ATT_Accuracy_Table_Number = 0x40,
            DLI_P2_ATT_Accuracy_Table_Number = 0x41,
            DLI_P3_ATT_Accuracy_Table_Number = 0x42,
            DLI_P4_ATT_Accuracy_Table_Number = 0x43,
            ULO_P1_ATT_Accuracy_Table_Number = 0x50,
            ULO_P2_ATT_Accuracy_Table_Number = 0x51,
            ULO_P3_ATT_Accuracy_Table_Number = 0x52,
            ULO_P4_ATT_Accuracy_Table_Number = 0x53,
        }

        public JdasMbic()
        {
            InitializeComponent();
            
        }
        DataTable table = new DataTable();
        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 < 33; 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(sender, 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)
        {
            byte ret = 0;
            ret = serial.Serial_PortOpen(ref button_PortOpen, ref comboBox_Port);
            Serial_connectiondisable(ret);
            if (ret == 0 && 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(byte on_off)
        {
            if (on_off != 2)
            {
                comboBox_Port.Enabled = Convert.ToBoolean(on_off);
                comboBox_baudrate.Enabled = Convert.ToBoolean(on_off);
            }
            else
            {
                return false;
            }
            return Convert.ToBoolean(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);
        private delegate String Stringreturn(object combobox);
        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 = 9;
        public const byte BLUECELL_HEADER = 0xBE;
        public const byte BLUECELL_TAILER = 0xEB;


      

        const byte BLUECELL_ATTEN_LENGTH = 32 + 5;
#if false
        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 * 10);
            DL[1] = Convert.ToInt16(numericUpDown_ATT_DL2.Value * 10);
            DL[2] = Convert.ToInt16(numericUpDown_ATT_DL3.Value * 10);
            DL[3] = Convert.ToInt16(numericUpDown_ATT_DL4.Value * 10);

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

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

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

            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;

            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;
        }
#endif
        private void button_ATT_Click_2(object sender, EventArgs e)
        {
            Crc16 crc = new Crc16();
            byte[] temp_buf = new byte[TableGetMaxLength];
            NumericUpDown numeric = (NumericUpDown)sender;
            byte tmp_H = 0;
            byte tmp_L = 0;
            UInt16 Length = TableGetMaxLength - 3;
            Int16 UserData = Convert.ToInt16(numeric.Value * 10);
           

            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] = Convert.ToByte((Length & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte(Length & 0x00FF);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;

            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_ATT_DL1":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL1;
                    break;
                case "numericUpDown_ATT_DL2":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL2;
                    break;
                case "numericUpDown_ATT_DL3":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL3;
                    break;
                case "numericUpDown_ATT_DL4":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL4;
                    break;
                case "numericUpDown_ATT_UL1":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL1;
                    break;
                case "numericUpDown_ATT_UL2":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL2;
                    break;
                case "numericUpDown_ATT_UL3":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL3;
                    break;
                case "numericUpDown_ATT_UL4":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL4;
                    break;
                case "numericUpDown_DL1_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL1_USER;
                    break;
                case "numericUpDown_DL2_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL2_USER;
                    break;
                case "numericUpDown_DL3_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL3_USER;
                    break;
                case "numericUpDown_DL4_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_DL4_USER;
                    break;
                case "numericUpDown_UL1_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL1_USER;
                    break;
                case "numericUpDown_UL2_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL2_USER;
                    break;
                case "numericUpDown_UL3_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL3_USER;
                    break;
                case "numericUpDown_UL4_User":
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                        = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ATT_UL4_USER;
                    break;
            }
            UInt16 CRCRET = crc.CRC16_Generate(temp_buf, Length );
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 3] = Convert.ToByte(CRCRET & 0x00FF);
            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;
            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_DL_Shutdown_OFF":
                    pictureBox_DL_Shutdown_ON.Visible = true;
                    pictureBox_DL_Shutdown_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.Bluecell_DLI_Shutdown_ON_OFF;
                    break;

                case "pictureBox_DL_Shutdown_ON":
                    pictureBox_DL_Shutdown_OFF.Visible = true;
                    pictureBox_DL_Shutdown_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.Bluecell_DLI_Shutdown_ON_OFF;
                    break;
                case "pictureBox_UL_Shutdown_OFF":
                    pictureBox_UL_Shutdown_ON.Visible = true;
                    pictureBox_UL_Shutdown_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.Bluecell_ULO_Shutdown_ON_OFF;
                    break;

                case "pictureBox_UL_Shutdown_ON":
                    pictureBox_UL_Shutdown_OFF.Visible = true;
                    pictureBox_UL_Shutdown_ON.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.Bluecell_ULO_Shutdown_ON_OFF;
                    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.Bluecell_ULO_ALC_ON_OFF;
                    break;
                case "pictureBox_ALC1_OFF":
                    pictureBox_ALC1_ON.Visible = true;
                    pictureBox_ALC1_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.Bluecell_ULO_ALC_ON_OFF;
                    break;
                case "pictureBox_AGC1_ON":
                    pictureBox_AGC1_ON.Visible = false;
                    pictureBox_AGC1_OFF.Visible = true;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 0;
                    datatype = DATATYPE.Bluecell_DLI_AGC_ON_OFF;
                    break;
                case "pictureBox_AGC1_OFF":
                    pictureBox_AGC1_ON.Visible = true;
                    pictureBox_AGC1_OFF.Visible = false;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    datatype = DATATYPE.Bluecell_DLI_AGC_ON_OFF;
                    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_H] = ((6 & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = (6 & 0x00FF);
            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;
            UInt16 CRCRET = crc.CRC16_Generate(temp_buf, temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte((CRCRET & 0xFF00) >> 8);//CRC
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 3] = Convert.ToByte(CRCRET & 0x00FF);//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!!!");
                    AutoClosingMessageBox.Show("Save Success", "알림", 100);
                }
                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,
          bluecell_type,
          bluecell_length,
          bluecell_crcindex,
          Selftest1,
          Selftest2,
          Selftest3,
          Selftest4,
          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,
          ULO_P1_Level1_H,
          ULO_P1_Level1_L,
          ULO_P2_Level2_H,
          ULO_P2_Level2_L,
          ULO_P3_Level3_H,
          ULO_P3_Level3_L,
          ULO_P4_Level4_H,
          ULO_P4_Level4_L,
          DLI_P1_Level1_H,
          DLI_P1_Level1_L,
          DLI_P2_Level2_H,
          DLI_P2_Level2_L,
          DLI_P3_Level3_H,
          DLI_P3_Level3_L,
          DLI_P4_Level4_H,
          DLI_P4_Level4_L,
          DET_TEMP,
          DLI_AGC_ON_OFF,
          ULO_ALC_ON_OFF,
          ULO_Level1_H,
          ULO_Level1_L,
          ULO_Level2_H,
          ULO_Level2_L,
          ULO_Level3_H,
          ULO_Level3_L,
          ULO_Level4_H,
          ULO_Level4_L,
          DLI_Level1_H,
          DLI_Level1_L,
          DLI_Level2_H,
          DLI_Level2_L,
          DLI_Level3_H,
          DLI_Level3_L,
          DLI_Level4_H,
          DLI_Level4_L,
          ULO_ALC_Threshold_H,
          ULO_ALC_Threshold_L,
          bluecell_User_DL1_H,
          bluecell_User_DL1_L,
          bluecell_User_DL2_H,
          bluecell_User_DL2_L,
          bluecell_User_DL3_H,
          bluecell_User_DL3_L,
          bluecell_User_DL4_H,
          bluecell_User_DL4_L,
          bluecell_User_UL1_H,
          bluecell_User_UL1_L,
          bluecell_User_UL2_H,
          bluecell_User_UL2_L,
          bluecell_User_UL3_H,
          bluecell_User_UL3_L,
          bluecell_User_UL4_H,
          bluecell_User_UL4_L,
          bluecell_User_TEMP_H,
          bluecell_User_TEMP_L,
          bluecell_User_TEMP_OFFSET,
          Temp_High_Threshold,
          Temp_High_Threshold_Default,
          DLI_Level_High_Threshold_H,
          DLI_Level_High_Threshold_L,
          DLI_Level_Low_Threshold_H,
          DLI_Level_Low_Threshold_L,
          DLI_Level_High_Low_Threshold_default,
          ALARM_TEMP_HIGH, //bit
          ALARM_DLI_Level,
          ALARM_DLI_SHTUTDOWN,
          ALARM_DLI_AGC_Alarm,
          ALARM_ULO_ALC_Alarm,
          ALARM_ULO_Level,
          ALARM_ULO_SHTUTDOWN,
          ALARM_MASK1,
          ALARM_MASK2,
          ALARM_MASK3,
          ALARM_MASK4,
          ALARM_MASK5,
          ALARM_TESTMODE,
          ALARM_Test_Dummy1,
          ALARM_Test_Dummy2,
          ALARM_Test_Dummy3,
          ALARM_Test_Dummy4,
          ALARM_Test_Dummy5,
          CPUVERSION1,
          CPUVERSION2,
          CPUVERSION3,
          CPU_Current_Bank,
          CPU_Bank_Select,//Reboot_by,
          CPU_Bank1_Image_Version1,
          CPU_Bank1_Image_Version2,
          CPU_Bank1_Image_Version3,
          CPU_Bank1_Image_BuildTime1,
          CPU_Bank1_Image_BuildTime2,
          CPU_Bank1_Image_BuildTime3,
          CPU_Bank1_Image_BuildTime4,
          CPU_Bank1_Image_BuildTime5,
          CPU_Bank1_Image_BuildTime6,
          CPU_Bank1_Image_Name,
          CPU_Bank2_Image_Version1 = CPU_Bank1_Image_Name +32,
          CPU_Bank2_Image_Version2,
          CPU_Bank2_Image_Version3,
          CPU_Bank2_Image_BuildTime1,
          CPU_Bank2_Image_BuildTime2,
          CPU_Bank2_Image_BuildTime3,
          CPU_Bank2_Image_BuildTime4,
          CPU_Bank2_Image_BuildTime5,
          CPU_Bank2_Image_BuildTime6,
          CPU_Bank2_Image_Name,
          S_W_Reset=  CPU_Bank2_Image_Name + 32,
          Factory_Set_Initialization,
          Temp_High_Alarm,
          LED_TEST,
          NODE,
          Type,
          PCB_Version,
          Serial_Number = PCB_Version + 2, // INDEX : 20
          Manufacture = Serial_Number +20,
          Manufacture_Date,
          Freq_ID  = Manufacture_Date + 3,
          Carrier_ID,
          Carrier_ON_OFF,
          DLI_Level_High_Alarm1,
          DLI_Level_High_Alarm2,
          DLI_Level_High_Alarm3,
          DLI_Level_High_Alarm4,
          DLI_Level_Low_Alarm1,
          DLI_Level_Low_Alarm2,
          DLI_Level_Low_Alarm3,
          DLI_Level_Low_Alarm4,
          DLI_FRBT_Atten1_H,
          DLI_FRBT_Atten1_L,
          DLI_FRBT_Atten2_H,
          DLI_FRBT_Atten2_L,
          DLI_FRBT_Atten3_H,
          DLI_FRBT_Atten3_L,
          DLI_FRBT_Atten4_H,
          DLI_FRBT_Atten4_L,
          DLI_FRBT_D_Day,
          DLI_FRBT_Status,
          DLI_AGC_Threshold_H,
          DLI_AGC_Threshold_L,
          DLI_AGC_Threshold_default,
          DLI_Shutdown_ON_OFF,
          DLI_Shutdown_Threshold_H,
          DLI_Shutdown_Threshold_L,
          DLI_Shutdown_Threshold_Default,
          DLI_Shutdown_Retry_Count1,
          DLI_Shutdown_Retry_Count2,
          DLI_Shutdown_Retry_Count3,
          DLI_Shutdown_Retry_Count4,
          DLI_AGC_Alarm1,
          DLI_AGC_Alarm2,
          DLI_AGC_Alarm3,
          DLI_AGC_Alarm4,
          DLI_Shutdown_Alarm1,
          DLI_Shutdown_Alarm2,
          DLI_Shutdown_Alarm3,
          DLI_Shutdown_Alarm4,
          ULO_Level_High_Threshold_H,
          ULO_Level_High_Threshold_L,
          ULO_Level_High_Threshold_default,
          ULO_Level_High_Alarm1,
          ULO_Level_High_Alarm2,
          ULO_Level_High_Alarm3,
          ULO_Level_High_Alarm4,
          ULO_ALC_Threshold_Default,
          ULO_Shutdown_ON_OFF,
          ULO_Shutdown_Threshold_H,
          ULO_Shutdown_Threshold_L,
          ULO_Shutdown_Threshold_Default,
          ULO_Shutdown_Retry_Count1,
          ULO_Shutdown_Retry_Count2,
          ULO_Shutdown_Retry_Count3,
          ULO_Shutdown_Retry_Count4,
          ULO_ALC_Alarm1,
          ULO_ALC_Alarm2,
          ULO_ALC_Alarm3,
          ULO_ALC_Alarm4,
          ULO_Shutdown_Alarm1,
          ULO_Shutdown_Alarm2,
          ULO_Shutdown_Alarm3,
          ULO_Shutdown_Alarm4,
            MBIC_ULO_ALC_Atten1_H,
            MBIC_ULO_ALC_Atten1_L,
            MBIC_ULO_ALC_Atten2_H,
            MBIC_ULO_ALC_Atten2_L,
            MBIC_ULO_ALC_Atten3_H,
            MBIC_ULO_ALC_Atten3_L,
            MBIC_ULO_ALC_Atten4_H,
            MBIC_ULO_ALC_Atten4_L,
            MBIC_DLI_P1_Level_Table_Ref,
            MBIC_DLI_P2_Level_Table_Ref,
            MBIC_DLI_P3_Level_Table_Ref,
            MBIC_DLI_P4_Level_Table_Ref,
            MBIC_ULO_P1_Level_Table_Ref,
            MBIC_ULO_P2_Level_Table_Ref,
            MBIC_ULO_P3_Level_Table_Ref,
            MBIC_ULO_P4_Level_Table_Ref,
            MBIC_TEMP_DL_P1_Level_Table_Ref,
            MBIC_TEMP_DL_P2_Level_Table_Ref,
            MBIC_TEMP_DL_P3_Level_Table_Ref,
            MBIC_TEMP_DL_P4_Level_Table_Ref,
            MBIC_TEMP_UL_P1_Level_Table_Ref,
            MBIC_TEMP_UL_P2_Level_Table_Ref,
            MBIC_TEMP_UL_P3_Level_Table_Ref,
            MBIC_TEMP_UL_P4_Level_Table_Ref,
            MBIC_DLI_P1_Table_Length,
            MBIC_DLI_P2_Table_Length,
            MBIC_DLI_P3_Table_Length,
            MBIC_DLI_P4_Table_Length,
            MBIC_ULO_P1_Table_Length,
            MBIC_ULO_P2_Table_Length,
            MBIC_ULO_P3_Table_Length,
            MBIC_ULO_P4_Table_Length,
            MBIC_DLI_P1_Level_Table_Length,
            MBIC_DLI_P2_Level_Table_Length,
            MBIC_DLI_P3_Level_Table_Length,
            MBIC_DLI_P4_Level_Table_Length,
            MBIC_ULO_P1_Level_Table_Length,
            MBIC_ULO_P2_Level_Table_Length,
            MBIC_ULO_P3_Level_Table_Length,
            MBIC_ULO_P4_Level_Table_Length,
            MBIC_TEMP_DL_P1_Level_Table_Length,
            MBIC_TEMP_DL_P2_Level_Table_Length,
            MBIC_TEMP_DL_P3_Level_Table_Length,
            MBIC_TEMP_DL_P4_Level_Table_Length,
            MBIC_TEMP_UL_P1_Level_Table_Length,
            MBIC_TEMP_UL_P2_Level_Table_Length,
            MBIC_TEMP_UL_P3_Level_Table_Length,
            MBIC_TEMP_UL_P4_Level_Table_Length,
            BLUECELL_RESERVE49,
            BLUECELL_RESERVE50,
            BLUECELL_RESERVE51,
            BLUECELL_RESERVE52,
            BLUECELL_RESERVE53,
            bluecell_crc_H,
            bluecell_crc_L,
            bluecell_etx,


        };
        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 String ComboBox_Text_Get(object data)
        {
            ComboBox Temp_data = (ComboBox)data;
            return Temp_data.Text;
        }
        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(DataGridView table,object[] RefData, object[] AdcData, object Currdata)
        {
            /* 
             * RefData  : Dbm 값 
               AdcData  : Dbm과 매칭되어있는 ADC값 
               Currdata : 현재 ADC 값 
             */
            object data = Currdata; // 현재 ADC 값 
            double[] Vitual_array = new double[10];
            double step = 0;
            double temp,temp1;
            Double ret = 0xFF,Currentret = 0;
            int LastIndex = 0;
            int dot = 0;
            for(int i = 0; i < table.RowCount; i++)
            {
                try
                {
                    step = (Convert.ToDouble(AdcData[i]) - Convert.ToDouble(AdcData[i + 1])) *0.1;
                    for (int a = 0; a < 10; a++)
                    {
                        Vitual_array[a] = Convert.ToDouble(AdcData[i]) - (step * a);
                        /*현재 ADC 값과 테이블의 ADC값의 모든값들을 마이너스 합 후 */
                        Currentret = Convert.ToDouble(Vitual_array[a]) - Convert.ToDouble(data);


                        /*만약 0보다 작을 시 절대값을 취해준다.*/
                        if (Currentret < 0)
                        {
                            Currentret = (Currentret * -2) + Currentret;
                        }
                        /*만약 현재 결과 값이 이전 결과 값보다 
                         * 작을 경우 그값이 최종 결과 값이다.*/
                        if (ret >= Currentret)
                        {
                            ret = Currentret;
                            LastIndex = i;
                            dot = a;
                        }
                    }
                }
                catch { }
                

            }
            temp = Convert.ToDouble(AdcData[LastIndex]);
            temp1 = Convert.ToDouble(Currdata);
            if (temp1 < temp)
                return Convert.ToDouble(RefData[LastIndex + 1]) + (dot * 0.1);
            else
                return Convert.ToDouble(RefData[0]);

        }
        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(dataGridView_TableSetting,DET_DL1_RefTable, DET_DL1_Table, textBox_DET_DL1.Text);
                    break;
                case "textBox_DET_DL2":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_DL1_RefTable, DET_DL2_Table, textBox_DET_DL2.Text);
                    break;
                case "textBox_DET_DL3":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_DL1_RefTable, DET_DL3_Table, textBox_DET_DL3.Text);
                    break;
                case "textBox_DET_DL4":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_DL1_RefTable, DET_DL4_Table, textBox_DET_DL4.Text);
                    break;

                case "textBox_DET_UL1":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_UL1_RefTable, DET_UL1_Table, textBox_DET_UL1.Text);
                    break;
                case "textBox_DET_UL2":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_UL2_RefTable, DET_UL2_Table, textBox_DET_UL2.Text);
                    break;
                case "textBox_DET_UL3":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_UL3_RefTable, DET_UL3_Table, textBox_DET_UL3.Text);
                    break;
                case "textBox_DET_UL4":
                    ret = ADCValueTodBmConvert(dataGridView_TableSetting, DET_UL4_RefTable, DET_UL4_Table, textBox_DET_UL4.Text);
                    break;
            }
            return ret;
        }
        public void GUIDataStatusSet(byte[] buf)
        {
            double Volt_Calc_val = 0.001;//3.3 / 4095;
            double adcdata = 0;
            Int16 dBmdata = 0;
            short LiMitdata = 0;
            short AttData = 0;
            if (buf[(int)BluecellReqIndex.DLI_AGC_ON_OFF] == 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.ULO_ALC_ON_OFF] == 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.DLI_AGC_ON_OFF] == 0)
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL_Shutdown_ON, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL_Shutdown_OFF, true);// = false;
            }
            else
            {
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL_Shutdown_OFF, false);// = false;
                this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_UL_Shutdown_ON, true);// = false;
            }

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


            if (buf[(int)BluecellReqIndex.Selftest1] == 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.Selftest2] == 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.Selftest3] == 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.Selftest4] == 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;
            }

            if (buf[(int)BluecellReqIndex.ALARM_MASK1] == 1)
            {
                if (buf[(int)BluecellReqIndex.ALARM_TEMP_HIGH] != 0x80)
                {
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_ON, false);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_OFF, true);// = false;
                }
                else
                {
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_ON, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_OFF, false);// = false;
                }

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

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

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

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

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


                if (buf[(int)BluecellReqIndex.ALARM_ULO_SHTUTDOWN] == 0)
                {
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_ON, false);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_OFF, true);// = false;
                }
                else
                {
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_OFF, false);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_ON, true);// = false;
                }
            }
            else
            {
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_ON, false);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_TempHigh_Alarm_OFF, true);// = false;
              
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DLShutdown_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DLShutdown_Alarm_ON, false);// = false;
                
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_AGC_Alarm_ON, false);// = false;
               
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ALC_Alarm_ON, false);// = false;
               
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULOLevel_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULOLevel_Alarm_ON, false);// = false;

                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DLILevel_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_DLILevel_Alarm_ON, false);// = false;

                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_OFF, true);// = false;
                    this.Invoke(new SWVisible(PicVisible_Text_Set), pictureBox_ULShutdown_Alarm_ON, false);// = false;
            }
            this.Invoke(new SWVisible(CheckBoxVisible_Text_Set), checkBox_Fix, IamSetting_Get());// = false;
            if (IamSetting_Get() == false)
            {
                LiMitdata = 0;
                LiMitdata += buf[(int)BluecellReqIndex.Temp_High_Threshold];
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_Temp_Threshold, LiMitdata.ToString());
                LiMitdata = 0;
                LiMitdata += buf[(int)BluecellReqIndex.Temp_High_Threshold_Default];
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_Temp_Threshold_Default, LiMitdata.ToString("N2"));
                LiMitdata = 0;
                LiMitdata += buf[(int)BluecellReqIndex.bluecell_User_TEMP_OFFSET];
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_Temperature_Offset, LiMitdata.ToString());
                LiMitdata = 0;
                LiMitdata += buf[(int)BluecellReqIndex.ALARM_MASK1];
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_MASK, LiMitdata.ToString());
                LiMitdata = 0;


                LiMitdata += (short)((buf[(int)BluecellReqIndex.DLI_AGC_Threshold_H] << 8) & 0xFF00);
                LiMitdata += (short)(buf[(int)BluecellReqIndex.DLI_AGC_Threshold_L] & 0x00FF);
                LiMitdata /= 10;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DLI_AGC_Threshold, LiMitdata.ToString());

                LiMitdata = 0;
                LiMitdata += (short)((buf[(int)BluecellReqIndex.ULO_ALC_Threshold_H] << 8) & 0xFF00);
                LiMitdata += (short)(buf[(int)BluecellReqIndex.ULO_ALC_Threshold_L] & 0x00FF);
                LiMitdata /= 10;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ULO_ALC_Threshold, LiMitdata.ToString());
                AttData = (short)(((buf[(int)BluecellReqIndex.ATT_DL1_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ATT_DL1_L] & 0x00FF));
                adcdata = AttData *0.1;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL1, adcdata.ToString("N2"));

                AttData = (short)(((buf[(int)BluecellReqIndex.ATT_DL2_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ATT_DL2_L] & 0x00FF));
                adcdata = AttData *0.1;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL2, adcdata.ToString("N2"));

                AttData = (short)(((buf[(int)BluecellReqIndex.ATT_DL3_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ATT_DL3_L] & 0x00FF));
                adcdata = AttData *0.1;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL3, adcdata.ToString("N2"));

                AttData = (short)(((buf[(int)BluecellReqIndex.ATT_DL4_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ATT_DL4_L] & 0x00FF));
                adcdata = AttData *0.1;
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ATT_DL4, adcdata.ToString("N2"));


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


               

               
                //this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC4_MAX, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_DL1_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_DL1_L] & 0x00FF));
                adcdata = AttData *0.1;
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL1_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_DL2_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_DL2_L] & 0x00FF));
                adcdata = (AttData) *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_DL2_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_DL2_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL2_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_DL3_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_DL3_L] & 0x00FF));
                adcdata = AttData *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_DL3_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_DL3_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL3_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_DL4_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_DL4_L] & 0x00FF));
                adcdata = AttData *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_DL4_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_DL4_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DL4_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_UL1_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_UL1_L] & 0x00FF));
                adcdata = AttData *0.1;




                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_UL1_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_UL1_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL1_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_UL2_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_UL2_L] & 0x00FF));
                adcdata = AttData *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_UL2_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_UL2_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL2_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_UL3_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_UL3_L] & 0x00FF));
                adcdata = AttData *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_UL3_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_UL3_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL3_User, adcdata.ToString("N2"));
                adcdata = 0;
                AttData = (short)(((buf[(int)BluecellReqIndex.bluecell_User_UL4_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.bluecell_User_UL4_L] & 0x00FF));
                adcdata = AttData *0.1;
                //adcdata += ((buf[(int)BluecellReqIndex.bluecell_User_UL4_H] << 8) & 0xFF00);
                //adcdata += (buf[(int)BluecellReqIndex.bluecell_User_UL4_L] & 0x00FF);
                /*if ((((int)(adcdata) & 0xFF00) & 0xF000) == 0xF000)
                {
                    adcdata = (0xffff - adcdata) + 1;
                    adcdata *= -1;
                }
                adcdata = AttData *0.1;*/
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_UL4_User, adcdata.ToString("N2"));
                adcdata = 0;

                AttData = (short)(((buf[(int)BluecellReqIndex.DLI_Shutdown_Threshold_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.DLI_Shutdown_Threshold_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DLI_Shutdown_Threshold, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.ULO_Shutdown_Threshold_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ULO_Shutdown_Threshold_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ULO_Shutdown_Threshold, AttData.ToString());



                AttData = (short)(((buf[(int)BluecellReqIndex.DLI_Level_High_Threshold_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.DLI_Level_High_Threshold_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DLI_Level_High_Threshold, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.DLI_Level_Low_Threshold_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.DLI_Level_Low_Threshold_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_DLI_Level_Low_Threshold, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.ULO_Level_High_Threshold_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.ULO_Level_High_Threshold_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ULO_Level_High_Threshold, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten1_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten1_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                AttData = Convert.ToInt16(AttData *0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC1, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten2_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten2_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC2, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten3_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten3_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC3, AttData.ToString());

                AttData = (short)(((buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten4_H] << 8) & 0xFF00) | (buf[(int)BluecellReqIndex.MBIC_ULO_ALC_Atten4_L] & 0x00FF));
                AttData = Convert.ToInt16(AttData * 0.1);
                this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ALC4, AttData.ToString());
            }
            AttData = buf[(int)BluecellReqIndex.ULO_Shutdown_Retry_Count1];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownUL1, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.ULO_Shutdown_Retry_Count2];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownUL2, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.ULO_Shutdown_Retry_Count3];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownUL3, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.ULO_Shutdown_Retry_Count4];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownUL4, AttData.ToString());



            AttData = buf[(int)BluecellReqIndex.DLI_Shutdown_Retry_Count1];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownDL1, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.DLI_Shutdown_Retry_Count2];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownDL2, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.DLI_Shutdown_Retry_Count3];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownDL3, AttData.ToString());

            AttData = buf[(int)BluecellReqIndex.DLI_Shutdown_Retry_Count4];
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_ShutdownDL4, AttData.ToString());


            adcdata = 0;
            adcdata = ((buf[(int)BluecellReqIndex.DLI_P1_Level1_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DLI_P1_Level1_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1, adcdata.ToString("N3"));
            dBmdata = 0;
           dBmdata = (short)((buf[(int)BluecellReqIndex.DLI_Level1_H] << 8) & 0xFF00);
            dBmdata += (short)(buf[(int)BluecellReqIndex.DLI_Level1_L] & 0x00FF);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1_dBm, dBmdata.ToString());
            //this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_DL1_dBm, adcdata.ToString("N2"));
            adcdata = 0;

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

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

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


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

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

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

            adcdata = ((buf[(int)BluecellReqIndex.ULO_P4_Level4_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.ULO_P4_Level4_L] & 0x00FF);
            adcdata *= Volt_Calc_val;
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL4, adcdata.ToString("N3"));
            dBmdata = 0;
            dBmdata = (short)((buf[(int)BluecellReqIndex.ULO_Level4_H] << 8) & 0xFF00);
            dBmdata += (short)(buf[(int)BluecellReqIndex.ULO_Level4_L] & 0x00FF);
            this.Invoke(new StringSend(TextBox_Text_Set), textBox_DET_UL4_dBm, (dBmdata).ToString());
            adcdata = 0;
#if false
            adcdata += ((buf[(int)BluecellReqIndex.DET_TEMP_H] << 8) & 0xFF00);
            adcdata += (buf[(int)BluecellReqIndex.DET_TEMP_L] & 0x00FF);
            adcdata = adcdata * 3.3 / 4095;
            adcdata = (adcdata - 0.5) * 100;
#else
            adcdata = (buf[(int)BluecellReqIndex.DET_TEMP]);
#endif

            this.Invoke(new StringSend(Label_Text_Set), label_Temp, adcdata.ToString("N2") + "°C");
            adcdata = 0;
            string version = Convert.ToString(buf[(int)BluecellReqIndex.CPUVERSION1])
                             + "."
                             + Convert.ToString(buf[(int)BluecellReqIndex.CPUVERSION2])
                             + "."
                             + Convert.ToString(buf[(int)BluecellReqIndex.CPUVERSION3]);
            adcdata += (buf[(int)BluecellReqIndex.CPUVERSION2])/10;
            adcdata += (buf[(int)BluecellReqIndex.CPUVERSION3]);
            this.Invoke(new StringSend(Label_Text_Set), label_CPU_Version, version);


            if (buf[(int)BluecellReqIndex.DLI_AGC_ON_OFF] == 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.ULO_ALC_ON_OFF] == 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;
            }
          
            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;
            }
        }
        private Thread cpuThread;
        //private double[] cpuArray = new double[64];

        private void getPerformanceCounters()
        {
            var cpuPerfCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

            while (true)
            {
                //cpuArray[cpuArray.Length - 1] = Math.Round(cpuPerfCounter.NextValue(), 0);

                //Array.Copy(cpuArray, 1, cpuArray, 0, cpuArray.Length - 1);

                if (chart_Table.IsHandleCreated)
                {
                    this.Invoke((MethodInvoker)delegate { UpdateCpuChart(); });
                }
                else
                {
                    //......
                }

                Thread.Sleep(1000);
            }
        }
        private void UpdateCpuChart()
        {
            chart_Table.Series[0].Points.Clear();

            for (int i = 0; i < dataGridView_TableSetting.RowCount; ++i)
            {
                chart_Table.Series[0].Points.AddY(dataGridView_TableSetting[1, i].Value);
            }
        }

        private void TableChart_Create()
        {
            cpuThread = new Thread(new ThreadStart(this.getPerformanceCounters));
            cpuThread.IsBackground = true;
            cpuThread.Start();
        }


        
        public void TableDataStatusSet(byte[] buf)
        {
#if true
            Int16 TableValue = 0;
            double ret = 0;
            sbyte tempdata = 0;
            string data = Convert.ToString(this.Invoke(new Stringreturn(ComboBox_Text_Get), comboBox_TableName));
            bool Det_Att_Set = false;
            byte datalen_div = 2;
            int Length = 0; 
            int i = 0;
            int TableLength = 0;
            sbyte TableRef = 0;


            if (data.Contains("DET") == true)
            {
                Det_Att_Set = false;
                datalen_div = 2;
                Length = buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] << 8 | buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L];
                TableRef = (sbyte)buf[Length - 1];
                TableLength = buf[(Length )];
                Length -= 4;
                Length /= datalen_div;

            }
            else
            {
                Det_Att_Set = true;
                datalen_div = 1;
                Length = (buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] + 1 - 4) / datalen_div;
                TableRef = (sbyte)buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L + 1 + Length + 1];
                TableLength = buf[ Length + 2];
            }
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_TableRef, TableRef.ToString("N2"));
            this.Invoke(new StringSend(Numeric_Text_Set), numericUpDown_TableLength, TableLength.ToString("N2"));

            for (i = 0; i < Length; i++) {
                if(Det_Att_Set == false)
                {
                    TableValue = (short)((buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1 )+ (i * 2)] << 8) | buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 2) + (i * 2)]);
                    /*TableValue <<= 8;

                    TableValue = buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 2) +( i *  2) ];*/
                    ret = Convert.ToDouble(TableValue) / 1000;
                    if (ret > 4 || ret < 0)
                        TableValue = 0;



                }
                else
                {
                    tempdata =(SByte)((buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i]));//Convert.ToSByte((buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i]));

                    ret = Convert.ToDouble(tempdata) / 2;
                }
                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"));
            }
            

            TableChart_Create();

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



#endif

        }
        public void DateLabelInputFunc(string date)
        {
            this.Invoke(new StringSend(Label_Text_Set), label_CompileDate, date);
        }

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

                //button_ATT_Click_1(sender, null);
                button_ATT_Click_2(sender, 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 DET_TableMaxLength = 160 + 1+ 6 + 2; // Header + Type + Length + CRCINDEX + TableIndex + .....    +  CRC
        const byte ATT_TableMaxLength = 96 + 1 +  6 + 2; // Header + Type + Length + CRCINDEX + TableIndex + .....    +  CRC

        private void button_FileSave_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[DET_TableMaxLength];
            string tempstr;
            Int16 detret;
            int a=0 ,i = 0;
            int addsendLength = 0;
            byte tmp_h, tmp_l, attret;
            try
            {

                ComboBox cb = (ComboBox)comboBox_TableName;
                tempstr = cb.Text;
            }
            catch { tempstr = comboBox_TableName.Text; }
            byte type = 0;
            switch (tempstr)
            {
                case "ATT_DL1": type = (byte)MBIC_TableNumber.DLI_P1_ATT_Accuracy_Table_Number; break;
                case "ATT_DL2": type = (byte)MBIC_TableNumber.DLI_P2_ATT_Accuracy_Table_Number; break;
                case "ATT_DL3": type = (byte)MBIC_TableNumber.DLI_P3_ATT_Accuracy_Table_Number; break;
                case "ATT_DL4": type = (byte)MBIC_TableNumber.DLI_P4_ATT_Accuracy_Table_Number; break;
                case "ATT_UL1": type = (byte)MBIC_TableNumber.ULO_P1_ATT_Accuracy_Table_Number; break;
                case "ATT_UL2": type = (byte)MBIC_TableNumber.ULO_P2_ATT_Accuracy_Table_Number; break;
                case "ATT_UL3": type = (byte)MBIC_TableNumber.ULO_P3_ATT_Accuracy_Table_Number; break;
                case "ATT_UL4": type = (byte)MBIC_TableNumber.ULO_P4_ATT_Accuracy_Table_Number; break;
                case "DET_DL1": type = (byte)MBIC_TableNumber.DLI_P1_Level_Table_Number; break;
                case "DET_DL2": type = (byte)MBIC_TableNumber.DLI_P2_Level_Table_Number; break;
                case "DET_DL3": type = (byte)MBIC_TableNumber.DLI_P3_Level_Table_Number; break;
                case "DET_DL4": type = (byte)MBIC_TableNumber.DLI_P4_Level_Table_Number; break;
                case "DET_UL1": type = (byte)MBIC_TableNumber.ULO_P1_Level_Table_Number; break;
                case "DET_UL2": type = (byte)MBIC_TableNumber.ULO_P2_Level_Table_Number; break;
                case "DET_UL3": type = (byte)MBIC_TableNumber.ULO_P3_Level_Table_Number; break;
                case "DET_UL4": type = (byte)MBIC_TableNumber.ULO_P4_Level_Table_Number; break;
                case "TEMP_DL1": type = (byte)MBIC_TableNumber.DLI_P1_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL2": type = (byte)MBIC_TableNumber.DLI_P2_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL3": type = (byte)MBIC_TableNumber.DLI_P3_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL4": type = (byte)MBIC_TableNumber.DLI_P4_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL1": type = (byte)MBIC_TableNumber.ULO_P1_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL2": type = (byte)MBIC_TableNumber.ULO_P2_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL3": type = (byte)MBIC_TableNumber.ULO_P3_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL4": type = (byte)MBIC_TableNumber.ULO_P4_ATT_Temp_guarantee_Table_Number; break;

                default:
                    MessageBox.Show("존재하지 않는 ComboBox 입니다.\r\n 확인해주세요.");
                    break;
            }
            //temp_buf[] = dataGridView_TableSetting.Rows[r - 1].Cells[1].Value
            Int16 Length = Convert.ToByte(DET_TableMaxLength - 4);
            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_H] = Convert.ToByte((Length & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((Length & 0x00FF));
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = DET_TableMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = (byte)type;

            if (comboBox_TableName.Text.Contains("DET") == true)
            {
                for (i = 0; i < dataGridView_TableSetting.RowCount; i++)
                {
                    detret = Convert.ToInt16(Convert.ToDouble(dataGridView_TableSetting[1, i].Value) * 1000);
                    tmp_h = Convert.ToByte((detret & 0xFF00) >> 8);
                    tmp_l = Convert.ToByte((detret & 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;
                }
                addsendLength = 160 - i;
                for (a = i * 2; a < i + addsendLength - 2; a++)
                {
                    attret = 0;
                    temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a] = (attret);
                }
                sbyte tmp = (sbyte)numericUpDown_TableRef.Value;
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 0] = (byte)(tmp & 0xFF);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 1]   = Convert.ToByte(numericUpDown_TableLength.Value);
                UInt16 CRCRET = crc.CRC16_Generate(temp_buf, temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 2] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 3] = Convert.ToByte(CRCRET & 0x00FF);
                serial.Serial_DataSend(temp_buf, DET_TableMaxLength);
            }
            else
            {
                 Length = Convert.ToByte(ATT_TableMaxLength - 4);
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] = Convert.ToByte((Length & 0xFF00) >> 8);
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((Length & 0x00FF));
                for (i = 0; i < dataGridView_TableSetting.RowCount; i++)
                {
                    attret = Convert.ToByte(Convert.ToSByte(Convert.ToDouble(dataGridView_TableSetting[1, i].Value) * 2) & 0xFF);
                    
                    temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i] = (attret);
                }
                
                for (a = i; a < i + 30; a++)
                {
                    attret = 0;

                    temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + i] = (attret);
                }
                sbyte tmpdata = (sbyte)(numericUpDown_TableRef.Value);
                byte data = Convert.ToByte(tmpdata & 0xFF);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 0] = data;
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 1] = Convert.ToByte(numericUpDown_TableLength.Value);

                UInt16 CRCRET = crc.CRC16_Generate(temp_buf, temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 2] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
                temp_buf[(int)(Bluecell_ProtIndex_p.Bluecell_data + 1) + a + 3] = Convert.ToByte(CRCRET & 0x00FF);
                serial.Serial_DataSend(temp_buf, ATT_TableMaxLength);

            }

            
            ReqTimer_Cnt = 0;
        }
        const byte TableGetMaxLength =  1 + 7 + 1;


        private void button_FileLoad_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[TableGetMaxLength];
            string tempstr;
            try
            {

                ComboBox cb = (ComboBox)sender;
                tempstr = cb.Text;
            }
            catch { tempstr = comboBox_TableName.Text; }
            byte type = 0;
            switch (tempstr)
            {
                case "ATT_DL1": type = (byte)MBIC_TableNumber.DLI_P1_ATT_Accuracy_Table_Number; break;
                case "ATT_DL2": type = (byte)MBIC_TableNumber.DLI_P2_ATT_Accuracy_Table_Number; break;
                case "ATT_DL3": type = (byte)MBIC_TableNumber.DLI_P3_ATT_Accuracy_Table_Number; break;
                case "ATT_DL4": type = (byte)MBIC_TableNumber.DLI_P4_ATT_Accuracy_Table_Number; break;
                case "ATT_UL1": type = (byte)MBIC_TableNumber.ULO_P1_ATT_Accuracy_Table_Number; break;
                case "ATT_UL2": type = (byte)MBIC_TableNumber.ULO_P2_ATT_Accuracy_Table_Number; break;
                case "ATT_UL3": type = (byte)MBIC_TableNumber.ULO_P3_ATT_Accuracy_Table_Number; break;
                case "ATT_UL4": type = (byte)MBIC_TableNumber.ULO_P4_ATT_Accuracy_Table_Number; break;
                case "DET_DL1": type = (byte)MBIC_TableNumber.DLI_P1_Level_Table_Number; break;
                case "DET_DL2": type = (byte)MBIC_TableNumber.DLI_P2_Level_Table_Number; break;
                case "DET_DL3": type = (byte)MBIC_TableNumber.DLI_P3_Level_Table_Number; break;
                case "DET_DL4": type = (byte)MBIC_TableNumber.DLI_P4_Level_Table_Number; break;
                case "DET_UL1": type = (byte)MBIC_TableNumber.ULO_P1_Level_Table_Number; break;
                case "DET_UL2": type = (byte)MBIC_TableNumber.ULO_P2_Level_Table_Number; break;
                case "DET_UL3": type = (byte)MBIC_TableNumber.ULO_P3_Level_Table_Number; break;
                case "DET_UL4": type = (byte)MBIC_TableNumber.ULO_P4_Level_Table_Number; break;
                case "TEMP_DL1": type = (byte)MBIC_TableNumber.DLI_P1_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL2": type = (byte)MBIC_TableNumber.DLI_P2_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL3": type = (byte)MBIC_TableNumber.DLI_P3_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_DL4": type = (byte)MBIC_TableNumber.DLI_P4_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL1": type = (byte)MBIC_TableNumber.ULO_P1_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL2": type = (byte)MBIC_TableNumber.ULO_P2_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL3": type = (byte)MBIC_TableNumber.ULO_P3_ATT_Temp_guarantee_Table_Number; break;
                case "TEMP_UL4": type = (byte)MBIC_TableNumber.ULO_P4_ATT_Temp_guarantee_Table_Number; break;
                    
                default:
                    MessageBox.Show("존재하지 않는 ComboBox 입니다.\r\n 확인해주세요.");
                    break;
            }
            

            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_H] = Convert.ToByte((TableGetMaxLength - 4) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((TableGetMaxLength - 4) & 0x00FF);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = type;
            UInt16 CRCRET = crc.CRC16_Generate(temp_buf, temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 1] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte(CRCRET & 0x00FF );
            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];
            Int16 Length = 
            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_H] = 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 = 9;
            byte tmp_H = 0;
            byte tmp_L = 0;
            ReqTimer_Cnt = 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_H] = Convert.ToByte(((TableGetMaxLength - 3) & 0xFF00) >> 8);
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((TableGetMaxLength - 3) & 0x00FF);
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;

                if (   numeric.Name == "numericUpDown_Temp_Threshold"
                    || numeric.Name == "numericUpDown_Temp_Threshold_Default"
                    || numeric.Name == "numericUpDown_Temperature_Offset"
                    || numeric.Name.Contains("Default") == true)
                {
                    Int16 UserData = Convert.ToInt16(numeric.Value);
                    //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] = tmp_L;
                }
                else
                {
                    Int16 UserData = Convert.ToInt16(numeric.Value * 10);
                    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_Temp_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_Temp_High_Threshold;
                        break;
                    case "numericUpDown_Temp_Threshold_Default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_Temp_High_Threshold_Default;
                        break;
                    case "numericUpDown_Temperature_Offset":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_Temperature_Offset;
                        break;

                    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;
                    case "numericUpDown_DLI_Level_High_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Level_High_Threshold;
                        break;
                    case "numericUpDown_DLI_Level_Low_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Level_Low_Threshold;
                        break;
                    case "numericUpDown_DLI_Level_High_Low_Threshold_default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Level_High_Low_Threshold_default;
                        break;
                    case "numericUpDown_DLI_AGC_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_AGC_Threshold;
                        break;
                    case "numericUpDown_DLI_AGC_Threshold_Default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_AGC_Threshold_Default;
                        break;
                    case "numericUpDown_DLI_Shutdown_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Shutdown_Threshold;
                        break;
                    case "numericUpDown_DLI_Shutdown_Threshold_Default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Shutdown_Threshold_Default;
                        break;
                    case "numericUpDown_ULO_Level_High_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ULO_Level_High_Threshold;
                        break;
                    case "numericUpDown_DLI_Level_High_Threshold_default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_DLI_Level_High_Low_Threshold_default;
                        break;
                    case "numericUpDown_ULO_ALC_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ULO_ALC_Threshold;
                        break;
                    case "numericUpDown_ULO_ALC_Threshold_Default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ULO_ALC_Threshold_Default;
                        break;
                    case "numericUpDown_ULO_Shutdown_Threshold":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ULO_Shutdown_Threshold;
                        break;
                    case "numericUpDown_ULO_Shutdown_Threshold_Default":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_ULO_Shutdown_Threshold_Default;
                        break;
                    case "numericUpDown_MASK":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_Alarm_Mask;
                        break;
                    case "numericUpDown_ALC1":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                            = (byte)Bluecell_BootProtocol.DATATYPE.BLuecell_ATT_ALC1;
                        break;
                    case "numericUpDown_ALC2":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.BLuecell_ATT_ALC2;
                        break;
                    case "numericUpDown_ALC3":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.BLuecell_ATT_ALC3;
                        break;
                    case "numericUpDown_ALC4":
                        temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type]
                           = (byte)Bluecell_BootProtocol.DATATYPE.BLuecell_ATT_ALC4;
                        break;



                    default:break;
                }
                Crc16 crc = new Crc16();
                UInt16 CRCRET = crc.CRC16_Generate(temp_buf, Convert.ToByte((TableGetMaxLength - 3) & 0x00FF));
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
                temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 3] = Convert.ToByte(CRCRET & 0x00FF);

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

        private void button_Reset_Click(object sender, EventArgs e)
        {
            label_CompileDate.Text = "";
            byte[] temp_buf = new byte[TableGetMaxLength];
         //   byte[] Temp_MBICResetCmd = { 0x16, 0x16, 0x16, 0x16, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x48, 0x00, 0x14, 0x06, 0x09, 0x00, 0x14, 0x33, 0x00, 0x01, 0x00, 0x04, 0xa7, 0xe0, 0x0e, 0x01, 0x00, 0xac, 0xda, 0x03 };
           // serial.Serial_DataSend(Temp_MBICResetCmd, Temp_MBICResetCmd.Length);
            //return;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = (byte)Bluecell_BootProtocol.DATATYPE.BLUECELL_SOFTWARERESET;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] = Convert.ToByte((TableGetMaxLength - 2 & 0xFF00 )>> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((TableGetMaxLength - 2 & 0x00FF));
            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_ATT_DL1_ValueChanged(object sender, EventArgs e)
        {

        }
        
        private Thread[] rTh = new Thread[2];

        private void button_AllExcelSave_Click(object sender, EventArgs e)
        {
            //Thread 객체 생성. 생성시 이 Thread가 실행할 메소드를 지정해 준다. 
            //여기서는 Count라는 메소드를 rTh라는 쓰레드가 실행하게 될 것이다.

            rTh[0] = new Thread(ExcellTableAllSave);
            rTh[0].Start();
        }
        // Thread로 수행할 작업
        private void ExcellTableAllSave()
        {
            for (int i = 0; i < comboBox_TableName.Items.Count; i++)
            {
                CheckForIllegalCrossThreadCalls = false;
                comboBox_TableName.SelectedIndex = i;
                Thread.Sleep(1000);
                button_ExcelSave_Click(null, null);
            }
            rTh[0].Abort(); //쓰레드 강제 종료
            Console.WriteLine("Thread 종료~");
        }

        private void button_LedTest_Click(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[BLUECELL_RESET_LENGTH];
            Button button = (Button)sender;
            DATATYPE datatype;
            ReqTimer_Cnt = 0;
            switch (button.Name)
            {
                case "button_LedTest":
                    datatype = DATATYPE.Bluecell_LED_TEST;
                    temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = 1;
                    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_H] = 0;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = 6;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = 5;
            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);
        }
        UInt32[] crc32_tab = {
        0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
        0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
        0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
        0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
        0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
        0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
        0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
        0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
        0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
        0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
        0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
        0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
        0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
        0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
        0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
        0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
        0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
        0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
        0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
        0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
        0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
        0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
        0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
        0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
        0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
        0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
        0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
        0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
        0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
        0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
        0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
        0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
        0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
        0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
        0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
        0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
        0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
        0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
        0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
        0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
        0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
        0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
        0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
    };

        UInt32 crc32(byte[] buf, UInt32 size)						
        {						
            
            UInt32 crcret = 0;
            int a = 0;

            
            crcret = crcret ^ ~0U;						
						
            while (size-- > 0) {						
                //printf("index : size :  %d    buf %x \r\n", size,* p);
                crcret = crc32_tab[(crcret ^ buf[a++]) & 0xFF] ^ (crcret >> 8);			
            }						
						
            return crcret ^ ~0U;						
        }

public byte[] MBIC_BootLoader_DataArray;
        byte[] MBIC_Add_BootLoader_DataArray = new byte[128];
        const string MBIC_HeaderName = "JT-NRDAS ";
        
        private byte[] DataToConvertHex(string date)
        {
            char[] tmpret;
            byte[] ret = new byte[6];
            tmpret = date.ToCharArray();

            for (int i = 0; i < date.Length/2; i++)
            {
                ret[i] = Convert.ToByte((tmpret[i * 2] & 0x0F) * 10) ;
                ret[i] += Convert.ToByte((tmpret[(i * 2) + 1] & 0x0F));
            }

            return ret;
        }


        Crc16 crc = new Crc16();

        byte[] MBIC_BootLoaderData;
        private void button_Test_Click(object sender, EventArgs e)
        {
            string MBIC_HeaderFileName = "jhfs-mbic-nrdas-v00.00." + textBox_Version.Text+ ".mbc";
            char[] tempchardata;
            OpenFileDialog Mbic_ofd = new OpenFileDialog();
            UInt32 Crcret = 0;
            byte[] tempdata = new byte[1027];
            int i = 0;
            timer_JdasMbic.Stop(); //타이머를 멈춘다.
            //파일오픈창 생성 및 설정
            Mbic_ofd.Title = "업데이터 파일 탐색기";
            Mbic_ofd.FileName = "*.bin";
            Mbic_ofd.Filter = "binary 파일 (*.binary,*.bin) | *.binary;, *.bin; | 모든 파일 (*.*) | *.*";
            string s3 = DateTime.Now.ToString("yyMMddHHmmss");
            //string s3 = DateTime.Now.ToString("yyMMdd");

            //파일 오픈창 로드
            DialogResult dr = Mbic_ofd.ShowDialog();

            //OK버튼 클릭시
            if (dr == DialogResult.OK)
            {
                //File명과 확장자를 가지고 온다.
                string fileName = Mbic_ofd.SafeFileName;
                
                //File경로와 File명을 모두 가지고 온다.
                string fileFullName = Mbic_ofd.FileName;
                
                //File경로만 가지고 온다.
                string filePath = fileFullName.Replace(fileName, "");
                //this.serial.debug_hextoasciiConvert();

                //출력 예제용 로직
                //  label1.Text = "File Name  : " + fileName;
                // label2.Text = "Full Name  : " + fileFullName;
                // label3.Text = "File Path  : " + filePath;`
                //File경로 + 파일명 리턴
                //JT-NRDAS 

                tempchardata = MBIC_HeaderName.ToCharArray();
                for(i = 0; i < tempchardata.Length; i++)
                {
                    MBIC_Add_BootLoader_DataArray[i] = Convert.ToByte(tempchardata[i]);
                }
                MBIC_Add_BootLoader_DataArray[9] = 0x20; //file type : MBIC 0x20
                MBIC_Add_BootLoader_DataArray[10] = 0x00; //file type : CPU 0x00
                MBIC_Add_BootLoader_DataArray[11] = 0x00;//version
                MBIC_Add_BootLoader_DataArray[12] = 0x00;//version
                MBIC_Add_BootLoader_DataArray[13] = 0x01;//version

                tempchardata = MBIC_HeaderFileName.ToCharArray();
                for (i = 14; i < 14 + tempchardata.Length + 12; i++) // Total 41byte Filename
                {
                    try
                    {
                        MBIC_Add_BootLoader_DataArray[i] = Convert.ToByte(tempchardata[i - 14]);
                    }
                    catch
                    {
                        MBIC_Add_BootLoader_DataArray[i] = 0x20;
                    }
                }
                
                tempchardata = s3.ToCharArray();
                for (i = 55; i < 54 + tempchardata.Length / 2 + 1; i++)
                {
                    MBIC_Add_BootLoader_DataArray[i] = DataToConvertHex(s3)[i - 55];
                }

                MBIC_BootLoader_DataArray = File.ReadAllBytes(Mbic_ofd.FileName);
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128) & 0xFF00) >> 8);
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128)& 0x00FF));

                Crcret = crc32(MBIC_BootLoader_DataArray, Convert.ToUInt32(MBIC_BootLoader_DataArray.Length));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0xFF000000) >> (8 * 3));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x00FF0000) >> (8 * 2));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x0000FF00) >> (8 * 1));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x000000FF) >> (8 * 0));

                for (i = 0; i < 62; i++)
                    MBIC_Add_BootLoader_DataArray[66 + i] = 0x00;

                byte[] RetBinary = new byte[MBIC_Add_BootLoader_DataArray.Length + MBIC_BootLoader_DataArray.Length];
                Array.Copy(MBIC_Add_BootLoader_DataArray, 0, RetBinary, 0, MBIC_Add_BootLoader_DataArray.Length);
                Array.Copy(MBIC_BootLoader_DataArray, 0, RetBinary, MBIC_Add_BootLoader_DataArray.Length, MBIC_BootLoader_DataArray.Length);

//                System.IO.FileStream infile = new System.IO.FileStream("FileName.bin", System.IO.FileMode.Create); //파일 스트림 선언
                //infile.Write(RetBinary, 0, RetBinary.Length);    //쓰기
//                infile.Close();                  //꼭 닫아 주세요. 파일 입출력엔 엄청 중요하죠.

                MBIC_BootLoaderData = bluecell_BootProtocol.MBIC_HeaderMergeFunction(RetBinary, RetBinary.Length);

                /*Download Start Request*/
                /*tempdata[0] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128) & 0xFF000000) >> 24);
                tempdata[1] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128) & 0x00FF0000) >> 16);
                tempdata[2] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128) & 0x0000FF00) >> 8);
                tempdata[3] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length + 128) & 0x000000FF));*/
                tempdata[0] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length) & 0xFF000000) >> 24);
                tempdata[1] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length) & 0x00FF0000) >> 16);
                tempdata[2] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length) & 0x0000FF00) >> 8);
                tempdata[3] = Convert.ToByte(((MBIC_BootLoader_DataArray.Length) & 0x000000FF));
                tempdata[4] = 0;
                tempdata[5] = 0;
                tempdata[6] = 0;
                tempdata[7] = 0;
                tempdata = bluecell_BootProtocol.MBIC_HeaderMergeFunction(tempdata, 8);
                tempdata[(int)MBICProt_st.MBIC_CMD_0] = 0x10;
                tempdata[(int)MBICProt_st.MBIC_HEADERCHECKSUM_0] = crc.Chksum_Create(tempdata);
                this.serial.Serial_DataSend(tempdata, 8 + 22 + 3);
                //this.serial.Serial_DataSend(MBIC_BootLoaderData, 1024 + 22 + 3);

                /*    
                    tempdata[0] = 0xbe;
                    tempdata[1] = Termianl_Reset;
                    tempdata[2] = 0x02;
                    tempdata[3] = STH30_CreateCrc(tempdata, tempdata[2]);
                    tempdata[4] = 0xeb;
                    this.serial.Serial_DataSend(tempdata, 5);*/
                try
                {
                    // Controller_Debug.Controller_TX_TextLoad(tempdata);
                }
                catch { }
            }
            //취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
            else if (dr == DialogResult.Cancel)
            {
            }
        }
        private void button_Make_Click(object sender, EventArgs e)
        {
            char[] tempchardata;
            OpenFileDialog Mbic_ofd = new OpenFileDialog();
            UInt32 Crcret = 0;
            byte[] tempdata = new byte[1027];
            int i = 0;
            /*string dec = ;
            string hex = Convert.ToString(int.Parse(dec, 16).ToUpper();*/
            string hex = "24";
            string dec = Convert.ToInt32(hex, 10).ToString();


            string MBIC_HeaderFileName = "jhfs-mbic-nrdas-v00.00." + textBox_Version.Text + ".mbc";
            //현재 사용자 바탕화면 경로

            string localpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            timer_JdasMbic.Stop(); //타이머를 멈춘다.
            //파일오픈창 생성 및 설정
            Mbic_ofd.Title = "업데이터 파일 탐색기";
            Mbic_ofd.FileName = "*.bin";
            Mbic_ofd.Filter = "binary 파일 (*.binary,*.bin) | *.binary;, *.bin; | 모든 파일 (*.*) | *.*";
            string s3 = DateTime.Now.ToString("yyMMddHHmmss");
            //string s3 = DateTime.Now.ToString("yyMMdd");

            //파일 오픈창 로드
            DialogResult dr = Mbic_ofd.ShowDialog();

            //OK버튼 클릭시
            if (dr == DialogResult.OK)
            {
                //File명과 확장자를 가지고 온다.
                string fileName = Mbic_ofd.SafeFileName;

                //File경로와 File명을 모두 가지고 온다.
                string fileFullName = Mbic_ofd.FileName;

                //File경로만 가지고 온다.
                string filePath = fileFullName.Replace(fileName, "");
                //this.serial.debug_hextoasciiConvert();

                //출력 예제용 로직
                //  label1.Text = "File Name  : " + fileName;
                // label2.Text = "Full Name  : " + fileFullName;
                // label3.Text = "File Path  : " + filePath;`
                //File경로 + 파일명 리턴
                //JT-NRDAS 

                tempchardata = MBIC_HeaderName.ToCharArray();
                for (i = 0; i < tempchardata.Length; i++)
                {
                    MBIC_Add_BootLoader_DataArray[i] = Convert.ToByte(tempchardata[i]);
                }
                MBIC_Add_BootLoader_DataArray[9] = 0x20; //file type : MBIC 0x20
                MBIC_Add_BootLoader_DataArray[10] = 0x00; //file type : CPU 0x00
                MBIC_Add_BootLoader_DataArray[11] = 0x00;//version
                MBIC_Add_BootLoader_DataArray[12] = 0x00;//version
                MBIC_Add_BootLoader_DataArray[13] = Convert.ToByte(textBox_Version.Text,16);//version

                tempchardata = MBIC_HeaderFileName.ToCharArray();
                for (i = 14; i < 14 + tempchardata.Length + 12; i++) // Total 41byte Filename
                {
                    try
                    {
                        MBIC_Add_BootLoader_DataArray[i] = Convert.ToByte(tempchardata[i - 14]);
                    }
                    catch
                    {
                        MBIC_Add_BootLoader_DataArray[i] = 0x20;
                    }
                }

                tempchardata = s3.ToCharArray();
                for (i = 55; i < 55 + tempchardata.Length / 2 ; i++)
                {
                    MBIC_Add_BootLoader_DataArray[i] = DataToConvertHex(s3)[i - 55];
                }

                MBIC_BootLoader_DataArray = File.ReadAllBytes(Mbic_ofd.FileName);

                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((MBIC_BootLoader_DataArray.Length & 0xFF000000) >> (8 * 3));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((MBIC_BootLoader_DataArray.Length & 0x00FF0000) >> (8 * 2));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((MBIC_BootLoader_DataArray.Length & 0x0000FF00) >> (8 * 1));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((MBIC_BootLoader_DataArray.Length & 0x000000FF) >> (8 * 0));

                Crcret = crc32( MBIC_BootLoader_DataArray, Convert.ToUInt32(MBIC_BootLoader_DataArray.Length));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0xFF000000) >> (8 * 3));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x00FF0000) >> (8 * 2));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x0000FF00) >> (8 * 1));
                MBIC_Add_BootLoader_DataArray[i++] = Convert.ToByte((Crcret & 0x000000FF) >> (8 * 0));

                for (i = 0; i < 59; i++)
                    MBIC_Add_BootLoader_DataArray[69 + i] = 0x00;

                byte[] RetBinary = new byte[MBIC_Add_BootLoader_DataArray.Length + MBIC_BootLoader_DataArray.Length];
                Array.Copy(MBIC_Add_BootLoader_DataArray, 0, RetBinary, 0, MBIC_Add_BootLoader_DataArray.Length);
                Array.Copy(MBIC_BootLoader_DataArray, 0, RetBinary, MBIC_Add_BootLoader_DataArray.Length, MBIC_BootLoader_DataArray.Length);

                System.IO.FileStream infile = new System.IO.FileStream(localpath+"\\" + MBIC_HeaderFileName, System.IO.FileMode.Create); //파일 스트림 선언
                infile.Write(RetBinary, 0, RetBinary.Length);    //쓰기d
                infile.Close();                  //꼭 닫아 주세요. 파일 입출력엔 엄청 중요하죠.

             


            }
            //취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
            else if (dr == DialogResult.Cancel)
            {
            }
        }

        private void button_AllExcelLoad_Click(object sender, EventArgs e)
        {
            //Thread 객체 생성. 생성시 이 Thread가 실행할 메소드를 지정해 준다. 
            //여기서는 Count라는 메소드를 rTh라는 쓰레드가 실행하게 될 것이다.

            rTh[1] = new Thread(ExcellTableAllLoad);
            rTh[1].Start();
        }
        private void ExcellTableAllLoad()
        {
            for (int i = 0; i < comboBox_TableName.Items.Count; i++)
            {
                CheckForIllegalCrossThreadCalls = false;
                comboBox_TableName.SelectedIndex = i;
                Thread.Sleep(1000);
                button_ExcelLoad_Click(null, null);
                Thread.Sleep(1000);
                button_FileSave_Click(null,null);
                Thread.Sleep(2000);
            }
            rTh[1].Abort(); //쓰레드 강제 종료
            Console.WriteLine("Thread 종료~");
        }

        private void comboBox_BankSel_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte[] temp_buf = new byte[9];
            byte Length = 5;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Header] = BLUECELL_HEADER;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Type] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_BankSelect;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] = 0;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = 5;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = 5;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = Convert.ToByte(Convert.ToByte(comboBox_BankSel.SelectedIndex) + 1);
            UInt16 CRCRET = crc.CRC16_Generate(temp_buf, Length);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 1] = Convert.ToByte(CRCRET & 0xFF00 >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte(CRCRET & 0x00FF);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 3] = 0xeb;
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;
        }
        public IEnumerable<int> ExtractNumbers(string text)
        {
            text += " ";
            var temp = string.Empty;
            for (var i = 0; i < text.Length; i++)
            {
                if (char.IsDigit(text[i]))
                {
                    if ('-'.Equals(text[i - 1]))
                    {
                        temp += text[i - 1];
                    }
                    temp += text[i];
                }
                else if (temp.Length > 0)
                {
                    yield return int.Parse(temp);
                    temp = string.Empty;
                }
            }
        }
        private void button_TableAttribute_Default_Click(object sender, EventArgs e)
        {
            string targetStr = dataGridView_TableSetting.Rows[0].Cells[0].Value.ToString();
            string text = targetStr;
            var match = Regex.Match(text, "-?\\d+");
            
            ///정수형 변환
           // int rstInt = int.Parse(tempStr);

            //_body = Regex.Replace(dataGridView_TableSetting.Rows[0].Cells[0].Value.ToString(), @"[^0-9]", "");

            numericUpDown_TableLength.Value = dataGridView_TableSetting.Rows.Count;
            numericUpDown_TableRef.Value = Convert.ToDecimal(match.Value);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            label_CompileDate.Text = "";
            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_SOFTWARERESET;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_H] = Convert.ToByte((TableGetMaxLength - 4 & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_Length_L] = Convert.ToByte((TableGetMaxLength - 4 & 0x00FF));
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_CrcIndex] = TableGetMaxLength - 2;
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data] = (byte)1;
            UInt16 CRCRET = crc.CRC16_Generate(temp_buf, TableGetMaxLength - 4);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 1] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 2] = Convert.ToByte(CRCRET & 0x00FF);
            temp_buf[(int)Bluecell_ProtIndex_p.Bluecell_data + 3] = 0xeb;
            serial.Serial_DataSend(temp_buf, temp_buf.Length);
            ReqTimer_Cnt = 0;
        }

        private void button_FRBT_TimeSetting_Click(object sender, EventArgs e)
        {
            string Time = textBox_FRBT_TIME.Text;
            string[] Timearray = Time.Split('-');
            int i = 0;

            byte[] Timedata = new byte[6 + 4 + 3]; //Header CRC 4byte TYPE LENGTH 3byte
            Timedata[0] = 0xbe;
            Timedata[1] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_TimeSetting;
            Timedata[2] = 0;
            Timedata[3] = (byte)(Timedata.Length - 4);
            for (i = 0; i < 6; i++)
                Timedata[4 + i] = Convert.ToByte(Timearray[i]);

            UInt16 CRCRET = crc.CRC16_Generate(Timedata, Timedata[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            Timedata[4 + i++] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            Timedata[4 + i++] = Convert.ToByte(CRCRET & 0x00FF);
            serial.Serial_DataSend(Timedata, Timedata.Length);


        }

        private void button_FactorySet_Click(object sender, EventArgs e)
        {
            byte[] tempdata = new byte[1 + 4 + 3]; //Header CRC 4byte TYPE LENGTH 3byte
            tempdata[0] = 0xbe;
            tempdata[1] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_Factory_Set;
            tempdata[2] = 0;
            tempdata[3] = (byte)(tempdata.Length - 4);
            tempdata[5] = 1;
            UInt16 CRCRET = crc.CRC16_Generate(tempdata, tempdata[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            tempdata[6] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            tempdata[7] = Convert.ToByte(CRCRET & 0x00FF);
            serial.Serial_DataSend(tempdata, tempdata.Length);

        }

        private void button_Carrier_Click(object sender, EventArgs e)
        {
            byte[] tempdata = new byte[1 + 4 + 3]; //Header CRC 4byte TYPE LENGTH 3byte
            tempdata[0] = 0xbe;
            tempdata[1] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_CarrierOnOff;
            tempdata[2] = 0;
            tempdata[3] = (byte)(tempdata.Length - 4);
            tempdata[5] = 1;
            UInt16 CRCRET = crc.CRC16_Generate(tempdata, tempdata[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            tempdata[6] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            tempdata[7] = Convert.ToByte(CRCRET & 0x00FF);
            serial.Serial_DataSend(tempdata, tempdata.Length);
        }

        private void button_Carrier_OFF_Click(object sender, EventArgs e)
        {
            byte[] tempdata = new byte[1 + 4 + 3]; //Header CRC 4byte TYPE LENGTH 3byte
            tempdata[0] = 0xbe;
            tempdata[1] = (byte)Bluecell_BootProtocol.DATATYPE.Bluecell_CarrierOnOff;
            tempdata[2] = 0;
            tempdata[3] = (byte)(tempdata.Length - 4);
            tempdata[5] = 0;
            UInt16 CRCRET = crc.CRC16_Generate(tempdata, tempdata[(int)Bluecell_ProtIndex_p.Bluecell_Length_L]);
            tempdata[6] = Convert.ToByte((CRCRET & 0xFF00) >> 8);
            tempdata[7] = Convert.ToByte(CRCRET & 0x00FF);
            serial.Serial_DataSend(tempdata, tempdata.Length);
        }

        private void button_Down_Click(object sender, EventArgs e)
        {
            object[] temp = new object[dataGridView_TableSetting.RowCount];
            for (int i = 0; i < dataGridView_TableSetting.RowCount; i++)
            {
                temp[i] =dataGridView_TableSetting[1, i].Value;
            }
            dataGridView_TableSetting[1, 0].Value = 0;
            dataGridView_TableSetting[1, 1].Value = 0;
            for (int i = 0; i < dataGridView_TableSetting.RowCount - 2; i++)
            {
                dataGridView_TableSetting[1, i + 2].Value = temp[i];
            }
        }
    }
    class AutoClosingMessageBox
    {
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        System.Threading.Timer _timeoutTimer; //쓰레드 타이머
        string _caption;

        const int WM_CLOSE = 0x0010; //close 명령 

        AutoClosingMessageBox(string text, string caption, int timeout)
        {
            _caption = caption;
            _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                null, timeout, System.Threading.Timeout.Infinite);
            MessageBox.Show(text, caption);
        }

        //생성자 함수
        public static void Show(string text, string caption, int timeout)
        {
            new AutoClosingMessageBox(text, caption, timeout);
        }

        //시간이 다되면 close 메세지를 보냄

        void OnTimerElapsed(object state)
        {
            IntPtr mbWnd = FindWindow(null, _caption);
            if (mbWnd != IntPtr.Zero)
                SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            _timeoutTimer.Dispose();
        }
    }
}
