using HslCommunication;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace C_Windows_1
{
    public partial class TestPara : Form
    {
        int stage;
        public TestPara()
        {
            InitializeComponent();
        }


        private void TestPara_Load(object sender, EventArgs e)
        {
            try
            {
                //Form1.f1.Timer1.Stop();
                //Form1.f1.SerialPort1.Close();

                //ParaNum.SelectedIndex = 0;//Form1.f1.WorkStation .SelectedIndex ;

                //SerialPort1.PortName = Form1.f1.SerialPort1.PortName;
                //SerialPort1.BaudRate = Form1.f1.SerialPort1.BaudRate;
                //SerialPort1.DataBits = 8;
                //SerialPort1.StopBits = System.IO.Ports.StopBits.One;
                //SerialPort1.Parity = System.IO.Ports.Parity.None;
                //SerialPort1.Open();

                ParaNum.SelectedIndex = 0;
                label37.Visible = false;
                ParaName.Visible = false;


                ReadParameters(1);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        public static byte[] XdrFloat(float value)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
            {
                Byte BYTE1;
                BYTE1 = bytes[0];
                bytes[0] = bytes[2];
                bytes[2] = BYTE1;

                BYTE1 = bytes[1];
                bytes[1] = bytes[3];
                bytes[3] = BYTE1;
                return bytes;
            }
        }

        public OperateResult operateResult24;

        public static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;//延时秒数
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }



        public OperateResult operateResult;

        private void BtnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                if (FullTime.Text == "" ||    TestTime1.Text == "" || ExhaustTime.Text == "" ||  DelayTime1.Text == "" || DelayTime2.Text == "" || BLeakLPre.Text == "" || BLeakTPre.Text == "" || LUnit.Text == "" || PUnit.Text == "")
                {
                    MessageBox.Show("输入参数格式不对");
                }
                else
                {
                    if (FullTime.Text == "" ||   TestTime1.Text == "" || ExhaustTime.Text == "" ||  DelayTime1.Text == "" || DelayTime2.Text == "" || BLeakLPre.Text == "" || BLeakTPre.Text == "" || LUnit.Text == "" || PUnit.Text == "")
                    {
                        MessageBox.Show("输入参数格式不对");
                    }
                    else
                    {
                        BtnUpload.Text = "正在上传";
                        //BtnUpload.Enabled = false;
                        BtnUpload.ForeColor = Color.Red;
                        short a = Convert.ToInt16(Convert.ToDouble(FullTime.Text) * 10);//充气时间
                        Form1.f1.modbusClient.Write("1006", a);
                        //平衡时间不能写入，为大漏平衡时间和微漏平衡时间的和
                        //short b = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//平衡时间
                        short b = Convert.ToInt16(Convert.ToDouble(BalanTime.Text) * 10);
                        Form1.f1.modbusClient.Write("1007", b);
                        short c = Convert.ToInt16(Convert.ToDouble(TestTime1.Text) * 10);//检测时间1
                        Form1.f1.modbusClient.Write("1008", c);
                        short d = Convert.ToInt16(Convert.ToDouble(ExhaustTime.Text) * 10);//排气时间
                        Form1.f1.modbusClient.Write("1009", d);

                        
                        short f = Convert.ToInt16(Convert.ToDouble(DelayTime1.Text) * 10);//延时1
                        Form1.f1.modbusClient.Write("1011", f);
                        short g = Convert.ToInt16(Convert.ToDouble(DelayTime2.Text) * 10);//延时2
                        Form1.f1.modbusClient.Write("1012", g);


                        List<byte> bigleakmax = new List<byte>();//压差上限
                        bigleakmax.AddRange(XdrFloat(Convert.ToSingle(BLeakTPre.Text)));
                        byte[] bigleakda = bigleakmax.ToArray();
                        Form1.f1.modbusClient.Write("1017", bigleakda);

                        List<byte> bigleakmin = new List<byte>();//压差下限
                        bigleakmin.AddRange(XdrFloat(Convert.ToSingle(BLeakLPre.Text)));
                        byte[] bigleakxiao = bigleakmin.ToArray();
                        Form1.f1.modbusClient.Write("1019", bigleakxiao);

                        //BLeakLPre


                        List<byte> parmEvolume = new List<byte>();//等效容积
                        parmEvolume.AddRange(XdrFloat(Convert.ToSingle(Evolume.Text)));
                        byte[] pEvolume = parmEvolume.ToArray();
                        Form1.f1.modbusClient.Write("1025", pEvolume);


                        List<byte> parmFPtoplimit = new List<byte>();//充气压力上限
                        parmFPtoplimit.AddRange(XdrFloat(Convert.ToSingle(FPtoplimit.Text)));
                        byte[] toplimit = parmFPtoplimit.ToArray();
                        Form1.f1.modbusClient.Write("1013", toplimit);

                        List<byte> parmFPlowlimit = new List<byte>();//充气压力下限
                        parmFPlowlimit.AddRange(XdrFloat(Convert.ToSingle(FPlowlimit.Text)));
                        byte[] lowlimit = parmFPlowlimit.ToArray();
                        Form1.f1.modbusClient.Write("1015", lowlimit);

                        //short BBaTime = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//大漏平衡时间                      
                        //Form1.f1.modbusClient.Write("34", BBaTime);

                        //short SBaTime = Convert.ToInt16(Convert.ToDouble(SBalanTime.Text) * 10);//微漏平衡时间                      
                        //Form1.f1.modbusClient.Write("35", SBaTime);

                        List<byte> parmLeaktoplimit = new List<byte>();//泄露量上限
                        parmLeaktoplimit.AddRange(XdrFloat(Convert.ToSingle(Leaktoplimit.Text)));
                        byte[] Leaktop = parmLeaktoplimit.ToArray();
                        Form1.f1.modbusClient.Write("1021", Leaktop);

                        List<byte> parmLeaklowlimit = new List<byte>();//泄露量下限
                        parmLeaklowlimit.AddRange(XdrFloat(Convert.ToSingle(Leaklowlimit.Text)));
                        byte[] Leaklow = parmLeaklowlimit.ToArray();
                        Form1.f1.modbusClient.Write("1023", Leaklow);

                        //单位
                        ushort punit1 = Convert.ToUInt16(PUnit.SelectedIndex);
                        ushort lunit1 = Convert.ToUInt16(LUnit.SelectedIndex);
                        Form1.f1.modbusClient.Write("1027", punit1);
                        operateResult= Form1.f1.modbusClient.Write("1028", lunit1);


                        if (operateResult.IsSuccess)
                        {
                            MessageBox.Show("上传参数成功");
                            
                        }
                        else
                        {
                            MessageBox.Show("上传参数失败,请检查连接状态后重新上传");
                            
                        }
                        BtnUpload.Text = "上传参数";
                        BtnUpload.ForeColor = Color.Black;
                        BtnUpload.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        private void Timer5_Tick(object sender, EventArgs e)
        {
            //try
            //{
            //    int a = Convert.ToInt32(BLeakTPre.Text);
            //    int b = Convert.ToInt32(BLeakLPre.Text);



            //    string a1 = a.ToString("x4");
            //    string b1 = b.ToString("x4");

            //    if (b1.Length == 8)
            //    {
            //        b1 = b1.Substring(4, 4);
            //    }

            //    byte[] ltl1 = BitConverter.GetBytes(Convert.ToSingle(Leaktoplimit.Text));
            //    string ltl2 = BitConverter.ToString(ltl1.Reverse().ToArray()).Replace("-", "");
            //    string leaktoplim = ltl2.Substring(4, 4) + ltl2.Substring(0, 4);


            //    string lunit = LUnit.SelectedIndex.ToString("x4");
            //    string punit = PUnit.SelectedIndex.ToString("x4");


            //    byte[] ev1 = BitConverter.GetBytes(Convert.ToSingle(Evolume.Text));
            //    string ev2 = BitConverter.ToString(ev1.Reverse().ToArray()).Replace("-", "");
            //    string evol = ev2.Substring(4, 4) + ev2.Substring(0, 4);
            //    byte[] lllim1 = BitConverter.GetBytes(Convert.ToSingle(Leaklowlimit.Text));
            //    string lllim2 = BitConverter.ToString(lllim1.Reverse().ToArray()).Replace("-", "");
            //    string leaklowlim = lllim2.Substring(4, 4) + lllim2.Substring(0, 4);
            //    byte[] fpllim1 = BitConverter.GetBytes(Convert.ToSingle(FPlowlimit.Text));
            //    string fpllim2 = BitConverter.ToString(fpllim1.Reverse().ToArray()).Replace("-", "");
            //    string fplowlim = fpllim2.Substring(4, 4) + fpllim2.Substring(0, 4);


            //    //检测时间2对应的地址为0030.在此处写入
            //    int ttime2 = Convert.ToInt32(TestTime2.Text) * 10;
            //    string testtime2 = ttime2.ToString("x4");

            //    SendText.Text = Form1.f1.Station.Text + " 10 00 24 00 0D 1A";
            //    SendText.Text += a1;
            //    SendText.Text += b1;
            //    SendText.Text += leaktoplim;
            //    SendText.Text += lunit;
            //    SendText.Text += punit;
            //    SendText.Text += evol;
            //    SendText.Text += leaklowlim;
            //    SendText.Text += fplowlim;
            //    SendText.Text += testtime2;

            //    Send();
            //    stage = 10;
            //    Timer5.Stop();
            //    Timer6.Interval = 1000;
            //    Timer6.Start();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }

        private void Timer6_Tick(object sender, EventArgs e)
        {
            //try
            //{

            //    BtnUpload.Text = "上传参数";
            //    BtnUpload.ForeColor = Color.Black;
            //    Timer6.Stop();
            //}
            //catch
            //{
            //    Timer6.Stop();
            //}

        }



        private void BtnRead_Click(object sender, EventArgs e)
        {
            try
            {
                Parameters();
                if (Form1.f1.modbusClient.Read("1006", (ushort)1).IsSuccess)
                {
                    MessageBox.Show("参数读取成功");
                }
                else
                {
                    MessageBox.Show("参数读取失败");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void Booltomath(int sta, UInt16[] shangweiji)
        {
            try
            {
                OperateResult<bool> reslutget;
                reslutget = Form1.f1.modbusClient.ReadBool(Convert.ToString(sta));
                if (reslutget.IsSuccess == true)
                {
                    if (reslutget.Content == true)
                    {
                        shangweiji[sta] = 1;
                    }
                    else
                    {
                        shangweiji[sta] = 0;
                    }
                }
                //return shangweiji[sta];
            }
            catch (Exception)
            {

                throw;
            }

        }

        public short Digitalconversion(ushort readdata)
        {
            int Z;
            if (readdata > 32768)
                Z = (int)readdata - 65536;
            else
                Z = readdata;


            return (short)Z;
        }

        public void Parameters()
        {
            Form1.f1.Modbusread(1006, 7, Form1.ReadData);
            Form1.f1.Modbusread(1013, 16, Form1.ReadData);        
            
            FullTime.Text = ((Form1.ReadData[1006] / 10).ToString());
            BalanTime.Text = ((Form1.ReadData[1007] / 10).ToString());
            TestTime1.Text = (Form1.ReadData[1008] / 10).ToString();
            //TestTime2.Text = ((Form1.ReadData[48] / 10).ToString());
            ExhaustTime.Text = (Form1.ReadData[1009] / 10).ToString();
            //BypassTime.Text = (Form1.ReadData[29] / 10).ToString();
            DelayTime1.Text = (Form1.ReadData[1011] / 10).ToString();
            DelayTime2.Text = ((Form1.ReadData[1012] / 10).ToString());
            Evolume.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1026], Form1.ReadData[1025]).ToString();
            //参数名称
            //ParaName.Text = 
            FPtoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1014], Form1.ReadData[1013]).ToString();
            FPlowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1016], Form1.ReadData[1015]).ToString();
            //BBalanTime.Text = ((Form1.ReadData[34] / 10).ToString());
            //SBalanTime.Text = ((Form1.ReadData[35] / 10).ToString());
            BLeakTPre.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1018], Form1.ReadData[1017]).ToString();
            BLeakLPre.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1020], Form1.ReadData[1019]).ToString();
            Leaktoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1022], Form1.ReadData[1021]).ToString();
            Leaklowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[1024], Form1.ReadData[1023]).ToString();
            PUnit.SelectedIndex = Form1.ReadData[1027];
            LUnit.SelectedIndex = Form1.ReadData[1028];
            label24.Text = PUnit.Text;
            label25.Text = PUnit.Text;
            label28.Text = LUnit.Text;
            label29.Text = LUnit.Text;


        }


        private void BtnKeep_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(ParaNum.Text);
            SetParameters(i);
        }



        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            System.Threading.Thread.Sleep(100);
            int len = SerialPort1.BytesToRead;//获取可以读取的字节数
            if (len > 1)
            {
                byte[] buff = new byte[len];//创建缓存数据数组
                SerialPort1.Read(buff, 0, len);//把数据读取到buff数组
                                               //string str = Encoding.Default.GetString(buff);
                Invoke((new System.Action(() => //串口接收事件会自动创建线程
                {
                    ReceiveText.Clear();
                    ReceiveText.Text = ByteToHexStr(buff);
                    SerialPort1.DiscardInBuffer();
                    Stagenum();
                }
                    )));
            }
        }


        //十六进制接收
        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            try
            {
                if (bytes != null)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        returnStr += bytes[i].ToString("X2");//每个字节转换成两位十六进制
                                                             //     returnStr += " ";//两个16进制用空格隔开,方便看数据
                    }
                }
                return returnStr;
            }
            catch (Exception)
            {
                return returnStr;
            }
        }


        private void Stagenum()
        {
            switch (stage)
            {
                case 2://此时为读取参数并数据转换
                    string str2;
                    str2 = ReceiveText.Text;
                    //     str2 = str2.Replace(" ", "");

                    string pname1 = str2.Substring(8, 2);
                    string pname2 = str2.Substring(6, 2);
                    string pname3 = str2.Substring(12, 2);
                    string pname4 = str2.Substring(10, 2);
                    string pname5 = str2.Substring(16, 2);
                    string pname6 = str2.Substring(14, 2);
                    string pname7 = str2.Substring(20, 2);
                    string pname8 = str2.Substring(18, 2);
                    string pname9 = str2.Substring(24, 2);
                    string pname10 = str2.Substring(22, 2);



                    UInt32 pn1 = Convert.ToUInt32(pname1, 16);
                    byte[] p1 = System.BitConverter.GetBytes(pn1);
                    string paraname1 = System.Text.ASCIIEncoding.ASCII.GetString(p1);//字节数组转成对应的ASCII码

                    UInt32 pn2 = Convert.ToUInt32(pname2, 16);
                    byte[] p2 = System.BitConverter.GetBytes(pn2);
                    string paraname2 = System.Text.ASCIIEncoding.ASCII.GetString(p2);

                    UInt32 pn3 = Convert.ToUInt32(pname3, 16);
                    byte[] p3 = System.BitConverter.GetBytes(pn3);
                    string paraname3 = System.Text.ASCIIEncoding.ASCII.GetString(p3);

                    UInt32 pn4 = Convert.ToUInt32(pname4, 16);
                    byte[] p4 = System.BitConverter.GetBytes(pn4);
                    string paraname4 = System.Text.ASCIIEncoding.ASCII.GetString(p4);

                    UInt32 pn5 = Convert.ToUInt32(pname5, 16);
                    byte[] p5 = System.BitConverter.GetBytes(pn5);
                    string paraname5 = System.Text.ASCIIEncoding.ASCII.GetString(p5);

                    UInt32 pn6 = Convert.ToUInt32(pname6, 16);
                    byte[] p6 = System.BitConverter.GetBytes(pn6);
                    string paraname6 = System.Text.ASCIIEncoding.ASCII.GetString(p6);

                    UInt32 pn7 = Convert.ToUInt32(pname7, 16);
                    byte[] p7 = System.BitConverter.GetBytes(pn7);
                    string paraname7 = System.Text.ASCIIEncoding.ASCII.GetString(p7);

                    UInt32 pn8 = Convert.ToUInt32(pname8, 16);
                    byte[] p8 = System.BitConverter.GetBytes(pn8);
                    string paraname8 = System.Text.ASCIIEncoding.ASCII.GetString(p8);

                    UInt32 pn9 = Convert.ToUInt32(pname9, 16);
                    byte[] p9 = System.BitConverter.GetBytes(pn9);
                    string paraname9 = System.Text.ASCIIEncoding.ASCII.GetString(p9);

                    UInt32 pn10 = Convert.ToUInt32(pname10, 16);
                    byte[] p10 = System.BitConverter.GetBytes(pn10);
                    string paraname10 = System.Text.ASCIIEncoding.ASCII.GetString(p10);


                    ParaName.Text = paraname1;
                    ParaName.Text += paraname2;
                    ParaName.Text += paraname3;
                    ParaName.Text += paraname4;
                    ParaName.Text += paraname5;
                    ParaName.Text += paraname6;
                    ParaName.Text += paraname7;
                    ParaName.Text += paraname8;
                    ParaName.Text += paraname9;
                    ParaName.Text += paraname10;





                    string hexstring_full = str2.Substring(26, 4);
                    string hexstring_balan = str2.Substring(30, 4);
                    string hexstring_test = str2.Substring(34, 4);
                    string hexstring_exhasut = str2.Substring(38, 4);
                    string hexstring_bypass = str2.Substring(42, 4);
                    string hexstring_delay1 = str2.Substring(46, 4);
                    string hexstring_delay2 = str2.Substring(50, 4);



                    string hexstring_bleakbalan = str2.Substring(62, 4);
                    string hexstring_sleakbalan = str2.Substring(66, 4);
                    string hexstring_bleaktoppre = str2.Substring(70, 4);
                    string hexstring_bleaklowpre = str2.Substring(74, 4);

                    //      string hexstring_bleaktest = str2.Substring(124, 4);


                    string hexstring_lunit = str2.Substring(86, 4);
                    string hexstring_punit = str2.Substring(90, 4);


                    //十六进制转十进制
                    int full = Int32.Parse(hexstring_full, System.Globalization.NumberStyles.HexNumber) / 10;
                    int balan = Int32.Parse(hexstring_balan, System.Globalization.NumberStyles.HexNumber) / 10;
                    int test = Int32.Parse(hexstring_test, System.Globalization.NumberStyles.HexNumber) / 10;

                    double exhaust = Convert.ToDouble(Convert.ToInt32(hexstring_exhasut, 16)) / 10;

                    int bypass = Int32.Parse(hexstring_bypass, System.Globalization.NumberStyles.HexNumber) / 10;

                    double delay1 = Int32.Parse(hexstring_delay1, System.Globalization.NumberStyles.HexNumber);
                    double delay2 = Int32.Parse(hexstring_delay2, System.Globalization.NumberStyles.HexNumber);
                    double delay11 = delay1 / 10;
                    double delay22 = delay2 / 10;



                    int bleakbalan = Int32.Parse(hexstring_bleakbalan, System.Globalization.NumberStyles.HexNumber) / 10;
                    int sleakbalan = Int32.Parse(hexstring_sleakbalan, System.Globalization.NumberStyles.HexNumber) / 10;
                    int bleaktoppre = Int32.Parse(hexstring_bleaktoppre, System.Globalization.NumberStyles.HexNumber);
                    int bleaklowpre = Int32.Parse(hexstring_bleaklowpre, System.Globalization.NumberStyles.HexNumber);


                    //   int bleaktest = Int32.Parse(hexstring_bleaktest, System.Globalization.NumberStyles.HexNumber) / 10;


                    int lunit = Int32.Parse(hexstring_lunit, System.Globalization.NumberStyles.HexNumber);
                    int punit = Int32.Parse(hexstring_punit, System.Globalization.NumberStyles.HexNumber);





                    FullTime.Text = full.ToString();
                    BalanTime.Text = balan.ToString();
                    TestTime1.Text = test.ToString();
                    ExhaustTime.Text = exhaust.ToString();
                    BypassTime.Text = bypass.ToString();
                    DelayTime1.Text = delay11.ToString();
                    DelayTime2.Text = delay22.ToString();

                    BBalanTime.Text = bleakbalan.ToString();
                    SBalanTime.Text = sleakbalan.ToString();
                    //   TestTime2 .Text = bleaktest.ToString();
                    BLeakTPre.Text = bleaktoppre.ToString();
                    if (bleaklowpre > 32767)
                    {
                        string bleaklowpre2 = "FFFFFFFFFFFF" + hexstring_bleaklowpre;
                        long bleaklowpre3 = Convert.ToInt64(bleaklowpre2, 16);
                        BLeakLPre.Text = bleaklowpre3.ToString();
                    }
                    else
                    {
                        BLeakLPre.Text = bleaklowpre.ToString();
                    }







                    LUnit.SelectedIndex = lunit;
                    PUnit.SelectedIndex = punit;

                    label24.Text = PUnit.Text;
                    label25.Text = PUnit.Text;
                    //   label50.Text = PUnit.Text;

                    label28.Text = LUnit.Text;
                    label29.Text = LUnit.Text;
                    //  label49.Text = LUnit.Text;

                    string hexstring_fptop1 = str2.Substring(54, 4);
                    string hexstring_fptop2 = str2.Substring(58, 4);
                    string hexstring_fptop = hexstring_fptop2 + hexstring_fptop1;

                    string hexstring_ltop1 = str2.Substring(78, 4);
                    string hexstring_ltop2 = str2.Substring(82, 4);
                    string hexstring_ltop = hexstring_ltop2 + hexstring_ltop1;

                    string hexstring_evol1 = str2.Substring(94, 4);
                    string hexstringg_evol2 = str2.Substring(98, 4);
                    string hexstringg_evol = hexstringg_evol2 + hexstring_evol1;

                    string hexstring_llow1 = str2.Substring(102, 4);
                    string hexstring_llow2 = str2.Substring(106, 4);
                    string hexstring_llow = hexstring_llow2 + hexstring_llow1;

                    string hexstring_fplow1 = str2.Substring(110, 4);
                    string hexstring_fplow2 = str2.Substring(114, 4);
                    string hexstring_fplow = hexstring_fplow2 + hexstring_fplow1;


                    string bleaktesttime = str2.Substring(118, 4);
                    int testtime2 = Convert.ToInt32(bleaktesttime, 16) / 10;
                    TestTime2.Text = testtime2.ToString();


                    UInt32 x1 = Convert.ToUInt32(hexstring_fptop, 16);//字符串转16进制32位无符号整数
                    FPtoplimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x1), 0).ToString();//IEEE754 字节转换float
                    UInt32 x2 = Convert.ToUInt32(hexstring_ltop, 16);//字符串转16进制32位无符号整数
                    Leaktoplimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x2), 0).ToString();//IEEE754 字节转换float
                    UInt32 x3 = Convert.ToUInt32(hexstringg_evol, 16);//字符串转16进制32位无符号整数
                    Evolume.Text = BitConverter.ToSingle(BitConverter.GetBytes(x3), 0).ToString();//IEEE754 字节转换float
                    UInt32 x4 = Convert.ToUInt32(hexstring_llow, 16);//字符串转16进制32位无符号整数
                    Leaklowlimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x4), 0).ToString();//IEEE754 字节转换float
                    UInt32 x5 = Convert.ToUInt32(hexstring_fplow, 16);//字符串转16进制32位无符号整数
                    FPlowlimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x5), 0).ToString();//IEEE754 字节转换float


                    Timer3.Interval = 150;
                    Timer3.Start();


                    break;

                case 3:
                    string str3;
                    str3 = ReceiveText.Text;
                    string positive = str3.Substring(6, 2);
                    int pos = Convert.ToInt32(positive, 16);
                    if (pos == 1)
                    {
                        Test_Type.SelectedIndex = 0;
                    }
                    else
                    {
                        Test_Type.SelectedIndex = 1;
                    }
                    break;



            }


        }


        //发送过程
        private void Send()
        {

            //将CRC冗余码添加进指令中
            String str = SendText.Text;
            byte[] byt = StrtoHexbyte(str);
            int str2;
            str2 = Crc16_Modbus(byt, byt.Length);
            //byte byte1 =(byte)((str2 >> 8)&0xff);
            string str3;
            // str3=(( str2 >> 8) & 0xff).ToString ();
            str3 = Convert.ToString((str2 >> 8) & 0xff, 16);
            string str4;
            str4 = Convert.ToString(str2 & 0xff, 16);
            // string str4;
            // str4 = (str2 & 0xff).ToString();
            // string str3;
            if (str3.Length == 1)
            {

                str3 = "0" + str3;
                //   textBox2.Text = textBox2.Text + str4 + str3;
            }
            if (str4.Length == 1)
            {
                str4 = "0" + str4;
                //  textBox2.Text = textBox2.Text + str4 + str3;
            }
            SendText.Text = SendText.Text + str4 + str3;
            String str5 = SendText.Text;
            if (str5.Length > 0)
            {

                byte[] byt2 = StrtoHexbyte(str5);
                SerialPort1.Write(byt2, 0, byt2.Length);//发送数据

            }

        }


        //计算CRC冗余码
        int Crc16_Modbus(byte[] modbusdata, int length)
        {
            int i, j;
            int crc = 0xffff;//0xffff or 0
            for (i = 0; i < length; i++)
            {
                crc ^= modbusdata[i] & 0xff;
                for (j = 0; j < 8; j++)
                {
                    if ((crc & 0x01) == 1)
                    {
                        crc = (crc >> 1) ^ 0xa001;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }

            return crc;
        }



        //将发送数据转为十六进制数据
        private static byte[] StrtoHexbyte(String hexstring)
        {
            int i;
            hexstring = hexstring.Replace(" ", "");

            byte[] returnBytes = new byte[(hexstring.Length) / 2];
            try
            {
                for (i = 0; i < returnBytes.Length; i++)
                {
                    returnBytes[i] = Convert.ToByte(hexstring.Substring(i * 2, 2), 16);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            return returnBytes;

        }

        private void TestPara_FormClosing(object sender, FormClosingEventArgs e)
        {
            //try
            //{
            //    SerialPort1.Close();
            //    if (Form1.f1.label6.Text == "已连接")
            //    {
            //        Form1.f1.SerialPort1.Open();
            //        Form1.f1.Timer1.Interval = 500;
            //        Form1.f1.Timer1.Start();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}

        }


        OperateResult<bool> operateResult25;
        OperateResult operateResult1;
        OperateResult operateResult2;
        OperateResult operateResult3;
        OperateResult operateResult4;

        private void Modify_Type_Click(object sender, EventArgs e)
        {
            if (Form1.f1.CH1.IsOpen() == true)
            {
                if (Test_Type.SelectedIndex == 0)
                {
                    operateResult1 = Form1.f1.CH1.Write("605", false);
                    if (operateResult1.IsSuccess)
                    {
                        Form1.f1.CH1.Write("604", true);
                    }

                    //SendText.Text = Form1.f1.Station.Text + " 05 02 5D 00 00 ";//关闭负压
                    //Send();
                    //stage = 10;
                    //Timer1.Interval = 50;
                    //Timer1.Start();
                }
                if (Test_Type.SelectedIndex == 1)
                {
                    operateResult2 = Form1.f1.CH1.Write("604", false);
                    if (operateResult2.IsSuccess)
                    {
                        Form1.f1.CH1.Write("605", true);
                    }
                }
                else
                {
                    operateResult3 = Form1.f1.CH1.Write("605", false);
                    if (operateResult3.IsSuccess)
                    {
                        Form1.f1.CH1.Write("604", true);
                    }
                }
            }
            else
            {
                Form1.f1.CH1.Open();
                if (Test_Type.SelectedIndex == 0)
                {
                    operateResult4 = Form1.f1.CH1.Write("605", false);
                    if (operateResult4.IsSuccess)
                    {
                        Form1.f1.CH1.Write("604", true);
                    }

                    //SendText.Text = Form1.f1.Station.Text + " 05 02 5D 00 00 ";//关闭负压
                    //Send();
                    //stage = 10;
                    //Timer1.Interval = 50;
                    //Timer1.Start();
                }
                if (Test_Type.SelectedIndex == 1)
                {
                    operateResult24 = Form1.f1.CH1.Write("604", false);
                    if (operateResult24.IsSuccess)
                    {
                        Form1.f1.CH1.Write("605", true);
                    }
                }
            }
        }




        //开启正压测试选项
        private void Timer1_Tick(object sender, EventArgs e)
        {
            SendText.Text = Form1.f1.Station.Text + " 05 02 5C FF 00 ";
            Send();
            stage = 10;
            Timer1.Stop();
        }

        //开启负压测试选项
        private void Timer2_Tick(object sender, EventArgs e)
        {
            SendText.Text = Form1.f1.Station.Text + " 05 02 5D FF 00 ";
            Send();
            stage = 10;
            Timer2.Stop();
        }

        //读取测试模式，点击了读取参数才可以读取测试模式
        private void Timer3_Tick(object sender, EventArgs e)
        {
            SendText.Text = Form1.f1.Station.Text + " 01 02 5C 00 01";
            //    CrcNum();
            Send();
            stage = 3;
            Timer3.Stop();
        }

        private void ParaNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = ParaNum.SelectedIndex + 1;
            ReadParameters(i);
        }



        //写入注册表
        private void SetParameters(int i)
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            regName.SetValue("fulltime" + i, FullTime.Text);

            regName.SetValue("balantime" + i, BalanTime.Text);

            regName.SetValue("testtime1" + i, TestTime1.Text);

            regName.SetValue("testtime2" + i, TestTime2.Text);

            regName.SetValue("exhausttime" + i, ExhaustTime.Text);

            regName.SetValue("bypasstime" + i, BypassTime.Text);

            regName.SetValue("delaytime1" + i, DelayTime1.Text);

            regName.SetValue("delaytime2" + i, DelayTime2.Text);

            regName.SetValue("evolume" + i, Evolume.Text);

            regName.SetValue("paraname" + i, ParaName.Text);




            regName.SetValue("fptoplimit" + i, FPtoplimit.Text);

            regName.SetValue("fplowlimit" + i, FPlowlimit.Text);

            //   regName.SetValue("bptoplimit" + i, BPtoplimit .Text);

            //   regName.SetValue("bplowlimit" + i, BPlowlimit .Text);

            regName.SetValue("blptoplimit" + i, BLeakTPre.Text);

            regName.SetValue("blplowlimit" + i, BLeakLPre.Text);

            regName.SetValue("leaktoplimit" + i, Leaktoplimit.Text);

            regName.SetValue("leaklowlimit" + i, Leaklowlimit.Text);

            regName.SetValue("bbalantime" + i, BBalanTime.Text);

            regName.SetValue("sbalantime" + i, SBalanTime.Text);




            regName.SetValue("punit" + i, PUnit.SelectedIndex);

            regName.SetValue("lunit" + i, LUnit.SelectedIndex);


            regName.Close();
        }



        //读取注册表
        private void ReadParameters(int i)
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            if (regName.GetValue("fulltime" + i) is null)
            {
                FullTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str1 = regName.GetValue("fulltime" + i).ToString();
                FullTime.Text = str1;

            }


            if (regName.GetValue("balantime" + i) is null)
            {
                BalanTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str2 = regName.GetValue("balantime" + i).ToString();
                BalanTime.Text = str2;

            }



            if (regName.GetValue("testtime1" + i) is null)
            {
                TestTime1.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str3 = regName.GetValue("testtime1" + i).ToString();
                TestTime1.Text = str3;

            }

            if (regName.GetValue("testtime2" + i) is null)
            {
                TestTime2.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str11 = regName.GetValue("testtime2" + i).ToString();
                TestTime2.Text = str11;


            }

            if (regName.GetValue("exhausttime" + i) is null)
            {
                ExhaustTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str4 = regName.GetValue("exhausttime" + i).ToString();
                ExhaustTime.Text = str4;

            }


            if (regName.GetValue("bypasstime" + i) is null)
            {
                BypassTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str4 = regName.GetValue("bypasstime" + i).ToString();
                BypassTime.Text = str4;

            }


            if (regName.GetValue("delaytime1" + i) is null)
            {
                DelayTime1.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str4 = regName.GetValue("delaytime1" + i).ToString();
                DelayTime1.Text = str4;

            }


            if (regName.GetValue("delaytime2" + i) is null)
            {
                DelayTime2.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str4 = regName.GetValue("delaytime2" + i).ToString();
                DelayTime2.Text = str4;

            }



            if (regName.GetValue("evolume" + i) is null)
            {
                Evolume.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str12 = regName.GetValue("evolume" + i).ToString();
                Evolume.Text = str12;

            }


            if (regName.GetValue("paraname" + i) is null)
            {
                ParaName.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str13 = regName.GetValue("paraname" + i).ToString();
                ParaName.Text = str13;

                //  regName.Close();
            }








            if (regName.GetValue("fptoplimit" + i) is null)
            {
                FPtoplimit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str5 = regName.GetValue("fptoplimit" + i).ToString();
                FPtoplimit.Text = str5;

            }


            if (regName.GetValue("fplowlimit" + i) is null)
            {
                FPlowlimit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str6 = regName.GetValue("fplowlimit" + i).ToString();
                FPlowlimit.Text = str6;


            }

            if (regName.GetValue("blptoplimit" + i) is null)
            {
                BLeakTPre.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str7 = regName.GetValue("blptoplimit" + i).ToString();
                BLeakTPre.Text = str7;

            }

            if (regName.GetValue("blplowlimit" + i) is null)
            {
                BLeakLPre.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str8 = regName.GetValue("blplowlimit" + i).ToString();
                BLeakLPre.Text = str8;

            }

            if (regName.GetValue("leaktoplimit" + i) is null)
            {
                Leaktoplimit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str9 = regName.GetValue("leaktoplimit" + i).ToString();
                Leaktoplimit.Text = str9;

            }


            if (regName.GetValue("leaklowlimit" + i) is null)
            {
                Leaklowlimit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str10 = regName.GetValue("leaklowlimit" + i).ToString();
                Leaklowlimit.Text = str10;


            }



            if (regName.GetValue("bbalantime" + i) is null)
            {
                BBalanTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str10 = regName.GetValue("bbalantime" + i).ToString();
                BBalanTime.Text = str10;


            }
            if (regName.GetValue("sbalantime" + i) is null)
            {
                SBalanTime.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str10 = regName.GetValue("sbalantime" + i).ToString();
                SBalanTime.Text = str10;


            }







            if (regName.GetValue("punit" + i) is null)
            {
                PUnit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str13 = regName.GetValue("punit" + i).ToString();
                PUnit.SelectedIndex = Convert.ToInt32(str13);
            }


            if (regName.GetValue("lunit" + i) is null)
            {
                LUnit.Text = null;
            }
            else
            {
                regName.OpenSubKey("User");
                string str13 = regName.GetValue("lunit" + i).ToString();
                LUnit.SelectedIndex = Convert.ToInt32(str13);

                regName.Close();
            }



        }
    }
}
