using EasyModbus;
using Sunny.UI;
using System;
using System.Linq;
using System.Windows.Forms;

namespace SLC1_N
{
    public partial class Electricity : Form
    {
        public Electricity()
        {
            InitializeComponent();
        }

        public delegate void MySignalHandler(int msg, bool mode); // 声明信号
        public event MySignalHandler Signal_PLC_WriteCoil;

        private void Electricity_Load(object sender, EventArgs e)
        {
            label90.Visible = false;
            tb_RelieveDelay.Visible = false;
            label104.Visible = false;

            // 读取一次本地参数
            cb_MachineNum.SelectedIndex = 0;
            cb_ParaNum.SelectedIndex = 0;
            ReadParametersJson("1","1");

            if (Form1.f1.User == "操作员")
            {
                chk_ChkBee.Enabled = false;
                chk_CH1CHKVacuum.Enabled = false; // 真空复选框
                chk_CH2CHKVacuum.Enabled = false;

                bt_BtnKeep.Enabled = false;
                tb_FullTime.Enabled = false;
                tb_BalanTime.Enabled = false;
                tb_TestTime1.Enabled = false;
                tb_ExhaustTime.Enabled = false;
                tb_DelayTime1.Enabled = false;
                tb_DelayTime2.Enabled = false;
                tb_RelieveDelay.Enabled = false;
                tb_Evolume.Enabled = false;
                tb_FPtoplimit.Enabled = false;
                tb_FPlowlimit.Enabled = false;
                tb_BalanPreMax.Enabled = false;
                tb_BalanPreMin.Enabled = false;
                tb_Leaktoplimit.Enabled = false;
                tb_Leaklowlimit.Enabled = false;
                cb_PUnit.Enabled = false;
                cb_LUnit.Enabled = false;
            }
            if  (Form1.f1.User == "工程师")
            {
                cb_PUnit.Enabled = false;
                cb_LUnit.Enabled = false;
                tb_FullTime.Enabled = false;
                tb_BalanTime.Enabled=false;
                tb_TestTime1.Enabled = false;
                tb_ExhaustTime.Enabled=false;
                tb_FPlowlimit.Enabled=false;
                tb_FPtoplimit.Enabled=false;
                tb_Leaktoplimit.Enabled=false;
                tb_Leaklowlimit.Enabled=false;
            }

            if (Form1.f1.User != "厂商")
            {
                //消失
                label111.Visible = false;
                label112.Visible = false;

                label117.Visible = false;
                tb_BalanPreMax.Visible = false;
                tb_BalanPreMin.Visible = false;
                label116.Visible = false;
            }

            //PressMax.Text = Form1.f1.PressMax.ToString();
            //PressMin.Text = Form1.f1.PressMin.ToString();
            //PressingTimeMax.Text = Form1.f1.PressingTimeMax.ToString();
            //PressingTimeMin.Text = Form1.f1.PressingTimeMin.ToString();
            //ReadParameters(MachineNum.SelectedIndex + 1, ParaNum.SelectedIndex + 1);

            // Json记录读取
            var config = new JsonConfig("config.json");
            tb_code1len.Text = config.GetValue<string>("CodeLength1", "9");
            tb_code2len.Text = config.GetValue<string>("CodeLength2", "9");
            tb_code3len.Text = config.GetValue<string>("CodeLength3", "9");
            tb_code4len.Text = config.GetValue<string>("CodeLength4", "9");

            chk_ch1saoma.Checked = config.GetValue<bool>("chk_ch1saoma", true);
            chk_ch2saoma.Checked = config.GetValue<bool>("chk_ch2saoma", true);
            chk_ch3saoma.Checked = config.GetValue<bool>("chk_ch3saoma", true);
            chk_ch4saoma.Checked = config.GetValue<bool>("chk_ch4saoma", true);

            // 连续测试/次数
            chk_ContinueTest.Checked = config.GetValue<bool>("chk_ContinueTest", true);
            tb_ContinueTestCount.Enabled = chk_ContinueTest.Checked;
            tb_ContinueTestCount.Text = config.GetValue<int>("tb_ContinueTestCount", 1).ToString();
        }

