using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using GcDevicePc.Common;

namespace GcDevicePc.ProThread
{
    class HMIControl
    {
        //设备参数
        string strIP = null;
        ushort netPost = 0;
        bool setflag = false;

        GCModbus m_modbus = new GCModbus();

        public HMIControl(string ip, ushort post)
        {
            if (ip != null && post > 0)
            {
                this.strIP = ip;
                this.netPost = post;
                this.setflag = true;
                //Monitorr1112Start();
            }
            else
            {
                setflag = false;
            }
        }

        ~HMIControl()
        {
            this.strIP = null;
            this.netPost = 0;
            this.setflag = false;
        }

        public int WriteSingleVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            //if (this.setflag)
            //    ret = m_modbus.WriteHoldingReg(147, mystate, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(147, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(147, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        public int WriteBatVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            //if (this.setflag)
            //    ret = m_modbus.WriteHoldingReg(148, mystate, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(148, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(148, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        public int WriteSTDBatVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            //if (this.setflag)
            //    ret = m_modbus.WriteHoldingReg(151, mystate, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(151, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(151, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        public int WriteOpenVal()
        {
            int ret = -1;
            ushort mystate = (ushort)0x1;
            //if (this.setflag)
            //    ret = m_modbus.WriteHoldingReg(150, mystate, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(150, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(150, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        /// <summary>
        /// 写入更新指令
        /// </summary>
        public void WriteUpdateVal()
        {
            ushort mystate = (ushort)0x5;
            int ret = 0;
            int count = 0;

            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(156, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    m_modbus.WriteHoldingReg(156, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }
        }

        public int WriteStartVal()
        {
            int ret = -1;
            //if (this.setflag)
            //    ret = m_modbus.WriteSingleCoil(30, (ushort)65280, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteSingleCoil(30, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(30, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        public int WriteStopVal()
        {
            int ret = -1;
            int count = 0;
            if (this.setflag)
            {
                //ret = m_modbus.WriteSingleCoil(31, (ushort)65280, this.strIP, this.netPost);
                //Console.WriteLine("0");

                ret = m_modbus.WriteSingleCoil(31, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(31, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }

            return ret;


        }

        public int WriteEndVal()
        {
            int ret = -1;
            //if (this.setflag)
            //    ret = m_modbus.WriteSingleCoil(33, (ushort)65280, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteSingleCoil(33, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(33, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;     
        }

        public int WriteCancalVal()
        {
            int ret = -1;
            //if (this.setflag)
            //    ret = m_modbus.WriteSingleCoil(32, (ushort)65280, this.strIP, this.netPost);

            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.WriteSingleCoil(32, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(32, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            return ret;
        }

        /// <summary>
        /// 氢空开关测试
        /// </summary>
        /// <param name="val"></param>
        public void HAirSwitch(int val)
        {
            int ret;
            ushort switchaddr = 155;
            ushort writestatue = 0;
            byte[] nowdata = new byte[1];
            int count = 0;
            if (this.setflag)
            {
                switch (val)
                {
                    case 1:
                        switchaddr = 20;
                        break;
                    case 2:
                        switchaddr = 21;
                        break;
                    case 3:
                        switchaddr = 22;
                        break;
                    case 4:
                        switchaddr = 23;
                        break;
                    default:
                        switchaddr = 20;
                        break;
                }

                ret = m_modbus.ReadCoilStatus(switchaddr, 1, this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(switchaddr, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

                if (ret == 0)
                {
                    if (nowdata[0] == 0)
                    {
                        writestatue = (ushort)65280;
                    }
                    else
                    {
                        writestatue = 0;
                    }

                    ret = m_modbus.WriteSingleCoil(switchaddr, writestatue, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(switchaddr, writestatue, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }
                }
                else
                {

                }

            }

        }

        public void AdjustSwitch(int val)
        {
            if (this.setflag)
            {
                switch (val)
                {
                    case 0:
                        adjust_ling();
                        break;
                    case 1:
                        adjust_biao();
                        break;
                    default:
                        break;
                }

            }
            else
            {

            }
        }

        public void adjust_ling()
        {
            int ret;
            ushort writestatue = 0;
            byte[] nowdata = new byte[1];
            int count = 0;
            if (this.setflag)
            {
                ret = m_modbus.ReadCoilStatus(26, 1, this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(26, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

                if (ret == 0)
                {
                    if (nowdata[0] == 0)
                    {
                        writestatue = (ushort)65280;
                    }
                    else
                    {
                        writestatue = 0;
                    }

                    ret = m_modbus.WriteSingleCoil(26, writestatue, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(26, writestatue, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }

                    ret = m_modbus.WriteSingleCoil(27, writestatue, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(27, writestatue, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }
                }
            }
        }

        public void adjust_biao()
        {
            int ret;
            ushort writestatue = 0;
            byte[] nowdata = new byte[1];
            int count = 0;
            if (this.setflag)
            {


                ret = m_modbus.ReadCoilStatus(27, 1, this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(27, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

                if (ret == 0)
                {
                    if (nowdata[0] == 0)
                    {
                        writestatue = (ushort)65280;
                    }
                    else
                    {
                        writestatue = 0;
                    }

                    ret = m_modbus.WriteSingleCoil(27, writestatue, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(27, writestatue, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }

                    ret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                    while (ret != 0)
                    {
                        count++;
                        ret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                        Thread.Sleep(100);
                        if (count == 3)
                            break;
                    }
                }
            }
        }


        public bool Adjust_ling_biao(int type,ushort oc)
        {
            int lret = 0;
            int bret = 0;
            int count = 0;
            if (type == 0)
            {
                bret = m_modbus.WriteSingleCoil(26, oc, this.strIP, this.netPost);
                while (bret != 0)
                {
                    count++;
                    bret = m_modbus.WriteSingleCoil(26, oc, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }

                lret = m_modbus.WriteSingleCoil(27, oc, this.strIP, this.netPost);
                while (lret != 0)
                {
                    count++;
                    lret = m_modbus.WriteSingleCoil(27, oc, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            else if (type == 1)
            {
                bret = m_modbus.WriteSingleCoil(27, oc, this.strIP, this.netPost);
                while (bret != 0)
                {
                    count++;
                    bret = m_modbus.WriteSingleCoil(27, oc, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }

                lret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                while (lret != 0)
                {
                    count++;
                    lret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }

            if (lret == 0 && bret == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
             
        }


        private ushort getrelaystat(ushort index)
        {
            int ret;
            byte[] nowdata = new byte[1];
            int count = 0;

            ret = m_modbus.ReadCoilStatus(index, 1, this.strIP, this.netPost, ref nowdata);
            while (ret != 0)
            {
                count++;
                ret = m_modbus.ReadCoilStatus(index, 1, this.strIP, this.netPost, ref nowdata);
                Thread.Sleep(200);
                if (count == 3)
                    break;
            }

            if (nowdata[0] == 0)
            {
                return 0;
            }
            else
            {
                return (ushort)65280;
            }
        }

        /// <summary>
        /// 点火
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        public void FireUPWork(int local, ushort time1, ushort time2)
        {
            ushort mystate = (ushort)0x1;
            ushort []buf = new ushort[2];
            int ret = 0;
            int count = 0;
            buf[0] = time1;
            buf[1] = time2;
           // buf[2] = mystate;

            ushort fireaddr = 155;

            if (this.setflag)
            {
                switch(local)
                {
                    case 0:
                        fireaddr = 155;
                        break;
                    case 1:
                        fireaddr = 157;
                        break;
                    case 2:
                        fireaddr = 158;
                        break;
                    default:
                        fireaddr = 155;
                        break;
                }

                ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
                count = 0;
                ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost );
                while(ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
        }

        //159
        public void FireUPWork(int local, ushort time1, ushort time2, ushort time3)
        {
            ushort mystate = (ushort)0x1;
            ushort[] buf = new ushort[2];
            int ret = 0;
            int count = 0;
            buf[0] = time1;
            buf[1] = time2;
            //buf[2] = time3;
            // buf[2] = mystate;

            ushort fireaddr = 155;

            if (this.setflag)
            {
                switch (local)
                {
                    case 0:
                        fireaddr = 155;
                        break;
                    case 1:
                        fireaddr = 157;
                        break;
                    case 2:
                        fireaddr = 158;
                        break;
                    default:
                        fireaddr = 155;
                        break;
                }

                ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteMultipleRegs(153, 2, this.strIP, this.netPost, ref buf);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
                count = 0;
                ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(fireaddr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }

                count = 0;
                ret = m_modbus.WriteHoldingReg(159, time3, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(159, time3, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
        }

        public void Sw_RelayAdiust(int type)
        {
            int ret;
            //ushort switchaddr = 19;
            //ushort writestatue = 0;
            byte[] nowdata = new byte[1];
            int count = 0;

            if (type == 0)
            {//零
                ret = m_modbus.WriteSingleCoil(28, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(28, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }

                ret = m_modbus.WriteSingleCoil(29, 0, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(29, 0, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            else if(type == 1)
            {//标
                ret = m_modbus.WriteSingleCoil(29, (ushort)65280, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(29, (ushort)65280, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
                
                ret = m_modbus.WriteSingleCoil(28, 0, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(28, 0, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
            else if(type == 2)
            {//样
                ret = m_modbus.WriteSingleCoil(29, 0, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(29, 0, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }

                ret = m_modbus.WriteSingleCoil(28, 0, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(28, 0, this.strIP, this.netPost);
                    Thread.Sleep(100);
                    if (count == 3)
                        break;
                }
            }
        }


        public bool TCD_Switch(ushort port)
        {
            int ret = 0;
            ushort addr = 0;
            int count = 0;
            byte [] nowdata = new byte[1];
            ushort writestatue = 0;
            if (this.setflag)
            {
                switch (port)
                {
                    case 0:
                        addr = 11;
                        break;
                    case 1:
                        addr = 12;
                        break;
                    case 2:
                        addr = 13;
                        break;
                    default:
                        addr = 11;
                        break;
                }

                ret = m_modbus.ReadCoilStatus(addr,1,this.strIP, this.netPost, ref nowdata);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadCoilStatus(addr, 1, this.strIP, this.netPost, ref nowdata);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
                if(ret == 0 && nowdata[0] == 0)
                {
                    writestatue = (ushort)65280;
                }
                else
                {
                    writestatue = 0;
                    TCD_SetDAC(port, 0);
                }

                count = 0;
                ret = m_modbus.WriteSingleCoil(addr, writestatue, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteSingleCoil(addr, writestatue, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }

               
            }

            return (ret == 0) ? true: false;   
        }

        /// <summary>
        /// 设置TCD1 DAC值
        /// </summary>
        /// <param name="val"></param>
        public bool TCD_SetDAC(ushort port, ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 0;

            if (this.setflag)
            {
                switch (port)
                {
                    case 0:
                        addr = 132;
                        break;
                    case 1:
                        addr = 134;
                        break;
                    case 2:
                        addr = 136;
                        break;
                    default:
                        addr = 132;
                        break;
                }

                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }

            return (ret == 0) ? true : false;   
        }

        public bool SetBatCount(ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 10;

            if (this.setflag)
            {
                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                    Thread.Sleep(200);
                    if (count == 3)
                        break;
                }
            }
            return (ret == 0) ? true : false;
        }


        ManualResetEvent ctrr1112 = new ManualResetEvent(false);

        Thread t_ctrr1112;

        public void Monitorr1112Start()
        {
            if (ctrr1112 != null)
            {
                ctrr1112.Reset();
            }
            t_ctrr1112 = new Thread(Monitor_Ctl_R1112);
            t_ctrr1112.IsBackground = true;
            t_ctrr1112.Start();
        }

        public void DataToHwStop()
        {
            ctrr1112.Set();
        }

        private void Monitor_Ctl_R1112()
        {
            while (!ctrr1112.WaitOne(1000))
            {
                int lret;
                int bret;
                int count = 0;
                switch (CKVocAnalyzer.GlobalCKV.adjustType)
                {
                    case CKVocAnalyzer.GlobalCKV.AdjustType.AT_Zero:
                        if (getrelaystat(26) == 0)
                        {
                            bret = m_modbus.WriteSingleCoil(26, (ushort)65280, this.strIP, this.netPost);
                            while (bret != 0)
                            {
                                count++;
                                bret = m_modbus.WriteSingleCoil(26, (ushort)65280, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }
                        if (getrelaystat(27) == 0)
                        {
                            lret = m_modbus.WriteSingleCoil(27, (ushort)65280, this.strIP, this.netPost);
                            while (lret != 0)
                            {
                                count++;
                                lret = m_modbus.WriteSingleCoil(27, (ushort)65280, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }

                        break;
                    case CKVocAnalyzer.GlobalCKV.AdjustType.AT_Biao:
                        if (getrelaystat(26) != 0)
                        {
                            bret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                            while (bret != 0)
                            {
                                count++;
                                bret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }
                        if (getrelaystat(27) == 0)
                        {
                            lret = m_modbus.WriteSingleCoil(27, (ushort)65280, this.strIP, this.netPost);
                            while (lret != 0)
                            {
                                count++;
                                lret = m_modbus.WriteSingleCoil(27, (ushort)65280, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }
  
                        break;
                    case CKVocAnalyzer.GlobalCKV.AdjustType.AT_Yang:

                        if (getrelaystat(26) != 0)
                        {
                            bret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                            while (bret != 0)
                            {
                                count++;
                                bret = m_modbus.WriteSingleCoil(26, 0, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }

                        if (getrelaystat(27) != 0)
                        {
                            lret = m_modbus.WriteSingleCoil(27, 0, this.strIP, this.netPost);
                            while (lret != 0)
                            {
                                count++;
                                lret = m_modbus.WriteSingleCoil(27, 0, this.strIP, this.netPost);
                                Thread.Sleep(100);
                                if (count == 3)
                                    break;
                            }
                        }



                        break;
                    default:
                        break;
                }
            }

        }

        public void UpdateHmicfg(ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 1200;
            ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
            while (ret != 0)
            {
                count++;
                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                Thread.Sleep(200);
                if (count == 3)
                    break;
            }
        }

        public void UpdateHmiOther(int index, ushort val)
        {
            ushort mystate = val;
            int ret = 0;
            int count = 0;
            ushort addr = 0;

            switch (index)
            {
                case 0:
                    addr = 1201;
                    break;
                case 1:
                    addr = 1202;
                    break;
                default:
                    break;
            }

            ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
            while (ret != 0)
            {
                count++;
                ret = m_modbus.WriteHoldingReg(addr, mystate, this.strIP, this.netPost);
                Thread.Sleep(200);
                if (count == 3)
                    break;
            }
        }
    }
}
