using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//Add code
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices; //ini파일 로드 필요라이브러리


namespace Atten_Table_Ctrl   
{

    public partial class Bluecell_AttenTable_Ctrl : Form
    {
        public const byte Bluecell_STX = 0;
        public const byte Bluecell_Type = 1;
        public const byte Bluecell_Length = 2;
        public const byte Bluecell_DATA = 3;
        /*****************************************/
        public const byte TABLE_UARTDATA_LENGTH = 64;
        public const byte TABLE_UART_LENGTH = 69;

        public const byte CHSET_UARTDATA_LENGTH = 4;
        public const byte CHSET_UART_LENGTH = 9;
        /*****************************************/
        public const byte ATTEN_0DB = 0;
        public const double ATTEN_0_5DB = 0.5;
        public const byte ATTEN_1DB = 1;
        public const byte ATTEN_2DB = 2;
        public const byte ATTEN_4DB = 4;
        public const byte ATTEN_8DB = 8;
        public const byte ATTEN_16DB = 16;
        public const byte ATTEN_31DB = 31;
        public const double ATTEN_31_5DB = 31.5;

        public const byte QPC6614_ATTEN_0DB_VAL = 0x3F;
        public const byte QPC6614_ATTEN_0_5DB_VAL = 0x3E;
        public const byte QPC6614_ATTEN_1DB_VAL = 0x3D;
        public const byte QPC6614_ATTEN_2DB_VAL = 0x3B;
        public const byte QPC6614_ATTEN_4DB_VAL = 0x37;
        public const byte QPC6614_ATTEN_8DB_VAL = 0x2F;
        public const byte QPC6614_ATTEN_16DB_VAL = 0x1F;
        public const byte QPC6614_ATTEN_30DB_VAL = 0x03;
        public const byte QPC6614_ATTEN_31_5DB_VAL = 0x00;

        public const byte PE43711_ATTEN_0DB_VAL = 0x00;
        public const byte PE43711_ATTEN_0_5DB_VAL = 0x02;
        public const byte PE43711_ATTEN_1DB_VAL = 0x04;
        public const byte PE43711_ATTEN_2DB_VAL = 0x08;
        public const byte PE43711_ATTEN_4DB_VAL = 0x10;
        public const byte PE43711_ATTEN_8DB_VAL = 0x20;
        public const byte PE43711_ATTEN_16DB_VAL = 0x40;
        public const byte PE43711_ATTEN_30DB_VAL =   PE43711_ATTEN_2DB_VAL | PE43711_ATTEN_4DB_VAL | PE43711_ATTEN_8DB_VAL | PE43711_ATTEN_16DB_VAL;
        public const byte PE43711_ATTEN_31_5DB_VAL = PE43711_ATTEN_0DB_VAL | PE43711_ATTEN_0_5DB_VAL  | PE43711_ATTEN_1DB_VAL  | PE43711_ATTEN_2DB_VAL| PE43711_ATTEN_4DB_VAL| PE43711_ATTEN_8DB_VAL | PE43711_ATTEN_16DB_VAL;
        public const byte PE43711_ATTEN_31_75DB_VAL = 0x7F;


        public const byte HMC939_ATTEN_0DB_VAL = 0x1F;
        public const byte HMC939_ATTEN_1DB_VAL = 0x1E;
        public const byte HMC939_ATTEN_2DB_VAL = 0x1D;
        public const byte HMC939_ATTEN_4DB_VAL = 0x1B;
        public const byte HMC939_ATTEN_8DB_VAL = 0x17;
        public const byte HMC939_ATTEN_16DB_VAL = 0x0F;
        public const byte HMC939_ATTEN_30DB_VAL = 0x01;
        public const byte HMC939_ATTEN_31DB_VAL = 0x00;


        public const byte ADRF5720_ATTEN_31_5DB_VAL = 0x3F;
        public const byte ADRF5720_ATTEN_30DB_VAL = 0x3E;
        public const byte ADRF5720_ATTEN_16DB_VAL = 0x20;
        public const byte ADRF5720_ATTEN_8DB_VAL = 0x10;
        public const byte ADRF5720_ATTEN_4DB_VAL = 0x08;
        public const byte ADRF5720_ATTEN_2DB_VAL = 0x04;
        public const byte ADRF5720_ATTEN_1DB_VAL = 0x02;
        public const byte ADRF5720_ATTEN_0_5DB_VAL = 0x01;
        public const byte ADRF5720_ATTEN_0DB_VAL = 0x00;


        public const byte QPC6614_ATTEN_31_5DB_Trace = 0x3F;
        public const byte QPC6614_ATTEN_16DB_Trace = 0x20;
        public const byte QPC6614_ATTEN_8DB_Trace = 0x10;
        public const byte QPC6614_ATTEN_4DB_Trace = 0x08;
        public const byte QPC6614_ATTEN_2DB_Trace = 0x04;
        public const byte QPC6614_ATTEN_1DB_Trace = 0x02;
        public const byte QPC6614_ATTEN_0_5DB_Trace = 0x01;
        public const byte QPC6614_ATTEN_0DB_Trace = 0x00;