        //上传参数
        private void BtnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                bt_BtnUpload.Enabled = false;
                tb_RelieveDelay.Text = "0";
                if (tb_FullTime.Text == "" || tb_BalanTime.Text == "" || tb_TestTime1.Text == "" || tb_ExhaustTime.Text == "" || 
                    tb_DelayTime1.Text == "" || tb_DelayTime2.Text == "" || tb_RelieveDelay.Text == "" || tb_Evolume.Text == "" || 
                    cb_LUnit.Text == "" || cb_PUnit.Text == "" || tb_FPtoplimit.Text == "" || tb_FPlowlimit.Text == "" || 
                    tb_BalanPreMax.Text == "" || tb_BalanPreMin.Text == "" || tb_Leaktoplimit.Text == "" || tb_Leaklowlimit.Text == "")
                {
                    MessageBox.Show("输入参数格式不对");
                }
                else
                {
                    // 拆分 float
                    int[] FPtoplimit = Fun_SplitBytes(float.Parse(tb_FPtoplimit.Text)); // [low, high]
                    int[] FPlowlimit = Fun_SplitBytes(float.Parse(tb_FPlowlimit.Text));
                    int[] BalanPreMax = Fun_SplitBytes(float.Parse(tb_BalanPreMax.Text));
                    int[] BalanPreMin = Fun_SplitBytes(float.Parse(tb_BalanPreMin.Text));
                    int[] Leaktoplimit = Fun_SplitBytes(float.Parse(tb_Leaktoplimit.Text));
                    int[] Leaklowlimit = Fun_SplitBytes(float.Parse(tb_Leaklowlimit.Text));

                    int[] Evolume = Fun_SplitBytes(float.Parse(tb_Evolume.Text));

                    // 合并数据（float + int）
                    int[] values = new int[] 
                    {
                        Convert.ToInt32(tb_FullTime.Text) * 10,    // 充气时间
                        Convert.ToInt32(tb_BalanTime.Text) * 10,   // 平衡时间
                        Convert.ToInt32(tb_TestTime1.Text) * 10,   // 检测时间
                        Convert.ToInt32(tb_ExhaustTime.Text) * 10, // 排气时间

                        Convert.ToInt32(tb_RelieveDelay.Text),// 解除2延时
                        Convert.ToInt32(tb_DelayTime1.Text),  // 延时1
                        Convert.ToInt32(tb_DelayTime2.Text),  // 延时2

                        FPtoplimit[0], FPtoplimit[1],    // 充气上限
                        FPlowlimit[0], FPlowlimit[1],    // 充气下限
                        BalanPreMax[0], BalanPreMax[1],  // 平衡上限
                        BalanPreMin[0], BalanPreMin[1],  // 平衡下限
                        Leaktoplimit[0], Leaktoplimit[1],// 泄漏量上限
                        Leaklowlimit[0], Leaklowlimit[1],// 泄漏量下限

                        Evolume[0], Evolume[1],  // 等效容积
                        cb_PUnit.SelectedIndex, // 压力单位
                        cb_LUnit.SelectedIndex, // 泄漏量单位
                    }; // [low, high, 1]

                    switch (cb_MachineNum.SelectedIndex)
                    {
                        case 0:  
                            Form1.f1.LL28CH1client.writeRegisters(1006, values); // 通道1 从1006 开始写入
                            break;
                        case 1:
                            Form1.f1.LL28CH2client.writeRegisters(1006, values); // 通道2 从1006 开始写入
                            break;
                        case 2:
                            Form1.f1.LL28CH3client.writeRegisters(1006, values); // 通道3 从1006 开始写入
                            break;
                        case 3:
                            Form1.f1.LL28CH4client.writeRegisters(1006, values); // 通道4 从1006 开始写入
                            break;
                    }
                }