        public const byte PE43711_ATTEN_31_75DB_Trace = (~PE43711_ATTEN_31_75DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_31_5DB_Trace = (~PE43711_ATTEN_31_5DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_30DB_Trace = (~PE43711_ATTEN_30DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_16DB_Trace = (~PE43711_ATTEN_16DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_8DB_Trace = (~PE43711_ATTEN_8DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_4DB_Trace = (~PE43711_ATTEN_4DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_2DB_Trace = (~PE43711_ATTEN_2DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_1DB_Trace = (~PE43711_ATTEN_1DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_0_5DB_Trace = (~PE43711_ATTEN_0_5DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        public const byte PE43711_ATTEN_0DB_Trace = (~PE43711_ATTEN_0DB_VAL) & PE43711_ATTEN_31_75DB_VAL;
        /************************Index************************************/
        public const byte CH_A_ATT_EN_150M = 0;
        public const byte CH_A_ATT_EN_WIFI1_2_4Ghz = 1;
        public const byte CH_A_ATT_EN_WIFI2_2_4Ghz = 2;
        public const byte CH_A_ATT_EN_WIFI3_2_4Ghz = 3;
        public const byte CH_A_ATT_EN_WIFI4_2_4Ghz = 4;
        public const byte CH_A_ATT_EN_WIFI1_5_8Ghz = 5;
        public const byte CH_A_ATT_EN_WIFI2_5_8Ghz = 6;
        public const byte CH_A_ATT_EN_WIFI3_5_8Ghz = 7;
        public const byte CH_A_ATT_EN_WIFI4_5_8Ghz = 8;
        public const byte CH_A_ATT_EN_30G1_28_28_5Ghz = 9;
        public const byte CH_A_ATT_EN_30G2_28_28_5Ghz = 10;
        public const byte CH_A_ATT_EN_30G3_28_28_5Ghz = 11;
        public const byte CH_A_ATT_EN_30G1_28_5_29Ghz = 12;
        public const byte CH_A_ATT_EN_30G2_28_5_29Ghz = 13;
        public const byte CH_A_ATT_EN_30G3_28_5_29Ghz = 14;
        public const byte CH_A_ATT_EN_30G1_29_29_5Ghz = 15;
        public const byte CH_A_ATT_EN_30G2_29_29_5Ghz = 16;
        public const byte CH_A_ATT_EN_30G3_29_29_5Ghz = 17;
        public const byte CH_A_ATT_EN_30G1_29_5_30Ghz = 18;
        public const byte CH_A_ATT_EN_30G2_29_5_30Ghz = 19;
        public const byte CH_A_ATT_EN_30G3_29_5_30Ghz = 20;
        public const byte CH_B_ATT_EN_150M = 21;
        public const byte CH_B_ATT_EN_WIFI1_2_4Ghz = 22;
        public const byte CH_B_ATT_EN_WIFI2_2_4Ghz = 23;
        public const byte CH_B_ATT_EN_WIFI3_2_4Ghz = 24;
        public const byte CH_B_ATT_EN_WIFI4_2_4Ghz = 25;
        public const byte CH_B_ATT_EN_WIFI1_5_8Ghz = 26;
        public const byte CH_B_ATT_EN_WIFI2_5_8Ghz = 27;
        public const byte CH_B_ATT_EN_WIFI3_5_8Ghz = 28;
        public const byte CH_B_ATT_EN_WIFI4_5_8Ghz = 29;
        public const byte CH_B_ATT_EN_30G1_28_28_5Ghz = 30;
        public const byte CH_B_ATT_EN_30G2_28_28_5Ghz = 31;
        public const byte CH_B_ATT_EN_30G3_28_28_5Ghz = 32;
        public const byte CH_B_ATT_EN_30G1_28_5_29Ghz = 33;
        public const byte CH_B_ATT_EN_30G2_28_5_29Ghz = 34;
        public const byte CH_B_ATT_EN_30G3_28_5_29Ghz = 35;
        public const byte CH_B_ATT_EN_30G1_29_29_5Ghz = 36;
        public const byte CH_B_ATT_EN_30G2_29_29_5Ghz = 37;
        public const byte CH_B_ATT_EN_30G3_29_29_5Ghz = 38;
        public const byte CH_B_ATT_EN_30G1_29_5_30Ghz = 39;
        public const byte CH_B_ATT_EN_30G2_29_5_30Ghz = 40;
        public const byte CH_B_ATT_EN_30G3_29_5_30Ghz = 41;
       

        /************************A Channel********************************/
        public const string CH_A_ATT_EN_150M_Str  = "CH_A_ATT_EN_150M";
        public const string CH_A_ATT_EN_WIFI1_Str = "CH_A_ATT_EN_WIFI1";
        public const string CH_A_ATT_EN_WIFI2_Str = "CH_A_ATT_EN_WIFI2";
        public const string CH_A_ATT_EN_WIFI3_Str = "CH_A_ATT_EN_WIFI3";
        public const string CH_A_ATT_EN_WIFI4_Str = "CH_A_ATT_EN_WIFI4";
        public const string CH_A_ATT_EN_30G1_Str  = "CH_A_ATT_EN_30G1";
        public const string CH_A_ATT_EN_30G2_Str  = "CH_A_ATT_EN_30G2";
        public const string CH_A_ATT_EN_30G3_Str  = "CH_A_ATT_EN_30G3";
        /************************B Channel********************************/
        public const string CH_B_ATT_EN_150M_Str = "CH_B_ATT_EN_150M";
        public const string CH_B_ATT_EN_WIFI1_Str = "CH_B_ATT_EN_WIFI1";
        public const string CH_B_ATT_EN_WIFI2_Str = "CH_B_ATT_EN_WIFI2";
        public const string CH_B_ATT_EN_WIFI3_Str = "CH_B_ATT_EN_WIFI3";
        public const string CH_B_ATT_EN_WIFI4_Str = "CH_B_ATT_EN_WIFI4";
        public const string CH_B_ATT_EN_30G1_Str  = "CH_B_ATT_EN_30G1";
        public const string CH_B_ATT_EN_30G2_Str  = "CH_B_ATT_EN_30G2";
        public const string CH_B_ATT_EN_30G3_Str  = "CH_B_ATT_EN_30G3";
        /************************A_CH_ATTN_CTRL 제어***************************/
        const byte ATT_AB_CH_Read = 0x00;
        const byte ATT_A_CH_150M = 0x01;
        const byte ATT_A_CH_WIFI2_4Ghz = 0x02;
        const byte ATT_A_CH_WIFI5_8Ghz = 0x03;
        const byte ATT_A_CH_30G_28_28_5 = 0x04;
        const byte ATT_A_CH_30G_28_5_29 = 0x05;
        const byte ATT_A_CH_30G_29_29_5 = 0x06;
        const byte ATT_A_CH_30G_29_5_30 = 0x07;
        const byte ATT_B_CH_150M = 0x08;
        const byte ATT_B_CH_WIFI2_4Ghz = 0x09;
        const byte ATT_B_CH_WIFI5_8Ghz = 0x0A;
        const byte ATT_B_CH_30G_28_28_5 = 0x0B;
        const byte ATT_B_CH_30G_28_5_29 = 0x0C;
        const byte ATT_B_CH_30G_29_29_5 = 0x0D;
        const byte ATT_B_CH_30G_29_5_30 = 0x0E;
        /****************************ATTN TABLE Write**********************************/
        const byte ATT_A_EN_150M = 0x10;
        const byte ATT_A_EN_WIFI1_2_4Ghz = 0x11;
        const byte ATT_A_EN_WIFI2_2_4Ghz = 0x12;
        const byte ATT_A_EN_WIFI3_2_4Ghz = 0x13;
        const byte ATT_A_EN_WIFI4_2_4Ghz = 0x14;
        const byte ATT_A_EN_WIFI1_5_8Ghz = 0x15;
        const byte ATT_A_EN_WIFI2_5_8Ghz = 0x16;
        const byte ATT_A_EN_WIFI3_5_8Ghz = 0x17;
        const byte ATT_A_EN_WIFI4_5_8Ghz = 0x18;
        const byte ATT_A_EN_30G1_28_28_5Ghz = 0x19;
        const byte ATT_A_EN_30G2_28_28_5Ghz = 0x1A;
        const byte ATT_A_EN_30G3_28_28_5Ghz = 0x1E;
        const byte ATT_A_EN_30G1_28_5_29Ghz = 0x1F;
        const byte ATT_A_EN_30G2_28_5_29Ghz = 0x20;
        const byte ATT_A_EN_30G3_28_5_29Ghz = 0x21;
        const byte ATT_A_EN_30G1_29_29_5Ghz = 0x22;
        const byte ATT_A_EN_30G2_29_29_5Ghz = 0x23;
        const byte ATT_A_EN_30G3_29_29_5Ghz = 0x24;
        const byte ATT_A_EN_30G1_30_30_5Ghz = 0x25;
        const byte ATT_A_EN_30G2_30_30_5Ghz = 0x26;
        const byte ATT_A_EN_30G3_30_30_5Ghz = 0x27;
        const byte ATT_B_EN_150M = 0x28;
        const byte ATT_B_EN_WIFI1_2_4Ghz = 0x29;
        const byte ATT_B_EN_WIFI2_2_4Ghz = 0x2A;
        const byte ATT_B_EN_WIFI3_2_4Ghz = 0x2B;
        const byte ATT_B_EN_WIFI4_2_4Ghz = 0x2C;
        const byte ATT_B_EN_WIFI1_5_8Ghz = 0x2D;
        const byte ATT_B_EN_WIFI2_5_8Ghz = 0x2E;
        const byte ATT_B_EN_WIFI3_5_8Ghz = 0x2F;
        const byte ATT_B_EN_WIFI4_5_8Ghz = 0x30;
        const byte ATT_B_EN_30G1_28_28_5Ghz = 0x31;
        const byte ATT_B_EN_30G2_28_28_5Ghz = 0x32;
        const byte ATT_B_EN_30G3_28_28_5Ghz = 0x33;
        const byte ATT_B_EN_30G1_28_5_29Ghz = 0x34;
        const byte ATT_B_EN_30G2_28_5_29Ghz = 0x35;
        const byte ATT_B_EN_30G3_28_5_29Ghz = 0x36;
        const byte ATT_B_EN_30G1_29_29_5Ghz = 0x37;
        const byte ATT_B_EN_30G2_29_29_5Ghz = 0x38;
        const byte ATT_B_EN_30G3_29_29_5Ghz = 0x39;
        const byte ATT_B_EN_30G1_30_30_5Ghz = 0x3A;
        const byte ATT_B_EN_30G2_30_30_5Ghz = 0x3B;
        const byte ATT_B_EN_30G3_30_30_5Ghz = 0x3C;
        /****************************ATTN TABLE Read**********************************/
        const byte ATT_A_EN_150M_R = 0x40;
        const byte ATT_A_EN_WIFI1_2_4Ghz_R = 0x41;
        const byte ATT_A_EN_WIFI2_2_4Ghz_R = 0x42;
        const byte ATT_A_EN_WIFI3_2_4Ghz_R = 0x43;
        const byte ATT_A_EN_WIFI4_2_4Ghz_R = 0x44;
        const byte ATT_A_EN_WIFI1_5_8Ghz_R = 0x45;
        const byte ATT_A_EN_WIFI2_5_8Ghz_R = 0x46;
        const byte ATT_A_EN_WIFI3_5_8Ghz_R = 0x47;
        const byte ATT_A_EN_WIFI4_5_8Ghz_R = 0x48;
        const byte ATT_A_EN_30G1_28_28_5Ghz_R	=	0x49;
        const byte ATT_A_EN_30G2_28_28_5Ghz_R	=	0x4A;
        const byte ATT_A_EN_30G3_28_28_5Ghz_R	=	0x4E;
        const byte ATT_A_EN_30G1_28_5_29Ghz_R	=	0x4F;
        const byte ATT_A_EN_30G2_28_5_29Ghz_R	=	0x50;
        const byte ATT_A_EN_30G3_28_5_29Ghz_R	=	0x51;
        const byte ATT_A_EN_30G1_29_29_5Ghz_R	=	0x52;
        const byte ATT_A_EN_30G2_29_29_5Ghz_R	=	0x53;
        const byte ATT_A_EN_30G3_29_29_5Ghz_R	=	0x54;
        const byte ATT_A_EN_30G1_30_30_5Ghz_R	=	0x55;
        const byte ATT_A_EN_30G2_30_30_5Ghz_R	=	0x56;
        const byte ATT_A_EN_30G3_30_30_5Ghz_R	=	0x57;
        const byte ATT_B_EN_150M_R = 0x58;
        const byte ATT_B_EN_WIFI1_2_4Ghz_R = 0x59;
        const byte ATT_B_EN_WIFI2_2_4Ghz_R = 0x5A;
        const byte ATT_B_EN_WIFI3_2_4Ghz_R = 0x5B;
        const byte ATT_B_EN_WIFI4_2_4Ghz_R = 0x5C;
        const byte ATT_B_EN_WIFI1_5_8Ghz_R = 0x5D;
        const byte ATT_B_EN_WIFI2_5_8Ghz_R = 0x5E;
        const byte ATT_B_EN_WIFI3_5_8Ghz_R = 0x5F;
        const byte ATT_B_EN_WIFI4_5_8Ghz_R = 0x60;
        const byte ATT_B_EN_30G1_28_28_5Ghz_R	=	0x61;
        const byte ATT_B_EN_30G2_28_28_5Ghz_R	=	0x62;
        const byte ATT_B_EN_30G3_28_28_5Ghz_R	=	0x63;
        const byte ATT_B_EN_30G1_28_5_29Ghz_R	=	0x64;
        const byte ATT_B_EN_30G2_28_5_29Ghz_R	=	0x65;
        const byte ATT_B_EN_30G3_28_5_29Ghz_R	=	0x66;
        const byte ATT_B_EN_30G1_29_29_5Ghz_R	=	0x67;
        const byte ATT_B_EN_30G2_29_29_5Ghz_R	=	0x68;
        const byte ATT_B_EN_30G3_29_29_5Ghz_R	=	0x69;
        const byte ATT_B_EN_30G1_30_30_5Ghz_R	=	0x6A;
        const byte ATT_B_EN_30G2_30_30_5Ghz_R	=	0x6B;
        const byte ATT_B_EN_30G3_30_30_5Ghz_R	=	0x6C;
        const byte ATT_AB_ALARM_READ            =   0x6D;




        private CheckBox allCheck = new CheckBox();
        delegate void SetTextCallback(string Text);
        delegate void SetByteCallback(byte[] data);
        delegate void SetintCallback(int Text);
        public Bluecell_AttenTable_Ctrl()
        {
            InitializeComponent();

            this.serialPort1 = new System.IO.Ports.SerialPort();
            this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.SP1_DataReceived);

            //port
            //Put every available ports in comboBox
            //드라이버에 잡히는 것 순서대로 명령 리스트 시켜줌
            comboBox1.BeginUpdate();
            foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
            {
                comboBox1.Items.Add(comport);
            }
            comboBox1.EndUpdate();

            //SerialPort 초기 설정.
            comboBox1.DataSource = SerialPort.GetPortNames();
            serialPort1.BaudRate = (int)115200;
            serialPort1.DataBits = (int)8;
            serialPort1.Parity = System.IO.Ports.Parity.None;
            serialPort1.StopBits = StopBits.One;


            //Serial Form _Off button 설정
            PortClose_button.Enabled = false;  //off 기능 사라짐
            PortClose_button.Visible = false; //off 버튼 숨겨짐
            SerialFormEnagleChoice(true); //serial 설정버튼 on
            DataTableSet();
            //  DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();

            allCheck.Name = "allCheck";
            allCheck.CheckedChanged += new EventHandler(AllCheckClick);
            allCheck.Size = new Size(13, 13);
            allCheck.Location = new Point(dataGridView1.RowHeadersWidth + 33, (dataGridView1.ColumnHeadersHeight / 2) - (allCheck.Height / 2));//dataGridView1.Columns[1].Width - 20, (dataGridView1.ColumnHeadersHeight / 2) - (allCheck.Height / 2));
            dataGridView1.Controls.Add(allCheck); // DataGridView에 CheckBox 추가( 헤더용 )
                                                  //    dataGridView1.Columns.Insert(6, column1);
                                                  //dataGridView1.Rows.Add(typeof(CheckBox));
            Alarm_Init();

        }
        static string path = System.IO.Directory.GetCurrentDirectory();
        static string fileName = @"\config.ini"; //파일명
        string inipath = path + fileName;
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(    // GetIniValue 를 위해
            String section,
            String key,
            String def,
            StringBuilder retVal,
            int size,
            String filePath);

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(  // SetIniValue를 위해
            String section,
            String key,
            String val,
            String filePath);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

        public String GetIniValue(String Section, String Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, inipath);
            return temp.ToString();
        }

        // INI 값을 셋팅
        public void SetIniValue(String Section, String Key, String Value)
        {
            WritePrivateProfileString(Section, Key, Value, inipath);
        }
        
        private void DataTableSet()
        {
            string tempstr;
            double tempval;
            DataTable dt = new DataTable("Table1");//DataTable 정의

            //Column 정의
            DataColumn nameColumn = new DataColumn("DB", typeof(string));
            DataColumn ageColumn = new DataColumn("offset", typeof(string));
            dt.Columns.Add(nameColumn);
            dt.Columns.Add(ageColumn);

            //Row 생성
            DataRow row = dt.NewRow();
           // row[nameColumn] = "31.5" +" dB";
           // row[ageColumn] = "-0.5";
            tempstr = "31.5";
            tempval=Convert.ToDouble(tempstr);

           
            //dt.Rows.Add(row);
            for (int i = 0; i < 64; i++)
            {
                row = dt.NewRow();
               
             
                tempstr = string.Format("{0:F1}", tempval);//Convert.ToString(tempval);
                row[nameColumn] = tempstr + " dB";
                if(i < 25)
                    row[ageColumn] = "-0.5";
                else
                    row[ageColumn] = "0.0";
                dt.Rows.Add(row);
                tempval -= 0.5;
            }
            //Grid에 값 세팅
            this.dataGridView1.DataSource = dt;
        }
        string g_data; // 전역 변수(혹은 멤버 변수)

        private void SP1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string temp_data;

            //string receiveData = "";
            //포트로 데이터가 들어오면
            if (serialPort1.IsOpen)
            {

                //  Atten_UartGet_Data(data);
                /* if (g_data.Contains("\r\n") == true)
                {
                    temp_data = g_data;
                    g_data = "";
                }*/
                if (Ascii_checkBox.Checked == true)
                {
                    string data = serialPort1.ReadExisting();
                    g_data += data;
                    if (data != string.Empty)
                    {
                        PacketData(data);
                    }
                }
                else
                {

                    try
                    {
                        // 리스트 두개 사용
                        int nLnegth = 0;
                        nLnegth = serialPort1.BytesToRead;
                        byte[] btTemp = new byte[nLnegth];
                        serialPort1.Read(btTemp, 0, nLnegth);

                        if (nLnegth > 0)
                        {
                            if (Termainal_metroCheckBox.Checked == true)
                            {
                               
                                PacketData(btTemp);
                            }
                            CheckForIllegalCrossThreadCalls = false;
                            Atten_UartGet_Data(btTemp);
                            // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
                        }
                    }
                    catch (Exception ex)
                    {
                        if (serialPort1.IsOpen)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.ToString());
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }
        }

    private void Atten_QPC6614_TraceValue_Func(int value, out double ret)
    {
        int Temp_data = 0;
        ret = ATTEN_0DB;
        if (value != 0) Temp_data = (~value) & 0x3F;
        if (value != 0) Temp_data = (~value) & 0x3F;
        while (true)
        {
            if ((Temp_data | QPC6614_ATTEN_0DB_Trace) == 0)
                {
                    ret += ATTEN_0DB;
                    break;
                }
                else if ((Temp_data & QPC6614_ATTEN_0_5DB_Trace) > 0)
                {
                    Temp_data &= ~(QPC6614_ATTEN_0_5DB_Trace);
                    ret += ATTEN_0_5DB;
                }
                else if ((Temp_data & QPC6614_ATTEN_1DB_Trace) > 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_1DB_Trace);
                    ret += ATTEN_1DB;
                }
                else if ((Temp_data & QPC6614_ATTEN_2DB_Trace) > 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_2DB_Trace);
                    ret += ATTEN_2DB;

                }
                else if ((Temp_data & QPC6614_ATTEN_4DB_Trace) > 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_4DB_Trace);
                    ret += ATTEN_4DB;
                }
                else if ((Temp_data & QPC6614_ATTEN_8DB_Trace) > 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_8DB_Trace);
                    ret += ATTEN_8DB;
                }
                else if ((Temp_data & QPC6614_ATTEN_16DB_Trace) > 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_16DB_Trace);
                    ret += ATTEN_16DB;
                }
                else if ((Temp_data & QPC6614_ATTEN_31_5DB_Trace) >= 0)
                {
                    Temp_data &= (~QPC6614_ATTEN_31_5DB_Trace);
                    ret += ATTEN_31_5DB;
                }
            //value[Bluecell_DATA]
        }
    }
    private void Atten_PE43711_TraceValue_Func(int value, out double ret)
        {
            int Temp_data = 0;
            ret = ATTEN_0DB;
            if (value != 0) Temp_data = (value) & 0x7E;
            else
            {
                ret = ATTEN_0DB;
            }
            while (true)
            {
                if ((Temp_data | PE43711_ATTEN_0DB_VAL) == 0)
                {
                    ret += ATTEN_0DB;
                    break;
                }
                else if ((Temp_data & PE43711_ATTEN_0_5DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_0_5DB_VAL) & 0x7F);
                    ret += ATTEN_0_5DB;
                }
                else if ((Temp_data & PE43711_ATTEN_1DB_VAL) > 0)
                {
                    Temp_data &= (~PE43711_ATTEN_1DB_VAL);
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_1DB_VAL) & 0x7E);
                    ret += ATTEN_1DB;
                }
                else if ((Temp_data & PE43711_ATTEN_2DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_2DB_VAL) & 0x7E);
                    ret += ATTEN_2DB;

                }
                else if ((Temp_data & PE43711_ATTEN_4DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_4DB_VAL) & 0x7E);
                    ret += ATTEN_4DB;
                }
                else if ((Temp_data & PE43711_ATTEN_8DB_VAL) > 0)
                {
                    Temp_data &= (~PE43711_ATTEN_8DB_VAL);
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_8DB_VAL) & 0x7E);
                    ret += ATTEN_8DB;
                }
                else if ((Temp_data & PE43711_ATTEN_16DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_16DB_VAL) & 0x7E);
                    ret += ATTEN_16DB;
                }
                else if ((Temp_data & PE43711_ATTEN_31_5DB_VAL) >= 0)
                {
                    Temp_data &= Convert.ToByte((~PE43711_ATTEN_31_5DB_VAL) & 0x7E);
                    ret += ATTEN_31_5DB;
                }
                //value[Bluecell_DATA]
            }
        }

        private void Atten_ADRF5720_TraceValue_Func(byte value, out double ret)
        {
            byte Temp_data = 0;
            ret = ATTEN_0DB;
            if (value != 0)
                Temp_data = (byte)(value & 0x3F);
            while (true)
            {

                if ((Temp_data & ADRF5720_ATTEN_31_5DB_VAL) == ADRF5720_ATTEN_31_5DB_VAL)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_31_5DB_VAL) & 0x3F);
                    ret += ATTEN_31_5DB;
                }
                else if ((Temp_data & ADRF5720_ATTEN_16DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_16DB_VAL) & 0x3F);
                    ret += ATTEN_16DB;
                }
                else if ((Temp_data & ADRF5720_ATTEN_8DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_8DB_VAL) & 0x3F);
                    ret += ATTEN_8DB;
                }
                else if ((Temp_data & ADRF5720_ATTEN_4DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_4DB_VAL) & 0x3F);
                    ret += ATTEN_4DB;
                }
                else if ((Temp_data & ADRF5720_ATTEN_2DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_2DB_VAL) & 0x3F);
                    ret += ATTEN_2DB;

                }
                else if ((Temp_data & ADRF5720_ATTEN_1DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_1DB_VAL) & 0x3F);
                    ret += ATTEN_1DB;
                }
                else if ((Temp_data & ADRF5720_ATTEN_0_5DB_VAL) > 0)
                {
                    Temp_data &= Convert.ToByte((~ADRF5720_ATTEN_0_5DB_VAL) & 0x3F);
                    ret += ATTEN_0_5DB;
                }
                else if ((Temp_data | ADRF5720_ATTEN_0DB_VAL) == 0)
                {
                    ret += ATTEN_0DB;
                    break;
                }
            }
        }
        private void Atten_HMC939_TraceValue_Func(int value, out double ret)
        {
            int Temp_data = 0;
            ret = ATTEN_0DB;

            Temp_data = (~value) & 0x1F;
            while (true)
            {
                if ((Temp_data | ((~HMC939_ATTEN_0DB_VAL) & 0x1F)) == 0)
                {
                    ret += ATTEN_0DB;
                    break;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_1DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_1DB_VAL);
                    ret += ATTEN_1DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_2DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_2DB_VAL);
                    ret += ATTEN_2DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_4DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_4DB_VAL);
                    ret += ATTEN_4DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_8DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_8DB_VAL);
                    ret += ATTEN_8DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_16DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_16DB_VAL);
                    ret += ATTEN_16DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_30DB_VAL) & 0x1F)) > 0)
                {
                    Temp_data &= (HMC939_ATTEN_30DB_VAL);
                    ret += ATTEN_31DB;
                }
                else if ((Temp_data & ((~HMC939_ATTEN_31DB_VAL) & 0x1F)) >= 0)
                {
                    Temp_data &= (HMC939_ATTEN_31DB_VAL);
                    ret += ATTEN_31_5DB;
                }
                //value[Bluecell_DATA]
            }
        }
        private void Atten_Operate(byte[] value)
        {
            double temp0=0,temp1=0, temp2=0,temp3=0;
            string strtemp = "";
            int index = 0;
            switch (value[Bluecell_Type])
            {
                case ATT_AB_CH_Read:
                    Atten_QPC6614_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    CheckForIllegalCrossThreadCalls = false;
                    this.Ach150Mhz_numericUpDown.Text = Convert.ToString(temp0);//value[Bluecell_DATA];

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp3); index++;
                    if (temp3 >= 30) temp3 = 30 + temp2;
                    else temp3 += temp2;

                    if (Achwifi2_4_radioButton.Checked == true)
                    {
                        this.Achwifi_numericUpDown.Text = Convert.ToString(temp3);
                    }

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp3); index++;
                    if (temp3 >= 30) temp3 = 30 + temp2;
                    else temp3 += temp2;

                    if (Achwifi5_8_radioButton.Checked == true)
                    {
                        this.Achwifi_numericUpDown.Text = Convert.ToString(temp3);
                    }



                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Ach30g28_28_5_radioButton.Checked == true)
                    {
                        this.Ach30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Ach30g28_5_29_radioButton.Checked == true)
                    {
                        this.Ach30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Ach30g29_29_5_radioButton.Checked == true)
                    {
                        this.Ach30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Ach30g29_5_30_radioButton.Checked == true)
                    {
                        this.Ach30g_numericUpDown.Text = Convert.ToString(temp2);
                    }
                    ////////////////////////////////////////////////////////////////////////////////////////////////
                    Atten_QPC6614_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;

                    CheckForIllegalCrossThreadCalls = false;
                    this.Bch150Mhz_numericUpDown.Text = Convert.ToString(temp0);//value[Bluecell_DATA];

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp3); index++;
                    if (temp3 >= 30) temp3 = 30 + temp2;
                    else temp3 += temp2;

                    if (Bchwifi2_4_radioButton.Checked == true)
                    {
                        this.Bchwifi_numericUpDown.Text = Convert.ToString(temp3);
                    }

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    Atten_PE43711_TraceValue_Func(value[Bluecell_DATA + index], out temp3); index++;
                    if (temp3 >= 30) temp3 = 30 + temp2;
                    else temp3 += temp2;

                    if (Bchwifi5_8_radioButton.Checked == true)
                    {
                        this.Bchwifi_numericUpDown.Text = Convert.ToString(temp3);
                    }



                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Bch30g28_28_5_radioButton.Checked == true)
                    {
                        this.Bch30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Bch30g28_5_29_radioButton.Checked == true)
                    {
                        this.Bch30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Bch30g29_29_5_radioButton.Checked == true)
                    {
                        this.Bch30g_numericUpDown.Text = Convert.ToString(temp2);
                    }

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp0); index++;
                    if (temp0 >= 30) temp0 = 30;

                    Atten_HMC939_TraceValue_Func(value[Bluecell_DATA + index], out temp1); index++;
                    if (temp1 >= 30) temp1 = 30 + temp0;
                    else temp1 += temp0;

                    Atten_ADRF5720_TraceValue_Func(value[Bluecell_DATA + index], out temp2); index++;
                    if (temp2 >= 30) temp2 = 30 + temp1;
                    else temp2 += temp1;
                    if (Bch30g29_5_30_radioButton.Checked == true)
                    {
                        this.Bch30g_numericUpDown.Text = Convert.ToString(temp2);
                    }
                    break;
                case ATT_A_CH_150M:
                case ATT_A_CH_WIFI2_4Ghz:
                case ATT_A_CH_WIFI5_8Ghz:
                case ATT_A_CH_30G_28_28_5:
                case ATT_A_CH_30G_28_5_29:
                case ATT_A_CH_30G_29_29_5:
                case ATT_A_CH_30G_29_5_30:
                case ATT_B_CH_WIFI2_4Ghz:
                case ATT_B_CH_WIFI5_8Ghz:
                case ATT_B_CH_30G_28_28_5:
                case ATT_B_CH_30G_28_5_29:
                case ATT_B_CH_30G_29_29_5:
                case ATT_B_CH_30G_29_5_30:

                    break;
                case ATT_A_EN_150M: break;
                case ATT_A_EN_WIFI1_2_4Ghz: break;
                case ATT_A_EN_WIFI2_2_4Ghz: break;
                case ATT_A_EN_WIFI3_2_4Ghz: break;
                case ATT_A_EN_WIFI4_2_4Ghz: break;
                case ATT_A_EN_WIFI1_5_8Ghz: break;
                case ATT_A_EN_WIFI2_5_8Ghz: break;
                case ATT_A_EN_WIFI3_5_8Ghz: break;
                case ATT_A_EN_WIFI4_5_8Ghz: break;
                case ATT_A_EN_30G1_28_28_5Ghz: break;
                case ATT_A_EN_30G2_28_28_5Ghz: break;
                case ATT_A_EN_30G3_28_28_5Ghz: break;
                case ATT_A_EN_30G1_28_5_29Ghz: break;
                case ATT_A_EN_30G2_28_5_29Ghz: break;
                case ATT_A_EN_30G3_28_5_29Ghz: break;
                case ATT_A_EN_30G1_29_29_5Ghz: break;
                case ATT_A_EN_30G2_29_29_5Ghz: break;
                case ATT_A_EN_30G3_29_29_5Ghz: break;
                case ATT_A_EN_30G1_30_30_5Ghz: break;
                case ATT_A_EN_30G2_30_30_5Ghz: break;
                case ATT_A_EN_30G3_30_30_5Ghz: break;
                case ATT_B_EN_150M: break;
                case ATT_B_EN_WIFI1_2_4Ghz: break;
                case ATT_B_EN_WIFI2_2_4Ghz: break;
                case ATT_B_EN_WIFI3_2_4Ghz: break;
                case ATT_B_EN_WIFI4_2_4Ghz: break;
                case ATT_B_EN_WIFI1_5_8Ghz: break;
                case ATT_B_EN_WIFI2_5_8Ghz: break;
                case ATT_B_EN_WIFI3_5_8Ghz: break;
                case ATT_B_EN_WIFI4_5_8Ghz: break;
                case ATT_B_EN_30G1_28_28_5Ghz: break;
                case ATT_B_EN_30G2_28_28_5Ghz: break;
                case ATT_B_EN_30G3_28_28_5Ghz: break;
                case ATT_B_EN_30G1_28_5_29Ghz: break;
                case ATT_B_EN_30G2_28_5_29Ghz: break;
                case ATT_B_EN_30G3_28_5_29Ghz: break;
                case ATT_B_EN_30G1_29_29_5Ghz: break;
                case ATT_B_EN_30G2_29_29_5Ghz: break;
                case ATT_B_EN_30G3_29_29_5Ghz: break;
                case ATT_B_EN_30G1_30_30_5Ghz: break;
                case ATT_B_EN_30G2_30_30_5Ghz: break;
                case ATT_B_EN_30G3_30_30_5Ghz: break;
                /****************************TABLE*********************/
                case ATT_A_EN_150M_R:
                case ATT_A_EN_WIFI1_2_4Ghz_R: 
                case ATT_A_EN_WIFI2_2_4Ghz_R: 
                case ATT_A_EN_WIFI3_2_4Ghz_R: 
                case ATT_A_EN_WIFI4_2_4Ghz_R: 
                case ATT_A_EN_WIFI1_5_8Ghz_R: 
                case ATT_A_EN_WIFI2_5_8Ghz_R: 
                case ATT_A_EN_WIFI3_5_8Ghz_R: 
                case ATT_A_EN_WIFI4_5_8Ghz_R: 
                case ATT_A_EN_30G1_28_28_5Ghz_R: 
                case ATT_A_EN_30G2_28_28_5Ghz_R: 
                case ATT_A_EN_30G3_28_28_5Ghz_R: 
                case ATT_A_EN_30G1_28_5_29Ghz_R: 
                case ATT_A_EN_30G2_28_5_29Ghz_R: 
                case ATT_A_EN_30G3_28_5_29Ghz_R: 
                case ATT_A_EN_30G1_29_29_5Ghz_R: 
                case ATT_A_EN_30G2_29_29_5Ghz_R: 
                case ATT_A_EN_30G3_29_29_5Ghz_R: 
                case ATT_A_EN_30G1_30_30_5Ghz_R: 
                case ATT_A_EN_30G2_30_30_5Ghz_R: 
                case ATT_A_EN_30G3_30_30_5Ghz_R: 
                case ATT_B_EN_150M_R: 
                case ATT_B_EN_WIFI1_2_4Ghz_R: 
                case ATT_B_EN_WIFI2_2_4Ghz_R: 
                case ATT_B_EN_WIFI3_2_4Ghz_R: 
                case ATT_B_EN_WIFI4_2_4Ghz_R: 
                case ATT_B_EN_WIFI1_5_8Ghz_R: 
                case ATT_B_EN_WIFI2_5_8Ghz_R: 
                case ATT_B_EN_WIFI3_5_8Ghz_R: 
                case ATT_B_EN_WIFI4_5_8Ghz_R: 
                case ATT_B_EN_30G1_28_28_5Ghz_R: 
                case ATT_B_EN_30G2_28_28_5Ghz_R: 
                case ATT_B_EN_30G3_28_28_5Ghz_R: 
                case ATT_B_EN_30G1_28_5_29Ghz_R: 
                case ATT_B_EN_30G2_28_5_29Ghz_R: 
                case ATT_B_EN_30G3_28_5_29Ghz_R: 
                case ATT_B_EN_30G1_29_29_5Ghz_R: 
                case ATT_B_EN_30G2_29_29_5Ghz_R: 
                case ATT_B_EN_30G3_29_29_5Ghz_R: 
                case ATT_B_EN_30G1_30_30_5Ghz_R: 
                case ATT_B_EN_30G2_30_30_5Ghz_R: 
                case ATT_B_EN_30G3_30_30_5Ghz_R:
                    Atten_Table_Read(value);
                    break;
                case ATT_AB_ALARM_READ:
                    Alarm_operate(value);
                    break;
            }

        }
        private void Atten_Table_Read(byte[] value)
        {
            string strtemp;
            double temp0;
            if (value.Length < 80)
            {
                if (this.Atten_comboBox.InvokeRequired)
                {
                    SetByteCallback d = new SetByteCallback(Atten_Table_Read);
                    this.Invoke(d, new object[] { value });
                }
                else
                    for (int i = 0; i < value.Length - 5; i++)
                    {
                        strtemp = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                        strtemp = strtemp.Replace("dB", "");
                        if (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_150M || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_150M)
                        {
                            Atten_QPC6614_TraceValue_Func((byte)value[Bluecell_DATA + i], out temp0);
                        }
                        else if ((Atten_comboBox.SelectedIndex < CH_A_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_A_ATT_EN_150M)
                                || (Atten_comboBox.SelectedIndex < CH_B_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_B_ATT_EN_150M))
                            Atten_PE43711_TraceValue_Func((byte)value[Bluecell_DATA + i], out temp0);
                        else if ((Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_29_5Ghz)
                                || (Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_28_5Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_5_29Ghz)
                                || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_29_5Ghz)
                                || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_5_29Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_28_5Ghz))
                            Atten_ADRF5720_TraceValue_Func((byte)value[Bluecell_DATA + i], out temp0);
                        else
                            Atten_HMC939_TraceValue_Func((byte)value[Bluecell_DATA + i], out temp0);
                        // if(temp0 != 0)
                        if (temp0 == 0)
                            temp0 = 31.5;

                        if (Convert.ToDouble(strtemp) == 0 && temp0 == 31.5)
                            temp0 = 0;

                        temp0 = temp0 - Convert.ToDouble(strtemp);
                        if (Convert.ToDouble(strtemp) == temp0)
                            temp0 = 0;
                        dataGridView1.Rows[i].Cells[2].Value = temp0;
                    }
            }
            else
            {
                MessageBox.Show("다시 시도해주세요.");
            }
        }
        int[] Atten_Uart_result = new int[200];
        int Atten_UartIndex;
        private void Atten_UartDataCheck(string Text)
        {
            int i = 0;
            byte[] temp_Val = new byte[100];
            int[] temp_intVal = new int[100];
            int temp_index = -1;
            string[] temp = Text.Split(' ').Where(s => !string.IsNullOrEmpty(s)).ToArray();

            //tempdata = Text.ToArray();
            for (i = 0; i < temp.Length; i++)
            {
                Atten_Uart_result[Atten_UartIndex + i] = int.Parse(temp[i].ToString(), System.Globalization.NumberStyles.HexNumber);
            }
            Atten_UartIndex += i;
            if (/*Array.IndexOf(Atten_Uart_result, "be") >= 0 &&*/ Text.Contains("eb"))
            {
                if (STH30_CheckCrc(temp_Val, (byte)(temp_Val[Bluecell_Length] + 2), temp_Val[temp_Val[Bluecell_Length] + 3]) == 1)
                {
                    temp_index = Array.IndexOf(Atten_Uart_result, 190);
                    for (i = 0; i < (Atten_Uart_result[temp_index + 2] + 5); i++)
                        temp_Val[i] = Convert.ToByte(Atten_Uart_result[i + temp_index]);

                    Atten_UartIndex = 0;
                   // temp_intVal = temp_Val;
                   // Atten_Operate(temp_intVal);
                    Array.Clear(Atten_Uart_result, 0, Atten_Uart_result.Length);
                    temp_index = 0;
                }
                else
                {
                    Array.Clear(Atten_Uart_result, 0, Atten_Uart_result.Length);
                    Atten_UartIndex = 0;
                    temp_index = 0;
                }

            }
        }
       /*
        private void Atten_UartDataCheck(byte[] Data)
        {
            int i = 0;
            byte[] temp_Val = new byte[100];
            int temp_index = -1;
            //string[] temp = Text.Split(' ').Where(s => !string.IsNullOrEmpty(s)).ToArray();
            Atten_Uart_result[Atten_UartIndex++] = Data;
            if (Atten_Uart_result[Atten_UartIndex - 1] == 0xeb)
            {
                if (STH30_CheckCrc(Atten_Uart_result, Atten_Uart_result[Bluecell_Length] + 2,Atten_Uart_result[Atten_Uart_result[Bluecell_Length] + 3]) == 1)
                {
                    temp_index = Array.IndexOf(Atten_Uart_result, 190);
                    for (i = 0; i < (Atten_Uart_result[temp_index + 2] + 5); i++)
                        temp_Val[i] = Convert.ToByte(Atten_Uart_result[i + temp_index]);

                    Atten_UartIndex = 0;
                    Atten_Operate(Atten_Uart_result);
                    Array.Clear(Atten_Uart_result, 0, Atten_Uart_result.Length);
                    temp_index = 0;
                }
                else
                {
                    Array.Clear(Atten_Uart_result, 0, Atten_Uart_result.Length);
                    Atten_UartIndex = 0;
                    temp_index = 0;
                }

            }
        }*/
        private void Atten_UartGet_Data(string Text)
        {
           
            Atten_UartDataCheck(Text);
            //Text.Substring(0, Text.IndexOf(searchText));
            /*if (this.tbReceived.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(Atten_UartGet_Data);
                this.Invoke(d, new object[] { Text });
            }
            else this.tbReceived.Text += Text;*/
            
        }
        private void Atten_UartGet_Data(byte[] Data)
        {
            // int[] temp_data = new int[Data.Length];   // 바이트 배열 크기만큼 롱형 배열 할당
            byte[] crc_buf = new byte[Data.Length];
            if (Data[Bluecell_STX] == 0xbe && Data[Data.Length - 1] == 0xeb)
            {
                crc_buf = Data;
                //STH30_CreateCrc(Data, (byte)(Data[Bluecell_Length] +2));
                if (STH30_CheckCrc(Data, (byte)(Data[Bluecell_Length] + 2), Data[Data[Bluecell_Length] + 3]) == 1)
                {
                    Atten_Operate(Data);
                }
                else
                {

                    this.tbReceived.Text += "CRC ERROR OCCUR!! \r\n";
                }
             }
            else
            {
                this.tbReceived.Text += "CRC ERROR OCCUR!! \r\n";
            }
        }
        //data 처리...!!!!!!!!!!!!!!!!!!!
        private void PacketData(string Text)
        {
            if (this.tbReceived.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(PacketData);
                this.Invoke(d, new object[] { Text });
            }
            else  this.tbReceived.Text += Text;
        }

        /*private void PacketData(byte[] data)
        {
            if (this.tbReceived.InvokeRequired)
            {
                SetByteCallback d = new SetByteCallback(PacketData);
                this.Invoke(d, new object[] { data });
            }
            else
            {
                this.tbReceived.Text += "\r\n[RX]";
                for (int i = 0; i < data.Length; i++)
                {
                    this.tbReceived.Text +=Convert.ToString(data[i],16);
                }
            }
        }*/
        //글자 수 제한///
        private const int WM_SETREDRAW = 11;

        public void PacketData(byte[] text)
        {
            string AppendMessage = "";
            if (tbReceived.InvokeRequired)
            {
                try
                {
                    tbReceived.Invoke(new SetByteCallback(PacketData), text);
                }
                catch { }
            }
            else
            {
                const int nLimitLines = 100; //제한 라인 수
                try
                {
                    this.tbReceived.Text += "\r\n[RX]";
                    for (int i = 0; i < text.Length; i++)
                    {
                        AppendMessage += Convert.ToString(text[i], 16);
                        AppendMessage += " ";
                    }
                    //string AppendMessage = DateTime.Now.ToString("hh:mm:ss.fff") + " " + text;
                    tbReceived.AppendText(AppendMessage);

                    if (tbReceived.Lines.Length > nLimitLines)
                    {
                        LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);

                        while ((tempLines.Count - nLimitLines) > 0)
                        {
                            tempLines.RemoveFirst();
                        }

                        tbReceived.Lines = tempLines.ToArray();
                    }
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                    tbReceived.Select(tbReceived.Text.Length, 0);
                    tbReceived.ScrollToCaret();
                }
                finally { }
            }
        }

//글자 수 제한///
        public static int i_test = 0;
        private void SerialFormEnagleChoice(bool of)
        {
            cmCom_Port.Enabled = of;
            cmBaudRate.Enabled = of;

            comboBox1.Enabled = of;
            comboBox2.Enabled = of;

        }
        /*
        private void tbReceived_TextChanged(object sender, EventArgs e)
        {
           // tbReceived.Lines = tbReceived.ToArray();
            SendMessage(this.Handle, WM_SETREDRAW, true, 0);


            this.tbReceived.SelectionStart = tbReceived.Text.Length;
            this.tbReceived.ScrollToCaret();
        }
        */
        private void comboBox1_MouseDown(object sender, MouseEventArgs e)
        {
            comboBox1.DataSource = SerialPort.GetPortNames();
        }

        //Port Closed butten
        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            tbReceived.Text = "Port Closed!!!";

            //Serial on/off Button의 보이고 안보이게 하기
            button1.Enabled = true; //on 기능 나타남
            button1.Visible = true; //on 버튼 나타남
            PortClose_button.Enabled = false;  //off 기능 사라짐
            PortClose_button.Visible = false; //off 버튼 사라짐

            SerialFormEnagleChoice(true); //serial 설정버튼 on
        }

        //Text Clear Butten
        private void button4_Click(object sender, EventArgs e)
        {
            tbReceived.Text = "";
        }

        //Text Send Butten
        private void button3_Click(object sender, EventArgs e)
        {

        }

        //Port 초기 설정값!!!!!

        //단, 이곳은 클릭했을때 결정나는것임 젠장!
        //그래서 앞에 초기값 설정을 넣어는 뒸음 ㅠㅠ


        //1.Port설정
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbReceived.Text = comboBox1.Text;//ok
            serialPort1.PortName = comboBox1.SelectedItem.ToString();
            serialPort1.PortName = comboBox1.Text.ToString();//

        }
        //추가 사용 함수!!!!!!!!!!
        //창이 닫히면 자동으로 포트를 닫아준다...되나????
        private void frm_guitar_mng_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();
            }
            catch
            {

            }
            if (serialPort1.IsOpen)
            {
                tbReceived.Text = "Port Open Connect!!!\r\n";
                //rbText.Text = "[" + SP1.PortName.ToString() + "] Port Open Connect!!";

                //Serial on/off Button의 보이고 안보이게 하기
                button1.Enabled = false; //on 기능 소멸
                button1.Visible = false; //on 버튼 사라짐
                PortClose_button.Enabled = true;  //off 기능 나타남
                PortClose_button.Visible = true; //off 버튼 나타남.

                SerialFormEnagleChoice(false); //serial 설정버튼 off
            }
            else
            {
                tbReceived.Text = "Port Open Failed!!!";
            }

            //////////////////////////다른 프로그래머의 데이터
            /*
             * if (serialPort1.IsOpen) serialPort1.Close();

               serialPort1.PortName = comboBox1.Text;
               tbReceived.Clear();
               tbReceived.Text += serialPort1.PortName + "Selected\n";
             */
            ////////////////////
        }
        private void Bluecell_AttenTable_Ctrl_Load(object sender, EventArgs e)
        {

        }
        /**********************
         * Row Heder Index Paint *
         ***********************/
        static bool init = false;
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (init == false)
            {
                HeaderIndexNumSet();
            }
        }
        private void HeaderIndexNumSet()
        {
            // ﻿if (dgv.Rows.Count > 0) dgv.RowHeadersWidth = 70;    
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
            }
            this.dataGridView1.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            this.dataGridView1.TopLeftHeaderCell.Value = "No.";
            init = true;
            SetDoNotSort(dataGridView1);
            dataGridView1.Columns["DB"].ReadOnly = true;
            //dataGridView1.RowHeadersWidth = 30;
            //  dataGridView1.RowHeadersAlign = 20;
            dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.ClearSelection();
        }
        /**********************
         * 행 Header Lock Function *
         ***********************/
        private void SetDoNotSort(DataGridView dgv)
        {
            foreach (DataGridViewColumn i in dgv.Columns)
            {
                i.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }
        /********************************************
          * 행 Header Click 시 Row index Re paint *
          *****************************************/
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            init = false;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
           /* if (dataGridView1.CurrentRow.Cells[0].Value != null && (bool)dataGridView1.CurrentRow.Cells[0].Value)
            {
                dataGridView1.CurrentRow.Cells[0].Value = false;
                dataGridView1.CurrentRow.Cells[0].Value = null;
            }
            else if (dataGridView1.CurrentRow.Cells[0].Value == null)
            {
                dataGridView1.CurrentRow.Cells[0].Value = true;
            }*/
           // int index = dataGridView1.CurrentRow.Index;
           // dataGridView1.Rows[index].SetValues(true);
            //  foreach (DataGridViewRow row in dataGridView1.Rows)
        }
        
        private void MultiSet_button_Click(object sender, EventArgs e)
        {
            ChkBox_Check();
        }
        private void ChkBox_Check()
        {
            int tempmax_val1=0, tempmin_val2 = 0xFF;
            for (int i = 0; i < 64; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                {
                    if (tempmax_val1 < i)
                    {
                        tempmax_val1 = i;
                    }
                    if (tempmin_val2 > i)
                    {
                        tempmin_val2 = i;
                    }
                }
            }
            for (int i = tempmin_val2; i <= tempmax_val1; i++)
            {
                dataGridView1.Rows[i].SetValues(true);
                DataTableMultiValueSet(i);
                
                // dataGridView1.Rows.Add();

                //MultiOffset_numericUpDown.Value
            }
            tempmax_val1 = 0;tempmin_val2 = 0xFF;
        }
        private void DataTableMultiValueSet(int index)
        {
            dataGridView1.Rows[index].Cells[2].Value = MultiSet_numericUpDown.Value;//"New Value";
        }
        private void MultiOffset_button_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 64; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                {
                    DataTableMultiOffsetValueSet(i);
                }
            }
           
        }

        private void Initialize_button_Click(object sender, EventArgs e)
        {
            DataTableSet();
            HeaderIndexNumSet();
        }
        private void AllCheckClick(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(allCheck.Checked) == true)
            {
                for (int i = 0; i < 64; i++)
                    dataGridView1.Rows[i].SetValues(true);
            }
            else
            {
                for (int i = 0; i < 64; i++)
                    dataGridView1.Rows[i].SetValues(false);
            }
        }

        private void Expansion_button_Click(object sender, EventArgs e)
        {
            int index_max = 0, index_min = 0, cnt = 0;
            double temp_val = 0,temp_val1 = 0,temp_ret=0;
            ChkBox_Check(out index_max,out index_min,out cnt);
            if (cnt > 1)
            {
                temp_val = index_max - index_min;
                temp_val1 = Convert.ToDouble(dataGridView1.Rows[index_max].Cells[2].Value) - Convert.ToDouble(dataGridView1.Rows[index_min].Cells[2].Value);
                temp_val1 /= temp_val;
                temp_val1 = Math.Round(temp_val1,1); // 반올림

                temp_val  = Convert.ToDouble(dataGridView1.Rows[index_min].Cells[2].Value);
                for (int i = index_min + 1; i < index_max; i++)
                {
                    temp_ret = temp_val + temp_val1;
                    dataGridView1.Rows[i].Cells[2].Value = Math.Round(temp_ret, 1); // 반올림temp_ret;
                    temp_val = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
                }

            }
        }
        private void ChkBox_Check(out int index_Max,out int index1_Min, out int cnt)
        {
            int  tempcnt=0,tempmax_val1 = 0, tempmin_val2 = 0xFF;
            for (int i = 0; i < 64; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                {
                    if (tempmax_val1 < i)
                    {
                        tempmax_val1 = i;
                    }
                    if (tempmin_val2 > i)
                    {
                        tempmin_val2 = i;
                    }
                    tempcnt++;
                }
            }
            cnt = tempcnt;
            index_Max = tempmax_val1;
            index1_Min = tempmin_val2;
            if (cnt <= 1)
                return;
            for (int i = tempmin_val2; i <= tempmax_val1; i++)
            {
                dataGridView1.Rows[i].SetValues(true);
              //  DataTableMultiValueSet(i);

                // dataGridView1.Rows.Add();

                //MultiOffset_numericUpDown.Value
            }
            tempmax_val1 = 0; tempmin_val2 = 0xFF;
        }
        private void DataTableMultiOffsetValueSet(int index)
        {
            double temp,temp1 = 0;
            temp = Convert.ToDouble(MultiOffset_numericUpDown.Value);
            temp1 = Convert.ToDouble(dataGridView1.Rows[index].Cells[2].Value);
            dataGridView1.Rows[index].Cells[2].Value = temp + temp1;//"New Value";
        }

        private void Read_button_Click(object sender, EventArgs e)
        {
            TableDataRead();
        }

        private void Write_button_Click(object sender, EventArgs e)
        {
            TableDataWrite();
        }

        private void Send_button_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                //this.serialPort1.Open();
                serialPort1.Write(this.tbSend.Text);
            }
            if (serialPort1.IsOpen == false)
            {
                MessageBox.Show("Please, open port.");
            }
            this.tbSend.Text = "";
        }
        const int POLYNOMIAL = 0x131; // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
        byte STH30_CheckCrc(byte[] data, byte nbrOfBytes, byte checksum)
        {
            byte bit;        // bit mask
            int crc = 0xFF; // calculated checksum
            byte byteCtr;    // byte counter
            byte index = 1;

            // calculates 8-Bit checksum with given polynomial
            for (byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
            {
                crc ^= (data[index++]);
                for (bit = 8; bit > 0; --bit)
                {
                    if (Convert.ToBoolean(crc & 0x80)) { crc = (crc << 1) ^ POLYNOMIAL; }
                    else crc = (crc << 1);
                }
            }
            if (crc != checksum) return 0;
            else return 1;
        }
        byte STH30_CreateCrc(byte[] data,byte nbrOfBytes)
        {
            byte bit;        // bit mask
            int crc = 0xFF; // calculated checksum
            byte byteCtr;    // byte counter
            byte index = 1;
            // calculates 8-Bit checksum with given polynomial
            for (byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
            {
                crc ^= (data[index++]);
                for (bit = 8; bit > 0; --bit)
                {
                    if (Convert.ToBoolean(crc & 0x80)) { crc = (crc << 1) ^ POLYNOMIAL; }
                    else crc = (crc << 1);
                }
            }
            return (byte)crc;
        }

       // byte[] tempbuf= { 0x02, 0x04, 0x00, 0x00, 0x01, 0x01 };
       /* private void Test_button_Click(object sender, EventArgs e)
        {
            // byte temp;
            // temp = STH30_CreateCrc(tempbuf, 6);
            ToByteArray("1E");
         //   label1.Text= Convert.ToString(STH30_CreateCrc(tempbuf,6),16);
            if (serialPort1.IsOpen == true)
            {
                //this.serialPort1.Open();
                byte[] bytes;// = { 0x1E };//System.Text.Encoding.ASCII.GetBytes(label1.Text);
                bytes = ToByteArray("1E");
                try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
            }
            if (serialPort1.IsOpen == false)
            {
                MessageBox.Show("Please, open port.");
            }
        }*/
        private static byte[] ToByteArray(String hex)
        {
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length / 2; i++) bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
            return bytes;
        }
        private void TableReadCmdGetFunc(int index, out byte cmd)
        {
            switch (index)
            {
                case CH_A_ATT_EN_150M            :	cmd = 0x40; break;
                case CH_A_ATT_EN_WIFI1_2_4Ghz    :	cmd = 0x41; break;
                case CH_A_ATT_EN_WIFI2_2_4Ghz    :	cmd = 0x42; break;
                case CH_A_ATT_EN_WIFI3_2_4Ghz    :	cmd = 0x43; break;
                case CH_A_ATT_EN_WIFI4_2_4Ghz    :	cmd = 0x44; break;
                case CH_A_ATT_EN_WIFI1_5_8Ghz    :	cmd = 0x45; break;
                case CH_A_ATT_EN_WIFI2_5_8Ghz    :	cmd = 0x46; break;
                case CH_A_ATT_EN_WIFI3_5_8Ghz    :	cmd = 0x47; break;
                case CH_A_ATT_EN_WIFI4_5_8Ghz    :	cmd = 0x48; break;
                case CH_A_ATT_EN_30G1_28_28_5Ghz :	cmd = 0x49; break;
                case CH_A_ATT_EN_30G2_28_28_5Ghz :	cmd = 0x4A; break;
                case CH_A_ATT_EN_30G3_28_28_5Ghz :	cmd = 0x4E; break;
                case CH_A_ATT_EN_30G1_28_5_29Ghz :	cmd = 0x4F; break;
                case CH_A_ATT_EN_30G2_28_5_29Ghz :	cmd = 0x50; break;
                case CH_A_ATT_EN_30G3_28_5_29Ghz :	cmd = 0x51; break;
                case CH_A_ATT_EN_30G1_29_29_5Ghz :	cmd = 0x52; break;
                case CH_A_ATT_EN_30G2_29_29_5Ghz :	cmd = 0x53; break;
                case CH_A_ATT_EN_30G3_29_29_5Ghz :	cmd = 0x54; break;
                case CH_A_ATT_EN_30G1_29_5_30Ghz :	cmd = 0x55; break;
                case CH_A_ATT_EN_30G2_29_5_30Ghz :	cmd = 0x56; break;
                case CH_A_ATT_EN_30G3_29_5_30Ghz :	cmd = 0x57; break;
                case CH_B_ATT_EN_150M            :	cmd = 0x58; break;
                case CH_B_ATT_EN_WIFI1_2_4Ghz    :	cmd = 0x59; break;
                case CH_B_ATT_EN_WIFI2_2_4Ghz    :	cmd = 0x5A; break;
                case CH_B_ATT_EN_WIFI3_2_4Ghz    :	cmd = 0x5B; break;
                case CH_B_ATT_EN_WIFI4_2_4Ghz    :	cmd = 0x5C; break;
                case CH_B_ATT_EN_WIFI1_5_8Ghz    :	cmd = 0x5D; break;
                case CH_B_ATT_EN_WIFI2_5_8Ghz    :	cmd = 0x5E; break;
                case CH_B_ATT_EN_WIFI3_5_8Ghz    :	cmd = 0x5F; break;
                case CH_B_ATT_EN_WIFI4_5_8Ghz    :	cmd = 0x60; break;
                case CH_B_ATT_EN_30G1_28_28_5Ghz :	cmd = 0x61; break;
                case CH_B_ATT_EN_30G2_28_28_5Ghz :	cmd = 0x62; break;
                case CH_B_ATT_EN_30G3_28_28_5Ghz :	cmd = 0x63; break;
                case CH_B_ATT_EN_30G1_28_5_29Ghz :	cmd = 0x64; break;
                case CH_B_ATT_EN_30G2_28_5_29Ghz :	cmd = 0x65; break;
                case CH_B_ATT_EN_30G3_28_5_29Ghz :	cmd = 0x66; break;
                case CH_B_ATT_EN_30G1_29_29_5Ghz :	cmd = 0x67; break;
                case CH_B_ATT_EN_30G2_29_29_5Ghz :	cmd = 0x68; break;
                case CH_B_ATT_EN_30G3_29_29_5Ghz :	cmd = 0x69; break;
                case CH_B_ATT_EN_30G1_29_5_30Ghz :	cmd = 0x6A; break;
                case CH_B_ATT_EN_30G2_29_5_30Ghz :	cmd = 0x6B; break;
                case CH_B_ATT_EN_30G3_29_5_30Ghz:   cmd = 0x6C; break;
                default:
                    cmd = 0;
                    break;
            }
        }

        private void TableDataRead() //Read Protocol 만드는 Function
        {
            int cnt = 0;
            byte[] bytes = new byte[TABLE_UART_LENGTH];
            byte type_cmd;
            TableReadCmdGetFunc(Atten_comboBox.SelectedIndex, out type_cmd);
            bytes[cnt++] = 0xBE;
            bytes[cnt++] = type_cmd;
            bytes[cnt++] = TABLE_UARTDATA_LENGTH;
            /*for (int i = 0; i < 70; i++)
            {
                bytes[cnt++] = 0;
            }*/
            bytes[TABLE_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[TABLE_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }
        private void TableWriteCmdGetFunc(int index, out byte cmd)
        {
            switch (index)
            {
                case CH_A_ATT_EN_150M: cmd = 0x10; break;
                case CH_A_ATT_EN_WIFI1_2_4Ghz: cmd = 0x11; break;
                case CH_A_ATT_EN_WIFI2_2_4Ghz: cmd = 0x12; break;
                case CH_A_ATT_EN_WIFI3_2_4Ghz: cmd = 0x13; break;
                case CH_A_ATT_EN_WIFI4_2_4Ghz: cmd = 0x14; break;
                case CH_A_ATT_EN_WIFI1_5_8Ghz: cmd = 0x15; break;
                case CH_A_ATT_EN_WIFI2_5_8Ghz: cmd = 0x16; break;
                case CH_A_ATT_EN_WIFI3_5_8Ghz: cmd = 0x17; break;
                case CH_A_ATT_EN_WIFI4_5_8Ghz: cmd = 0x18; break;
                case CH_A_ATT_EN_30G1_28_28_5Ghz: cmd = 0x19; break;
                case CH_A_ATT_EN_30G2_28_28_5Ghz: cmd = 0x1A; break;
                case CH_A_ATT_EN_30G3_28_28_5Ghz: cmd = 0x1E; break;
                case CH_A_ATT_EN_30G1_28_5_29Ghz: cmd = 0x1F; break;
                case CH_A_ATT_EN_30G2_28_5_29Ghz: cmd = 0x20; break;
                case CH_A_ATT_EN_30G3_28_5_29Ghz: cmd = 0x21; break;
                case CH_A_ATT_EN_30G1_29_29_5Ghz: cmd = 0x22; break;
                case CH_A_ATT_EN_30G2_29_29_5Ghz: cmd = 0x23; break;
                case CH_A_ATT_EN_30G3_29_29_5Ghz: cmd = 0x24; break;
                case CH_A_ATT_EN_30G1_29_5_30Ghz: cmd = 0x25; break;
                case CH_A_ATT_EN_30G2_29_5_30Ghz: cmd = 0x26; break;
                case CH_A_ATT_EN_30G3_29_5_30Ghz: cmd = 0x27; break;
                case CH_B_ATT_EN_150M: cmd = 0x28; break;
                case CH_B_ATT_EN_WIFI1_2_4Ghz: cmd = 0x29; break;
                case CH_B_ATT_EN_WIFI2_2_4Ghz: cmd = 0x2A; break;
                case CH_B_ATT_EN_WIFI3_2_4Ghz: cmd = 0x2B; break;
                case CH_B_ATT_EN_WIFI4_2_4Ghz: cmd = 0x2C; break;
                case CH_B_ATT_EN_WIFI1_5_8Ghz: cmd = 0x2D; break;
                case CH_B_ATT_EN_WIFI2_5_8Ghz: cmd = 0x2E; break;
                case CH_B_ATT_EN_WIFI3_5_8Ghz: cmd = 0x2F; break;
                case CH_B_ATT_EN_WIFI4_5_8Ghz: cmd = 0x30; break;
                case CH_B_ATT_EN_30G1_28_28_5Ghz: cmd = 0x31; break;
                case CH_B_ATT_EN_30G2_28_28_5Ghz: cmd = 0x32; break;
                case CH_B_ATT_EN_30G3_28_28_5Ghz: cmd = 0x33; break;
                case CH_B_ATT_EN_30G1_28_5_29Ghz: cmd = 0x34; break;
                case CH_B_ATT_EN_30G2_28_5_29Ghz: cmd = 0x35; break;
                case CH_B_ATT_EN_30G3_28_5_29Ghz: cmd = 0x36; break;
                case CH_B_ATT_EN_30G1_29_29_5Ghz: cmd = 0x37; break;
                case CH_B_ATT_EN_30G2_29_29_5Ghz: cmd = 0x38; break;
                case CH_B_ATT_EN_30G3_29_29_5Ghz: cmd = 0x39; break;
                case CH_B_ATT_EN_30G1_29_5_30Ghz: cmd = 0x3A; break;
                case CH_B_ATT_EN_30G2_29_5_30Ghz: cmd = 0x3B; break;
                case CH_B_ATT_EN_30G3_29_5_30Ghz: cmd = 0x3C; break;
                default:cmd = 0; break;
            }
        }
        private void TableDataWrite() //Read Protocol 만드는 Function
        {
            string strtemp;
            int  index= 0,cnt = 0,int_value;
            float float_value;
            double double_value1 = 0, double_value2 = 0;
            byte[] bytes = new byte[TABLE_UART_LENGTH];
            byte ret = 0, type_cmd;

            TableWriteCmdGetFunc(Atten_comboBox.SelectedIndex, out type_cmd);
            bytes[cnt++] = 0xBE;
            bytes[cnt++] = type_cmd;
            bytes[cnt++] = TABLE_UARTDATA_LENGTH;
            for (int i = 0; i < TABLE_UARTDATA_LENGTH; i++)
            {
                // strtemp = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);//table에 있는 문자열을 가져옴
                // strtemp= strtemp.Replace("dB", "");//문자열의 db를 제거 
                // double_value1 = Convert.ToDouble(strtemp);//문자열을 실수로 변환
                strtemp = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                strtemp = strtemp.Replace("dB", "");
                double_value1 = Convert.ToDouble(strtemp);
                double_value2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
                double_value1 += double_value2;
                if (   Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G1_28_28_5Ghz || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G2_28_28_5Ghz
                    || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G1_28_5_29Ghz || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G2_28_5_29Ghz
                    || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G1_29_29_5Ghz || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G2_29_29_5Ghz
                    || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G1_29_5_30Ghz || Atten_comboBox.SelectedIndex != CH_A_ATT_EN_30G2_29_5_30Ghz
                    || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G1_28_28_5Ghz || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G2_28_28_5Ghz
                    || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G1_28_5_29Ghz || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G2_28_5_29Ghz
                    || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G1_29_29_5Ghz || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G2_29_29_5Ghz
                    || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G1_29_5_30Ghz || Atten_comboBox.SelectedIndex != CH_B_ATT_EN_30G2_29_5_30Ghz)
                {
                    if (double_value1 >= 31.5)
                        double_value1 = 31.5;
                }
                else
                {
                    if (double_value1 >= 31)
                        double_value1 = 31;
                }
                strtemp = Convert.ToString(double_value1);
                //strtemp1 = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                if (strtemp.Contains("."))
                {
                    float_value = float.Parse(strtemp);
                    index = 0;
                    if(Atten_comboBox.SelectedIndex == CH_A_ATT_EN_150M || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_150M)
                    {
                        QPC6614_Atten_Rx_DataCheck(float_value, out ret);
                    }
                    else if ((Atten_comboBox.SelectedIndex  < CH_A_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_A_ATT_EN_150M)
                        || (Atten_comboBox.SelectedIndex < CH_B_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_B_ATT_EN_150M))
                        PE43711_Atten_Rx_DataCheck(float_value, out ret);
                    else if ((Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_29_5Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_28_5Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_5_29Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_29_5Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_5_29Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_28_5Ghz)) 
                        ADRF5720_Atten_Rx_DataCheck(float_value, out ret);
                }
                else
                {
                    int_value = int.Parse(strtemp);
                    index = 0;
                    if (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_150M || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_150M)
                    {
                        QPC6614_Atten_Rx_DataCheck(int_value, out ret);
                    }
                    if ((Atten_comboBox.SelectedIndex < CH_A_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_A_ATT_EN_150M)
                        || (Atten_comboBox.SelectedIndex < CH_B_ATT_EN_30G1_28_28_5Ghz && Atten_comboBox.SelectedIndex > CH_B_ATT_EN_150M))
                        PE43711_Atten_Rx_DataCheck(int_value, out ret);
                    else if ((Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_29_29_5Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_28_5Ghz || Atten_comboBox.SelectedIndex == CH_B_ATT_EN_30G3_28_5_29Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_5_30Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_29_29_5Ghz)
                        || (Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_5_29Ghz || Atten_comboBox.SelectedIndex == CH_A_ATT_EN_30G3_28_28_5Ghz))
                        ADRF5720_Atten_Rx_DataCheck(int_value, out ret);
                    else
                        HMC939_Atten_Rx_DataCheck((byte)int_value, out ret);
                }
                bytes[cnt++] = ret;
            }
            bytes[TABLE_UART_LENGTH - 2] = STH30_CreateCrc(bytes,(byte)(bytes.Length-3));
            bytes[TABLE_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }
        private void QPC6614_Atten_Rx_DataCheck(float data, out byte ret)
        {
            float Temp_Atten_Ret = data;
            byte Temp_Atten_Value = QPC6614_ATTEN_0DB_VAL;
            while (true)
            {
                if (data == 0)
                    break;

                if (data == ATTEN_31_5DB)
                {
                    data = 0;
                    Temp_Atten_Value &= QPC6614_ATTEN_31_5DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_1DB_VAL;
                }
                else if (data >= ATTEN_0_5DB)
                {
                    data = 0;
                    Temp_Atten_Value &= QPC6614_ATTEN_0_5DB_VAL;
                }
                else if (data >= ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value &= QPC6614_ATTEN_0DB_VAL;
                }
            }
            ret = Temp_Atten_Value;
        }

        private void PE43711_Atten_Rx_DataCheck(byte data, out byte ret)
        {
            byte Temp_Atten_Ret = data, Temp_Atten_Value = PE43711_ATTEN_31_75DB_VAL;
            while (true)
            {
                if (data == 0)
                    break;

                if (data == ATTEN_31_5DB)
                {
                    data = 0;
                    Temp_Atten_Value &= PE43711_ATTEN_31_5DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value &= PE43711_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value &= PE43711_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value &= PE43711_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value &= PE43711_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value &= PE43711_ATTEN_1DB_VAL;
                }
                else if (data >= ATTEN_0_5DB)
                {
                    data = 0;
                    Temp_Atten_Value &= PE43711_ATTEN_0_5DB_VAL;
                }
                else if (data >= ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value &= PE43711_ATTEN_0DB_VAL;
                }
            }
            ret = Temp_Atten_Value;
        }
        private void PE43711_Atten_Rx_DataCheck(float data, out byte ret)
        {
            float Temp_Atten_Ret = data;
            byte Temp_Atten_Value = PE43711_ATTEN_0DB_VAL;
            while (true)
            {
                if (data == 0)
                    break;

                if (data == ATTEN_31_5DB)
                {
                    data = 0;
                    Temp_Atten_Value |= PE43711_ATTEN_31_5DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value |= PE43711_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value |= PE43711_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value |= PE43711_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value |= PE43711_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value |= PE43711_ATTEN_1DB_VAL;
                }
                else if (data >= ATTEN_0_5DB)
                {
                    data = 0;
                    Temp_Atten_Value |= PE43711_ATTEN_0_5DB_VAL;
                }
                else if (data >= ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value |= PE43711_ATTEN_0DB_VAL;
                }
            }
            ret = Temp_Atten_Value;
        }


        private void HMC939_Atten_Rx_DataCheck(byte data, out byte ret)
        {
            byte Temp_Atten_Ret = data, Temp_Atten_Value = HMC939_ATTEN_0DB_VAL;
              while (true)
            {
                if (data == ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value &= HMC939_ATTEN_0DB_VAL;
                    break;
                }
                if (data == ATTEN_31DB)
                {
                    data = 0;
                    Temp_Atten_Value &= HMC939_ATTEN_31DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value &= HMC939_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value &= HMC939_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value &= HMC939_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value &= HMC939_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value &= HMC939_ATTEN_1DB_VAL;
                }


            }
            ret = Temp_Atten_Value;
        }
        private void ADRF5720_Atten_Rx_DataCheck(byte data, out byte ret)
        {
            byte Temp_Atten_Ret = data, Temp_Atten_Value = ADRF5720_ATTEN_0DB_VAL;
            while (true)
            {
                if (data == 0)
                    break;

                if (data == ATTEN_31_5DB)
                {
                    data = 0;
                    Temp_Atten_Value |= ADRF5720_ATTEN_31_5DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_1DB_VAL;
                }

                else if (data >= ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_0DB_VAL;
                }
            }
            ret = Temp_Atten_Value;
        }
        private void ADRF5720_Atten_Rx_DataCheck(double data, out byte ret)
        {
            double Temp_Atten_Ret = data;
            byte  Temp_Atten_Value = ADRF5720_ATTEN_0DB_VAL;

            while (true)
            {
                if (data == 0)
                    break;

                if (data == ATTEN_31_5DB)
                {
                    data = 0;
                    Temp_Atten_Value |= ADRF5720_ATTEN_31_5DB_VAL;
                }
                else if (data >= ATTEN_16DB)
                {
                    data -= ATTEN_16DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_16DB_VAL;
                }
                else if (data >= ATTEN_8DB)
                {
                    data -= ATTEN_8DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_8DB_VAL;
                }
                else if (data >= ATTEN_4DB)
                {
                    data -= ATTEN_4DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_4DB_VAL;
                }
                else if (data >= ATTEN_2DB)
                {
                    data -= ATTEN_2DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_2DB_VAL;
                }
                else if (data >= ATTEN_1DB)
                {
                    data -= ATTEN_1DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_1DB_VAL;
                }
                else if (data >= ATTEN_0_5DB)
                {
                    data -= ATTEN_0_5DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_0_5DB_VAL;
                }
                else if (data >= ATTEN_0DB)
                {
                    data -= ATTEN_0DB;
                    Temp_Atten_Value |= ADRF5720_ATTEN_0DB_VAL;
                }
            }
            ret = Temp_Atten_Value;
        }
        private void PortClose_button_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            tbReceived.Text = "Port Closed!!!";

            //Serial on/off Button의 보이고 안보이게 하기
            button1.Enabled = true; //on 기능 나타남
            button1.Visible = true; //on 버튼 나타남
            PortClose_button.Enabled = false;  //off 기능 사라짐
            PortClose_button.Visible = false; //off 버튼 사라짐

            SerialFormEnagleChoice(true); //serial 설정버튼 on
        }

        private void FileSave_button_Click(object sender, EventArgs e)
        {
            int current_status = Atten_comboBox.SelectedIndex;
            string current_name = CH_A_ATT_EN_150M_Str;
            double DB_Table = Convert.ToDouble(ATTEN_31_5DB);
            string selectedText = this.Atten_comboBox.GetItemText(this.Atten_comboBox.SelectedItem);
            current_name = selectedText;
           
            for (int i = 0; i <= 63; i++)
            {
                SetIniValue(current_name, Convert.ToString(DB_Table), Convert.ToString(dataGridView1.Rows[i].Cells[2].Value));
                DB_Table -= 0.5;
            }
        

        }
        /*
        CH_A_ATT_EN_150M = 0;
        CH_A_ATT_EN_WIFI1 = 1;
        CH_A_ATT_EN_WIFI2 = 2;
        CH_A_ATT_EN_WIFI3 = 3;
        CH_A_ATT_EN_WIFI4 = 4;
        */
        private void FileLoad_button_Click(object sender, EventArgs e)
        {
            int current_status = Atten_comboBox.SelectedIndex;
            string temp,current_name = CH_A_ATT_EN_150M_Str;
            double DB_Table = Convert.ToDouble(ATTEN_31_5DB);
            this.Atten_comboBox.GetItemText(this.Atten_comboBox.SelectedItem);
            string selectedText = this.Atten_comboBox.GetItemText(this.Atten_comboBox.SelectedItem);
            current_name = selectedText;

            for (int i = 0; i <= 63; i++)
            {
                //SetIniValue(current_name, , Convert.ToString(dataGridView1.Rows[i].Cells[2].Value));
                temp = GetIniValue(current_name, Convert.ToString(DB_Table));
                dataGridView1.Rows[i].Cells[2].Value = temp;
                DB_Table -= 0.5;

            }
           
            //switch ()
        }

        private void Atten_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTableSet();
            HeaderIndexNumSet();
            Read_button_Click(sender,e);
        }

        private void CurrentModeGetFunc()
        {
            int cnt = 0;
            byte[] bytes = new byte[TABLE_UART_LENGTH];
            bytes[cnt++] = 0xBE;
            bytes[cnt++] = ATT_AB_CH_Read;
            bytes[cnt++] = TABLE_UARTDATA_LENGTH;
            for (int i = cnt; i < TABLE_UART_LENGTH - 2; i++)
            {
                bytes[i] = 0;
            }
            bytes[TABLE_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[TABLE_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }
        private void CurrentModeSetFunc()
        {
            int cnt = 0;
            byte[] bytes = new byte[75];
            bytes[cnt++] = 0xBE;
            bytes[cnt++] = 0;
            bytes[cnt++] = TABLE_UARTDATA_LENGTH;
            /*for (int i = 0; i < 70; i++)
            {
                bytes[cnt++] = 0;
            }*/
            bytes[73] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[74] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }
        private void ModeGet_button_Click(object sender, EventArgs e)
        {
            CurrentModeGetFunc();
        }


        private void Ach150Mhz_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            float data_val = Convert.ToSingle(Ach150Mhz_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret;

            bytes[cnt++] = 0xBE;
            bytes[cnt++] = ATT_A_CH_150M;
            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            QPC6614_Atten_Rx_DataCheck(data_val, out ret);
            bytes[cnt++] = ret;//3번째 byte Data 
            bytes[CHSET_UART_LENGTH -2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH -1] = 0xEB;
            try{try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}}catch{MessageBox.Show("Please, open port.");}
        }

        private void AchWifi_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            float data_val = Convert.ToSingle(Achwifi_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret;
            
            bytes[cnt++] = 0xBE;
            if (Achwifi2_4_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_WIFI2_4Ghz;
            }
            else if (Achwifi5_8_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_WIFI5_8Ghz;
            }

            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            if (data_val > 90)
            {
                data_val -= 90;
            

                PE43711_Atten_Rx_DataCheck(data_val, out ret);

                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = ret;
            }
            else if (data_val > 60)
            {
                data_val -= 60;
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
            }
            else if (data_val > 30)
            {
                data_val -= 30;
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
            }            
            else if (data_val >= 0)
            {
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
            }

            bytes[CHSET_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }

        private void Ach30g_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            byte data_val = Convert.ToByte(Ach30g_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret = 0;

            bytes[cnt++] = 0xBE;
            if (Ach30g28_28_5_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_30G_28_28_5;
            }
            else if (Ach30g28_5_29_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_30G_28_5_29;
            }
            else if (Ach30g29_29_5_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_30G_29_29_5;
            }
            else if (Ach30g29_5_30_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_A_CH_30G_29_5_30;
            }

            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            if (data_val > 60)
            {
                data_val -= 60;
                ADRF5720_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
            }
            else if (data_val >= 30)
            {
                data_val -= 30;
                HMC939_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = ADRF5720_ATTEN_0DB_VAL;
            }
            else if (data_val >= 0)
            {
                HMC939_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = HMC939_ATTEN_0DB_VAL;
                bytes[cnt++] = ADRF5720_ATTEN_0DB_VAL;
            }
          
            bytes[CHSET_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }

        private void Bch150Mhz_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            float data_val = Convert.ToSingle(Bch150Mhz_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret;

            bytes[cnt++] = 0xBE;
            bytes[cnt++] = ATT_B_CH_150M;
            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            QPC6614_Atten_Rx_DataCheck(data_val, out ret);
            bytes[cnt++] = ret;//3번째 byte Data 
            bytes[CHSET_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH - 1] = 0xEB;
            try { try { serialPort1.Write(bytes, 0, bytes.Length); } catch { MessageBox.Show("Please, open port."); } } catch { MessageBox.Show("Please, open port."); }
        }

        private void BchWifi_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            float data_val = Convert.ToSingle(Bchwifi_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret;

            bytes[cnt++] = 0xBE;
            if (Bchwifi2_4_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_WIFI2_4Ghz;
            }
            else if (Bchwifi5_8_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_WIFI5_8Ghz;
            }
            
            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            if (data_val > 90)
            {
                data_val -= 90;
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = ret;//3번째 byte Data 
            }
            else if (data_val > 60)
            {
                data_val -= 60;
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
            }
            else if (data_val > 30)
            {
                data_val -= 30;
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = PE43711_ATTEN_30DB_VAL;//3번째 byte Data 
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                

            }
            else if (data_val >= 0)
            {
                PE43711_Atten_Rx_DataCheck(data_val, out ret);
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
                bytes[cnt++] = PE43711_ATTEN_0DB_VAL;//3번째 byte Data 
            }

            bytes[CHSET_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH - 1] = 0xEB;
            try{serialPort1.Write(bytes, 0, bytes.Length);}catch{MessageBox.Show("Please, open port.");}
        }

        private void Bch30g_button_Click(object sender, EventArgs e)
        {
            int cnt = 0;
            byte data_val = Convert.ToByte(Bch30g_numericUpDown.Value);
            byte[] bytes = new byte[CHSET_UART_LENGTH];
            byte ret = 0;

            bytes[cnt++] = 0xBE;
            if (Bch30g28_28_5_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_30G_28_28_5;
            }
            else if (Bch30g28_5_29_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_30G_28_5_29;
            }
            else if (Bch30g29_29_5_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_30G_29_29_5;
            }
            else if (Bch30g29_5_30_radioButton.Checked == true)
            {
                bytes[cnt++] = ATT_B_CH_30G_29_5_30;
            }

            bytes[cnt++] = CHSET_UARTDATA_LENGTH;
            if (data_val > 60)
            {
                data_val -= 60;
                ADRF5720_Atten_Rx_DataCheck(data_val, out ret);

                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
            }
            else if (data_val >= 30)
            {
                data_val -= 30;
                HMC939_Atten_Rx_DataCheck(data_val, out ret);

                bytes[cnt++] = HMC939_ATTEN_30DB_VAL;
                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = ADRF5720_ATTEN_0DB_VAL;
            }
            else if (data_val >= 0)
            {
                HMC939_Atten_Rx_DataCheck(data_val, out ret);

                bytes[cnt++] = ret;//3번째 byte Data 
                bytes[cnt++] = HMC939_ATTEN_0DB_VAL;
                bytes[cnt++] = ADRF5720_ATTEN_0DB_VAL;
            }

            bytes[CHSET_UART_LENGTH - 2] = STH30_CreateCrc(bytes, (byte)(bytes.Length - 3));
            bytes[CHSET_UART_LENGTH - 1] = 0xEB;
            try { serialPort1.Write(bytes, 0, bytes.Length); } catch { MessageBox.Show("Please, open port."); }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Termaina_metroCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(Termainal_metroCheckBox.Checked == true)
            {
                Status_tabControl.TabPages.Add(Terminal_tabPage);
            }
            else
            {
                Status_tabControl.TabPages.Remove(Terminal_tabPage);
            }
        }

        private void Table_metroCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (Table_metroCheckBox.Checked == true)
            {
                Status_tabControl.TabPages.Add(Table_tabPage);
                HeaderIndexNumSet();
            }
            else
            {
                Status_tabControl.TabPages.Remove(Table_tabPage);
            }
        }
        private void Alarm_Init()
        {
            Ach150mOn_pictureBox.Visible = false;
            AchwifiOn_pictureBox.Visible = false;
            Ach30GOn_pictureBox.Visible = false;

            Bch150mOn_pictureBox.Visible = false;
            BchwifiOn_pictureBox.Visible = false;
            Bch30GOn_pictureBox.Visible = false;
            
        }
        private void Alarm_operate(byte[] data)
        {
            if (this.Ach150mOn_pictureBox.InvokeRequired)
            {
                SetByteCallback d = new SetByteCallback(Alarm_operate);
                this.Invoke(d, new object[] { data });
            }
            else
            {
                if (data[Bluecell_DATA] == 1)
                    Ach150mOn_pictureBox.Visible = false;
                else
                    Ach150mOn_pictureBox.Visible = true;
                if (data[Bluecell_DATA + 1] == 1)
                    AchwifiOn_pictureBox.Visible = false;
                else
                    AchwifiOn_pictureBox.Visible = true;
                if (data[Bluecell_DATA + 2] ==1)
                    Ach30GOn_pictureBox.Visible = false;
                else
                    Ach30GOn_pictureBox.Visible = true;


                if (data[Bluecell_DATA + 3] ==1)
                    Bch150mOn_pictureBox.Visible = false;
                else
                    Bch150mOn_pictureBox.Visible = true;

                if (data[Bluecell_DATA + 4] ==1)
                    BchwifiOn_pictureBox.Visible = false;
                else
                    BchwifiOn_pictureBox.Visible = true;

                if (data[Bluecell_DATA + 5] ==1)
                    Bch30GOn_pictureBox.Visible = false;
                else
                    Bch30GOn_pictureBox.Visible = true;
            }
        }

        private void Ach150Mhz_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Ach150Mhz_button_Click(sender, e);
            }
        }

        private void Achwifi_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AchWifi_button_Click(sender, e);
            }
        }

        private void Ach30g_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Ach30g_button_Click(sender, e);
            }
        }

        private void Bch150Mhz_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Bch150Mhz_button_Click(sender, e);
            }
        }

        private void Bchwifi_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BchWifi_button_Click(sender, e);
            }
        }

        private void Bch30g_numericUpDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Bch30g_button_Click(sender, e);
            }
        }

        private void All_Zero_Write_button_Click(object sender, EventArgs e)
        {
            AllCheckClick(sender,e);
            for(int i = 0; i < 64; i++)
                dataGridView1.Rows[i].Cells[2].Value =0;

            TableDataWrite();

        }

    }
}