                bt_BtnUpload.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                mxlLog.Instance.Error($"参数界面上传异常,行号{ex.StackTrace} ", ex);
            }
        }

        // 将 float 拆分为两个（低字在前，高字在后）
        public int[] Fun_SplitBytes(float value)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            ushort lowWord = BitConverter.ToUInt16(bytes, 0);  // 低位（地址1015）
            ushort highWord = BitConverter.ToUInt16(bytes, 2); // 高位（地址1016）
            return new int[] { lowWord, highWord }; // 返回 [low, high]
        }

        //修改蜂鸣器设定，写入0是打开蜂鸣器，写入1是关闭蜂鸣器

        // 读取参数 按钮
        private void BtnRead_Click(object sender, EventArgs e)
        {
            try
            {
                // 通道1
                if(cb_MachineNum.Text == "1")
                {
                    tb_FullTime.Text = (Form1.f1.LL28CH1client.ArrRegister[6] / 10.0).ToString();
                    tb_BalanTime.Text = (Form1.f1.LL28CH1client.ArrRegister[7] / 10.0).ToString();
                    tb_TestTime1.Text = (Form1.f1.LL28CH1client.ArrRegister[8] / 10.0).ToString();
                    tb_ExhaustTime.Text = (Form1.f1.LL28CH1client.ArrRegister[9] / 10.0).ToString();

                    tb_DelayTime1.Text = Form1.f1.LL28CH1client.ArrRegister[11].ToString();
                    tb_DelayTime2.Text = Form1.f1.LL28CH1client.ArrRegister[12].ToString();
                    tb_Evolume.Text = Form1.f1.LL28CH1client.readFloat(25);

                    tb_FPtoplimit.Text = Form1.f1.LL28CH1client.readFloat(13);
                    tb_FPlowlimit.Text = Form1.f1.LL28CH1client.readFloat(15);
                    cb_PUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH1client.ArrRegister[27]);
                    cb_LUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH1client.ArrRegister[28]);

                    tb_BalanPreMax.Text = Form1.f1.LL28CH1client.readFloat(17);
                    tb_BalanPreMin.Text = Form1.f1.LL28CH1client.readFloat(19);
                    tb_Leaktoplimit.Text = Form1.f1.LL28CH1client.readFloat(21);
                    tb_Leaklowlimit.Text = Form1.f1.LL28CH1client.readFloat(23);

                    chk_ChkBee.Checked = Form1.f1.LL28CH1client.ArrCoil[4];
                }

                // 通道2
                if (cb_MachineNum.Text == "2")
                {
                    tb_FullTime.Text = (Form1.f1.LL28CH2client.ArrRegister[6] / 10.0).ToString();
                    tb_BalanTime.Text = (Form1.f1.LL28CH2client.ArrRegister[7] / 10.0).ToString();
                    tb_TestTime1.Text = (Form1.f1.LL28CH2client.ArrRegister[8] / 10.0).ToString();
                    tb_ExhaustTime.Text = (Form1.f1.LL28CH2client.ArrRegister[9] / 10.0).ToString();

                    tb_DelayTime1.Text = Form1.f1.LL28CH2client.ArrRegister[11].ToString();
                    tb_DelayTime2.Text = Form1.f1.LL28CH2client.ArrRegister[12].ToString();
                    tb_Evolume.Text = Form1.f1.LL28CH2client.readFloat(25);

                    tb_FPtoplimit.Text = Form1.f1.LL28CH2client.readFloat(13);
                    tb_FPlowlimit.Text = Form1.f1.LL28CH2client.readFloat(15);
                    cb_PUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH2client.ArrRegister[27]);
                    cb_LUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH2client.ArrRegister[28]);

                    tb_BalanPreMax.Text = Form1.f1.LL28CH2client.readFloat(17);
                    tb_BalanPreMin.Text = Form1.f1.LL28CH2client.readFloat(19);
                    tb_Leaktoplimit.Text = Form1.f1.LL28CH2client.readFloat(21);
                    tb_Leaklowlimit.Text = Form1.f1.LL28CH2client.readFloat(23);

                    chk_ChkBee.Checked = Form1.f1.LL28CH2client.ArrCoil[4];
                }

                // 通道3
                if (cb_MachineNum.Text == "3")
                {
                    tb_FullTime.Text = (Form1.f1.LL28CH3client.ArrRegister[6] / 10.0).ToString();
                    tb_BalanTime.Text = (Form1.f1.LL28CH3client.ArrRegister[7] / 10.0).ToString();
                    tb_TestTime1.Text = (Form1.f1.LL28CH3client.ArrRegister[8] / 10.0).ToString();
                    tb_ExhaustTime.Text = (Form1.f1.LL28CH3client.ArrRegister[9] / 10.0).ToString();

                    tb_DelayTime1.Text = Form1.f1.LL28CH3client.ArrRegister[11].ToString();
                    tb_DelayTime2.Text = Form1.f1.LL28CH3client.ArrRegister[12].ToString();
                    tb_Evolume.Text = Form1.f1.LL28CH3client.readFloat(25);

                    tb_FPtoplimit.Text = Form1.f1.LL28CH3client.readFloat(13);
                    tb_FPlowlimit.Text = Form1.f1.LL28CH3client.readFloat(15);
                    cb_PUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH3client.ArrRegister[27]);
                    cb_LUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH3client.ArrRegister[28]);

                    tb_BalanPreMax.Text = Form1.f1.LL28CH3client.readFloat(17);
                    tb_BalanPreMin.Text = Form1.f1.LL28CH3client.readFloat(19);
                    tb_Leaktoplimit.Text = Form1.f1.LL28CH3client.readFloat(21);
                    tb_Leaklowlimit.Text = Form1.f1.LL28CH3client.readFloat(23);

                    chk_ChkBee.Checked = Form1.f1.LL28CH3client.ArrCoil[4];
                }

                // 通道4
                if (cb_MachineNum.Text == "4")
                {
                    tb_FullTime.Text = (Form1.f1.LL28CH4client.ArrRegister[6] / 10.0).ToString();
                    tb_BalanTime.Text = (Form1.f1.LL28CH4client.ArrRegister[7] / 10.0).ToString();
                    tb_TestTime1.Text = (Form1.f1.LL28CH4client.ArrRegister[8] / 10.0).ToString();
                    tb_ExhaustTime.Text = (Form1.f1.LL28CH4client.ArrRegister[9] / 10.0).ToString();

                    tb_DelayTime1.Text = Form1.f1.LL28CH4client.ArrRegister[11].ToString();
                    tb_DelayTime2.Text = Form1.f1.LL28CH4client.ArrRegister[12].ToString();
                    tb_Evolume.Text = Form1.f1.LL28CH4client.readFloat(25);

                    tb_FPtoplimit.Text = Form1.f1.LL28CH4client.readFloat(13);
                    tb_FPlowlimit.Text = Form1.f1.LL28CH4client.readFloat(15);
                    cb_PUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH4client.ArrRegister[27]);
                    cb_LUnit.SelectedIndex = Convert.ToInt32(Form1.f1.LL28CH4client.ArrRegister[28]);

                    tb_BalanPreMax.Text = Form1.f1.LL28CH4client.readFloat(17);
                    tb_BalanPreMin.Text = Form1.f1.LL28CH4client.readFloat(19);
                    tb_Leaktoplimit.Text = Form1.f1.LL28CH4client.readFloat(21);
                    tb_Leaklowlimit.Text = Form1.f1.LL28CH4client.readFloat(23);

                    chk_ChkBee.Checked = Form1.f1.LL28CH4client.ArrCoil[4];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                mxlLog.Instance.Error($"参数界面读取异常,行号{ex.StackTrace} ", ex);
            }
        }

        // 保存参数 按钮
        private void BtnKeep_Click(object sender, EventArgs e)
        {
            SetParametersJson(cb_MachineNum.Text, cb_ParaNum.Text); // 通道-编号
        }

        // 写入参数  通道-编号
        private void SetParametersJson(string CH, string i)
        {
            var config = new JsonConfig("config.json");
            config.SetValue($"{CH}" + "params_number" + i, cb_ParaNum.Text);
            config.SetValue($"{CH}" + "paraname" + i, tb_ParaName.Text);

            config.SetValue($"{CH}" + "fulltime" + i, tb_FullTime.Text);
            config.SetValue($"{CH}" + "balantime" + i, tb_BalanTime.Text);
            config.SetValue($"{CH}" + "testtime1" + i, tb_TestTime1.Text);
            config.SetValue($"{CH}" + "exhausttime" + i, tb_ExhaustTime.Text);

            config.SetValue($"{CH}" + "delaytime1" + i, tb_DelayTime1.Text);
            config.SetValue($"{CH}" + "delaytime2" + i, tb_DelayTime2.Text);
            config.SetValue($"{CH}" + "relievedelay" + i, tb_RelieveDelay.Text);
            config.SetValue($"{CH}" + "evolume" + i, tb_Evolume.Text);

            config.SetValue($"{CH}" + "fptoplimit" + i, tb_FPtoplimit.Text);
            config.SetValue($"{CH}" + "fplowlimit" + i, tb_FPlowlimit.Text);
            config.SetValue($"{CH}" + "balanpremax" + i, tb_BalanPreMax.Text);
            config.SetValue($"{CH}" + "balanpremin" + i, tb_BalanPreMin.Text);

            config.SetValue($"{CH}" + "leaktoplimit" + i, tb_Leaktoplimit.Text);
            config.SetValue($"{CH}" + "leaklowlimit" + i, tb_Leaklowlimit.Text);
            config.SetValue($"{CH}" + "punit" + i, cb_PUnit.SelectedIndex);
            config.SetValue($"{CH}" + "lunit" + i, cb_LUnit.SelectedIndex);

            config.SetValue($"{CH}" + "bee" + i, chk_ChkBee.Checked);

            //mesconfig.IniWriteValue("Parameters", CH + "unit", CHKUnit.Checked.ToString());
            //mesconfig.IniWriteValue("Parameters", CH + "presscompensation", PressCompensation.Text);
            //MessageBox.Show("仪器编号：" + CH + "保存" + i + "组参数成功！");
        }

        // 读取本地参数记录
        private void ReadParametersJson(string CH, string i)
        {
            var config = new JsonConfig("config.json");
            //ParaNum.Text = config.GetValue<string>($"{CH}" + "params_number" + i, "");
            tb_ParaName.Text = config.GetValue<string>($"{CH}" + "paraname" + i, "0");

            tb_FullTime.Text = config.GetValue<string>($"{CH}" + "fulltime" + i, "0");
            tb_BalanTime.Text = config.GetValue<string>($"{CH}" + "balantime" + i, "0");
            tb_TestTime1.Text = config.GetValue<string>($"{CH}" + "testtime1" + i, "0");
            tb_ExhaustTime.Text = config.GetValue<string>($"{CH}" + "exhausttime" + i, "0");

            tb_DelayTime1.Text = config.GetValue<string>($"{CH}" + "delaytime1" + i, "0");
            tb_DelayTime2.Text = config.GetValue<string>($"{CH}" + "delaytime2" + i, "0");
            tb_RelieveDelay.Text = config.GetValue<string>($"{CH}" + "relievedelay" + i, "0");
            tb_Evolume.Text = config.GetValue<string>($"{CH}" + "evolume" + i, "0");

            tb_FPtoplimit.Text = config.GetValue<string>($"{CH}" + "fptoplimit" + i, "0");
            tb_FPlowlimit.Text = config.GetValue<string>($"{CH}" + "fplowlimit" + i, "0");
            tb_BalanPreMax.Text = config.GetValue<string>($"{CH}" + "balanpremax" + i, "0");
            tb_BalanPreMin.Text = config.GetValue<string>($"{CH}" + "balanpremin" + i, "0");

            tb_Leaktoplimit.Text = config.GetValue<string>($"{CH}" + "leaktoplimit" + i, "0");
            tb_Leaklowlimit.Text = config.GetValue<string>($"{CH}" + "leaklowlimit" + i, "0");
            cb_PUnit.SelectedIndex = config.GetValue<int>($"{CH}" + "punit" + i, 1);
            cb_LUnit.SelectedIndex = config.GetValue<int>($"{CH}" + "lunit" + i, 1);

            chk_ChkBee.Checked = config.GetValue<bool>($"{CH}" + "bee" + i, false);
        }

        private void MachineNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true; // 通道下拉框禁止所有输入
        }

        // 通道下拉框变化
        private void MachineNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReadParametersJson(cb_MachineNum.Text, cb_ParaNum.Text);
        }

        // 编号下拉框变化
        private void ParaNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReadParametersJson(cb_MachineNum.Text, cb_ParaNum.Text);
        }

        // 真空复选框
        private void CH1CHKVacuum_Click(object sender, EventArgs e)
        {
            //if (Form1.f1.plc.PLCIsRun)
            //{
            //    Form1.f1.PLCSignal.Stop();
            //    if (chk_CH1CHKVacuum.Checked)
            //    {
            //        //Form1.f1.plc.CH1VacuumTrue();
            //    }
            //    else
            //    {
            //        //Form1.f1.plc.CH1VacuumFalse();
            //    }
            //    Form1.f1.PLCSignal.Interval = 1000;
            //    Form1.f1.PLCSignal.Start();
            //}
            //else
            //{
            //    bool check = (!chk_CH1CHKVacuum.Checked);
            //    chk_CH1CHKVacuum.Checked = check;
            //    MessageBox.Show("PLC未通讯！");
            //}
        }

        // 真空复选框
        private void CH2CHKVacuum_Click(object sender, EventArgs e)
        {
            //if (Form1.f1.plc.PLCIsRun)
            //{
            //    Form1.f1.PLCSignal.Stop();
            //    if (chk_CH2CHKVacuum.Checked)
            //    {
            //        //Form1.f1.plc.CH2VacuumTrue();
            //    }
            //    else
            //    {
            //        //Form1.f1.plc.CH2VacuumFalse();
            //    }
            //    Form1.f1.PLCSignal.Interval = 1000;
            //    Form1.f1.PLCSignal.Start();
            //}
            //else
            //{
            //    bool check = (!chk_CH2CHKVacuum.Checked);
            //    chk_CH2CHKVacuum.Checked = check;
            //    MessageBox.Show("PLC未通讯！");
            //}
        }

        // 不知道干嘛的，隐藏在下面
        private void SaveConfig_Click(object sender, EventArgs e)
        {
            try
            {
                //Form1.f1.PressMax = Convert.ToDouble(PressMax.Text);
                //Form1.f1.PressMin = Convert.ToDouble(PressMin.Text);
                //Form1.f1.PressingTimeMax = Convert.ToDouble(PressingTimeMax.Text);
                //Form1.f1.PressingTimeMin = Convert.ToDouble(PressingTimeMin.Text);
                //string dialog = Form1.f1.machine;
                //ConfigINI mesconfig = new ConfigINI("Model", dialog);
                //mesconfig.IniWriteValue("Press", "PressMax", PressMax.Text);
                //mesconfig.IniWriteValue("Press", "PressMin", PressMin.Text);
                //mesconfig.IniWriteValue("Press", "PressingTimeMax", PressingTimeMax.Text);
                //mesconfig.IniWriteValue("Press", "PressingTimeMin", PressingTimeMin.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Electricity_FormClosed(object sender, FormClosedEventArgs e)
        {
            Signal_PLC_WriteCoil = null;
        }

        // 条码长度保存按钮
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            Form1.f1.tb_CH1codeLeng.Text = tb_code1len.Text;
            Form1.f1.tb_CH2codeLeng.Text = tb_code2len.Text;
            Form1.f1.tb_CH3codeLeng.Text = tb_code3len.Text;
            Form1.f1.tb_CH4codeLeng.Text = tb_code4len.Text;

            if (Form1.f1.saomaClient1 != null)
                Form1.f1.saomaClient1.codeLength = Convert.ToInt32(tb_code1len.Text);
            if (Form1.f1.saomaClient2 != null)
                Form1.f1.saomaClient2.codeLength = Convert.ToInt32(tb_code2len.Text);
            //if (Form1.f1.saomaClient3 != null)
            //    Form1.f1.saomaClient3.codeLength = Convert.ToInt32(tb_code1len.Text);
            //if (Form1.f1.saomaClient4 != null)
            //    Form1.f1.saomaClient5.codeLength = Convert.ToInt32(tb_code2len.Text);

            // Json记录
            var config = new JsonConfig("config.json");
            config.SetValue("CodeLength1", tb_code1len.Text);
            config.SetValue("CodeLength2", tb_code2len.Text);
            config.SetValue("CodeLength3", tb_code3len.Text);
            config.SetValue("CodeLength4", tb_code4len.Text);

            //MessageBox.Show("设置成功");
        }

        // 正压模式
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
               // Form1.f1.plc.w正压模式();
            }
        }

        // 负压模式
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
           //     Form1.f1.plc.w负压模式();
            }
        }

        // 通道1扫码
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_ch1saoma.Checked)
            {
                Signal_PLC_WriteCoil?.Invoke(600, true);
            }
            else
            {
                Signal_PLC_WriteCoil?.Invoke(600, false);
            }
            var config = new JsonConfig("config.json");
            config.SetValue("chk_ch1saoma", chk_ch1saoma.Checked);
        }

        // 通道2扫码
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_ch2saoma.Checked)
            {
                Signal_PLC_WriteCoil?.Invoke(601, true);
            }
            else
            {
                Signal_PLC_WriteCoil?.Invoke(601, false);
            }
            var config = new JsonConfig("config.json");
            config.SetValue("chk_ch2saoma", chk_ch2saoma.Checked);
        }

        // 通道3扫码
        private void chk_ch3saoma_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_ch3saoma.Checked)
            {
                Signal_PLC_WriteCoil?.Invoke(602, true);
            }
            else
            {
                Signal_PLC_WriteCoil?.Invoke(602, false);
            }
            var config = new JsonConfig("config.json");
            config.SetValue("chk_ch3saoma", chk_ch3saoma.Checked);
        }

        // 通道4扫码
        private void chk_ch4saoma_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_ch4saoma.Checked)
            {
                Signal_PLC_WriteCoil?.Invoke(603, true);
            }
            else
            {
                Signal_PLC_WriteCoil?.Invoke(603, false);
            }
            var config = new JsonConfig("config.json");
            config.SetValue("chk_ch4saoma", chk_ch4saoma.Checked);
        }

        // 单位下拉框
        private void PUnit_TextChanged(object sender, EventArgs e)
        {
            lb_PUnit1.Text = cb_PUnit.Text;
            lb_PUnit2.Text = cb_PUnit.Text;
        }
        private void LUnit_TextChanged(object sender, EventArgs e)
        {
            lb_LUnit1.Text = cb_LUnit.Text;
            lb_LUnit2.Text = cb_LUnit.Text;
        }

        // 蜂鸣器
        private void chk_ChkBee_CheckedChanged(object sender, EventArgs e)
        {
            switch (cb_MachineNum.SelectedIndex)
            {
                case 0:
                    Form1.f1.LL28CH1client.writeCoil("4", chk_ChkBee.Checked); 
                    break;
                case 1:
                    Form1.f1.LL28CH2client.writeCoil("4", chk_ChkBee.Checked);
                    break;
                case 2:
                    Form1.f1.LL28CH3client.writeCoil("4", chk_ChkBee.Checked);
                    break;
                case 3:
                    Form1.f1.LL28CH4client.writeCoil("4", chk_ChkBee.Checked);
                    break;
            }
        }

        // 是否连续测试
        private void chk_ContinueTest_CheckedChanged(object sender, EventArgs e)
        {
            Form1.f1.m_ContinueTest = chk_ContinueTest.Checked;

            tb_ContinueTestCount.Enabled = chk_ContinueTest.Checked;

            var config = new JsonConfig("config.json");
            config.SetValue("chk_ContinueTest", chk_ContinueTest.Checked);
        }

        // 连续次数
        private void tb_ContinueTestCount_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_ContinueTestCount.Text))
                return;

            Form1.f1.m_ContinueTestCount = Convert.ToInt32(tb_ContinueTestCount.Text);

            var config = new JsonConfig("config.json");
            config.SetValue("tb_ContinueTestCount", Convert.ToInt32(tb_ContinueTestCount.Text));
        }
    }
}
