using System;
using System.Threading;
using GcDevicePc.Common;
using System.Runtime.InteropServices;
using GcDevicePc.IniParam;

namespace GcDevicePc.ProThread
{
    public class GcDataTh
    {
        #region 变量
        public struct stTemp
        {
            public ushort usCurTemp_Word1;
            public ushort usCurTemp_Word2;
        }

        [StructLayout(LayoutKind.Explicit, Size = 8)]
        public struct UNION_TEMP
        {
            [FieldOffset(0)]
            public float fCurTemp;

            [FieldOffset(0)]
            public stTemp t;
        }

        //设备参数
        string strIP = null;
        ushort netPost = 0;
        bool setflag = false;

        public int checkflag = 0;

        /// <summary>
        /// Modbus
        /// </summary>
        private static GCModbus m_modbus = new GCModbus();

        //数据线程
        Thread t_GetShowData;
        Thread t_GetChannelData;
        Thread t_GetStatusData;
        //转换数据使用
        UNION_TEMP channel1_value;
        UNION_TEMP channel2_value;
        UNION_TEMP channel3_value;

        UNION_TEMP utemp;

        private UInt32 fDetMax_Time = 0;
        private UInt32 iDetMax_Time = 0;
        private UInt32 bDetMax_Time = 0;

        #endregion

        #region 构造函数和析构函数
        public GcDataTh(string ip, ushort post)
        {
            if (ip != null && post > 0)
            {
                this.strIP = ip;
                this.netPost = post;
                this.setflag = true;
            }
            else
            {
                setflag = false;
            }
        }

        ~GcDataTh()
        {
            this.strIP = null;
            this.netPost = 0;
            this.setflag = false;
        }
        #endregion

        /// <summary>
        /// 读设备温度
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="num">读取数目</param>
        /// <param name="temp">存储单元</param>
        private int GetTemp(ushort address, ushort num, ref float[] temp)
        {
            UInt16[] dest = new UInt16[num];
            int ret = -1;
            ret = m_modbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref dest);
            if (ret == 0)
            {
                for (int i = 0; i < 12; i += 2)
                {
                    utemp.t.usCurTemp_Word1 = dest[i];
                    utemp.t.usCurTemp_Word2 = dest[i + 1];
                    temp[i / 2] = utemp.fCurTemp;
                }

                int no = 6;
                for (int j = 16; j < 22; j++)
                {
                    temp[no] = (dest[j]);
                    no++;
                }
            }
            return ret;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="value"></param>
        /// <param name="len"></param>
        /// <param name="useflag"></param>
        private void BubbleSort(ref UInt32[] arr, ref float[] value, int len, ushort useflag)
        {
            int i = 0;
            int j = 0;
            int k = len - 1;//控制内部比较循环  
            int n = 0;

            //bool isrollback = false;

            for (i = 0; i < len - 1; i++)
            {
                int flag = 1;
                n = 0;

                for (j = 0; j < k; j++)
                {
                    UInt32 tmp = 0;
                    float tmpv = 0.0f;
                    if (arr[j] > arr[j + 1])
                    {
                        tmp = arr[j];
                        tmpv = value[j];

                        arr[j] = arr[j + 1];
                        value[j] = value[j + 1];

                        arr[j + 1] = tmp;
                        value[j + 1] = tmpv;

                        flag = 0;
                        n = j;
                    }
                }
                if (flag == 1)
                    break;
                k = n;
            }

            if (useflag == 0)
            {
                if ((Int32)(arr[0] - fDetMax_Time) < -100000)
                {
                    fDetMax_Time = arr[0];
                }
            }
            else if (useflag == 1)
            {
                if ((Int32)(arr[0] - iDetMax_Time) < -100000)
                {
                    iDetMax_Time = arr[0];
                }
            }
            else if (useflag == 2)
            {
                if ((Int32)(arr[0] - bDetMax_Time) < -100000)
                {
                    bDetMax_Time = arr[0];
                }
            }
        }

        #region 三通道数据获取
        /// <summary>
        /// 获取通道数据
        /// </summary>
        /// <param name="address"></param>
        /// <param name="num"></param>
        private void Get_Channel_Data(ushort address, ushort num)
        {
            int ret;
            UInt32 tmp;
            //int j = 0;

            //Get_Channel1_ValueNum = 0;
            UInt16[] dest = new UInt16[num];
            UInt32[] timev = new UInt32[30];
            float[] fidv = new float[30];

            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref dest);
                if (ret == 0)
                {
                    for (int start = 0, i = 0; start < num;)
                    {
                        tmp = dest[start + 1];
                        tmp <<= 16;
                        tmp = tmp + dest[start];
                        timev[i] = tmp;

                        channel1_value.t.usCurTemp_Word1 = dest[start + 2];
                        channel1_value.t.usCurTemp_Word2 = dest[start + 3];
                        fidv[i] = channel1_value.fCurTemp;
                        start += 4;
                        i++;
                    }

                    BubbleSort(ref timev, ref fidv, num / 4, 0);

                    {
                        for (int i = 0; i < num / 4; i++)
                        {
                            tmp = timev[i];



                            if (tmp > fDetMax_Time)
                            {

                                fDetMax_Time = tmp;
                                globaldata.m_signalbuffer.Channel1_Port.ctimer = tmp;
                                globaldata.m_signalbuffer.Channel1_Port.cdata = fidv[i];
                                globaldata.m_dpbuffer.ShowList.showDet.fDetValue = fidv[i] * 1000.0f;
#if (data)
                    string strsys = (tmp.ToString() + "," + fidv[i].ToString());
                    Trace.WriteLine(strsys, "data");
#endif
                                //j++;
                                //Get_Channel1_ValueNum++;
                                globaldata.m_signalbuffer.Draw_Port.Add(globaldata.m_signalbuffer.Channel1_Port);
                                globaldata.m_signalbuffer.Save_Port.Add(globaldata.m_signalbuffer.Channel1_Port);

                                if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtohw)
                                {
                                    lock (globaldata.channelA_A)
                                    {
                                        globaldata.channelA_A.Add((long)(fidv[i] * 1000000));
                                    }
                                    lock (globaldata.channelB_A)
                                    {
                                        globaldata.channelB_A.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                                else if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtoZb)
                                {
                                    lock (globaldata.channelA_A)
                                    {
                                        globaldata.channelA_A.Add((long)(fidv[i] * 1000000));
                                    }
                                    lock (globaldata.channelB_A)
                                    {
                                        globaldata.channelB_A.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void Get_Channel2_Data(ushort address, ushort num)
        {
            UInt32 tmp;
            int ret;
            System.UInt16[] dest = new System.UInt16[num];
            UInt32[] timev = new UInt32[30];
            float[] fidv = new float[30];

            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref dest);

                if (ret == 0)
                {
                    for (int start = 0, i = 0; start < num;)
                    {
                        tmp = dest[start + 1];
                        tmp <<= 16;
                        tmp = tmp + dest[start];
                        timev[i] = tmp;

                        channel2_value.t.usCurTemp_Word1 = dest[start + 2];
                        channel2_value.t.usCurTemp_Word2 = dest[start + 3];
                        fidv[i] = channel2_value.fCurTemp;
                        start += 4;
                        i++;
                    }

                    BubbleSort(ref timev, ref fidv, num / 4, 1);

                    {
                        for (int i = 0; i < num / 4; i++)
                        {
                            tmp = timev[i];

                            if (tmp > iDetMax_Time)
                            {
                                iDetMax_Time = tmp;
                                globaldata.m_signalbuffer.Channel2_Port.ctimer = tmp;
                                globaldata.m_signalbuffer.Channel2_Port.cdata = fidv[i];
                                globaldata.m_dpbuffer.ShowList.showDet.iDetValue = fidv[i] * 1000.0f;

                                globaldata.m_signalbuffer.Draw_Port2.Add(globaldata.m_signalbuffer.Channel2_Port);
                                globaldata.m_signalbuffer.Save_Port2.Add(globaldata.m_signalbuffer.Channel2_Port);

                                if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtohw)
                                {
                                    lock (globaldata.channelA_B)
                                    {
                                        globaldata.channelA_B.Add((long)(fidv[i] * 1000000));
                                    }

                                    lock (globaldata.channelB_B)
                                    {
                                        globaldata.channelB_B.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                                else if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtoZb)
                                {
                                    lock (globaldata.channelA_B)
                                    {
                                        globaldata.channelA_B.Add((long)(fidv[i] * 1000000));
                                    }

                                    lock (globaldata.channelB_B)
                                    {
                                        globaldata.channelB_B.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

        private void Get_Channel3_Data(ushort address, ushort num)
        {
            UInt32 tmp;
            int ret;
            UInt16[] dest = new UInt16[num];
            UInt32[] timev = new UInt32[30];
            float[] fidv = new float[30];


            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref dest);
                if (ret == 0)
                {
                    for (int start = 0, i = 0; start < num;)
                    {
                        tmp = dest[start + 1];
                        tmp <<= 16;
                        tmp = tmp + dest[start];
                        timev[i] = tmp;

                        channel3_value.t.usCurTemp_Word1 = dest[start + 2];
                        channel3_value.t.usCurTemp_Word2 = dest[start + 3];
                        fidv[i] = channel3_value.fCurTemp;
                        start += 4;
                        i++;
                    }

                    BubbleSort(ref timev, ref fidv, num / 4, 2);

                    {
                        for (int i = 0; i < num / 4; i++)
                        {
                            tmp = timev[i];

                            if (tmp > bDetMax_Time)
                            {
                                bDetMax_Time = tmp;
                                globaldata.m_signalbuffer.Channel3_Port.ctimer = tmp;
                                globaldata.m_signalbuffer.Channel3_Port.cdata = fidv[i];
                                globaldata.m_dpbuffer.ShowList.showDet.bDetValue = fidv[i] * 1000.0f;
                                globaldata.m_signalbuffer.Draw_Port3.Add(globaldata.m_signalbuffer.Channel3_Port);
                                globaldata.m_signalbuffer.Save_Port3.Add(globaldata.m_signalbuffer.Channel3_Port);

                                if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtohw)
                                {
                                    lock (globaldata.channelA_C)
                                    {
                                        globaldata.channelA_C.Add((long)(fidv[i] * 1000000));
                                    }
                                    lock (globaldata.channelB_C)
                                    {
                                        globaldata.channelB_C.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                                else if(globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.sendtoZb)
                                {
                                    lock (globaldata.channelA_C)
                                    {
                                        globaldata.channelA_C.Add((long)(fidv[i] * 1000000));
                                    }
                                    lock (globaldata.channelB_C)
                                    {
                                        globaldata.channelB_C.Add((long)(fidv[i] * 1000000));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region 底层通讯接口
        /// <summary>
        /// 获取线圈状态
        /// </summary>
        /// <param name="address"></param>
        /// <param name="num"></param>
        /// <param name="temp"></param>
        private int GetCState(ushort address, ushort num, ref byte[] temp)
        {
            int ret = -1;
            if (this.setflag)
                ret = m_modbus.ReadCoilStatus(address, num, this.strIP, this.netPost, ref temp);
            return ret;
        }

        /// <summary>
        /// 获取状态
        /// </summary>
        /// <param name="address"></param>
        /// <param name="num"></param>
        /// <param name="state"></param>
        public int GetState(ushort address, ushort num, ref UInt16[] state)
        {
            int ret = -1;
            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref state);

            }
            return ret;
        }

        private int GetHmiAbnormalHoldingReg(ushort address, ushort num, ref UInt16[] state)
        {
            int ret = -1;
            int count = 0;
            if (setflag)
            {
                ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, netPost, ref state);
                while (ret != 0)
                {
                    count++;
                    if (count > 3)
                        break;
                    ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, netPost, ref state);
                }
            }
            return ret;
        }

        #endregion


        ///////////////////////////////////////////////////////////
        /// <summary>
        /// 获取设备相关温度
        /// </summary>
        /// <param name="temp"></param>
        private bool GetGCTemp(ref float[] temp)
        {
            int ret = -1;
            ret = GetTemp(globaldata.tempstart, 24, ref temp);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 获取程升过程状态
        /// </summary>
        /// <param name="state"></param>
        public void GetProUpState(ref ushort[] state)
        {
            GetState(globaldata.statusstart, 6, ref state);
        }

        private int LoseNum = 0;
        /// <summary>
        /// 获取HMI运行状态
        /// </summary>
        /// <param name="state"></param>
        private bool GetHMIState(ref ushort[] state)
        {
            int ret = 0;
            ret = GetState(globaldata.hmistatus, 9, ref state);

            if (ret != 0)
            {
                LoseNum++;
                if (LoseNum > 5)
                {
                    globaldata.m_hmibuffer.gcinfo.hmioffline = true;
                }
            }
            else
            {
                globaldata.m_hmibuffer.gcinfo.hmioffline = false;
                LoseNum = 0;
            }

            return ret == 0 ? true : false;
        }

        public int GetHmiAbnormal(ref ushort[] data)
        {
            int ret = -1;
            try
            {
                ret = GetHmiAbnormalHoldingReg(1101, 3, ref data);
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
            return ret;
        }

        public int GetFireUpTime(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(153, 2, ref state);
            return ret;
        }

        public int GetFristFireUpTime(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(159, 1, ref state);
            return ret;
        }

        public int GetOtherset(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(1201, 2, ref state);
            return ret;
        }

        /// <summary>
        /// 获取各个固件版本
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetFireWareVersion(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(1000, 12, ref state);
            return ret;
        }

        /// <summary>
        /// 获取阀状态
        /// </summary>
        /// <param name="state"></param>
        private bool GetValveState(ref byte[] state)
        {
            int ret = -1;
            ret = GetCState(globaldata.valusstatus, 4, ref state);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 获取错误状态
        /// </summary>
        /// <param name="state"></param>
        private bool GetErrorState(ref byte[] state)
        {
            int ret = -1;
            ret = GetCState(globaldata.errortatus, 9, ref state);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 点火状态
        /// </summary>
        /// <param name="state"></param>
        private bool GetFireState(ref byte[] state)
        {
            int ret = -1;
            ret = GetCState(globaldata.detstatus, 3, ref state);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// TCD SW
        /// </summary>
        /// <param name="state"></param>
        private bool GetTCDSWState(ref byte[] state)
        {
            int ret = -1;
            ret = GetCState(11, 3, ref state);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 点火状态2
        /// </summary>
        /// <param name="state"></param>
        private bool GetFireState2(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(155, 4, ref state);
            return ret == 0 ? true : false;
        }

        public bool GetDACValState(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(132, 6, ref state);
            return ret == 0 ? true : false;
        }

        public bool GetEPCVal(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(600, 36, ref state);
            return ret == 0 ? true : false;
        }

        public bool GetEPC1(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(600, 6, ref state);
            return ret == 0 ? true : false;
        }

        public bool GetEPC2(ref ushort[] state)
        {
            int ret = -1;
            ret = GetState(612, 6, ref state);
            return ret == 0 ? true : false;
        }
        /// <summary>
        /// 获取10路继电器
        /// </summary>
        /// <param name="state"></param>
        private bool GetALLSWState(ref byte[] state)
        {
            int ret = -1;
            ret = GetCState(16, 12, ref state);
            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 获取链路数据
        /// </summary>
        private void get_channel_data()
        {
            if (CProfileDevice.m_DevParam.syspara.u16DetFront == 1)
            {
                Get_Channel_Data(160, 120);
            }

            if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
            {
                Get_Channel2_Data(280, 120);
            }

            if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
            {
                Get_Channel3_Data(400, 120);
            }
        }

        /// <summary>
        /// 获取方法开始时间
        /// </summary>
        /// <returns></returns>
        public UInt32 Get_Method_StartTime()
        {
            UInt32 time = 0;
            UInt32 tmp = 0;
            int ret = 0;
            int count = 0;
            UInt16[] dest = new UInt16[2];

            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(88, 2, this.strIP, this.netPost, ref dest);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadHoldingReg(88, 2, this.strIP, this.netPost, ref dest);
                    Thread.Sleep(50);
                    if (count == 3)
                        break;
                }

                if (ret == 0)
                {
                    tmp = dest[1];
                    tmp <<= 16;
                    tmp = tmp + dest[0];
                    time = tmp;
                }
                else
                {
                    time = 0;
                }
            }
            return time;
        }

        /// <summary>
        /// 获取方法结束时间
        /// </summary>
        /// <returns></returns>
        public UInt32 Get_Method_EndTime()
        {
            UInt32 time = 0;
            UInt32 tmp = 0;
            int ret = 0;
            int count = 0;
            System.UInt16[] dest = new System.UInt16[2];

            if (this.setflag)
            {
                ret = m_modbus.ReadHoldingReg(90, 2, this.strIP, this.netPost, ref dest);
                while (ret != 0)
                {
                    count++;
                    ret = m_modbus.ReadHoldingReg(90, 2, this.strIP, this.netPost, ref dest);
                    Thread.Sleep(50);
                    if (count == 3)
                        break;
                }

                if (ret == 0)
                {
                    tmp = dest[1];
                    tmp <<= 16;
                    tmp = tmp + dest[0];
                    time = tmp;
                }
                else
                {
                    time = 0;
                }
            }
            return time;
        }


        /// <summary>
        /// 更新显示数据
        /// </summary>
        public void update_showdata()
        {
            float[] gctemp = new float[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            byte[] valvestate = new byte[4];
            byte[] detstate = new byte[3];
            byte[] errstate = new byte[2] { 0, 0 };
            byte[] tenstate = new byte[2] { 0, 0 };
            ushort[] firestatus = new ushort[4] { 0, 0, 0, 0 };
            ushort[] dacstatus = new ushort[6] { 0, 0, 0, 0, 0, 0 };
            ushort[] hmiabnormal = new ushort[3];
            ushort[] hmipower = new ushort[6] { 0,0,0,0,0,0};

            //ushort[] epcdata = new ushort[36];
            ushort[] epc1 = new ushort[6];
            ushort[] epc2 = new ushort[6];

            if (GetGCTemp(ref gctemp))
            {
                globaldata.m_dpbuffer.ShowList.showtemp.FPActualTemp = gctemp[0];
                globaldata.m_dpbuffer.ShowList.showtemp.BPActualTemp = gctemp[4];
                globaldata.m_dpbuffer.ShowList.showtemp.FPSetTemp = gctemp[6];
                globaldata.m_dpbuffer.ShowList.showtemp.BPSetTemp = gctemp[10];
                globaldata.m_dpbuffer.ShowList.showtemp.ColActualTemp = gctemp[3];
                globaldata.m_dpbuffer.ShowList.showtemp.ColSetTemp = gctemp[9];
                if (CProfileDevice.m_DevParam.syspara.u16DetFront == 1)
                {
                    globaldata.m_dpbuffer.ShowList.showtemp.fDetActualTemp = gctemp[1];
                    globaldata.m_dpbuffer.ShowList.showtemp.fDetSetTemp = gctemp[7];
                }
                if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                {
                    globaldata.m_dpbuffer.ShowList.showtemp.iDetActualTemp = gctemp[2];
                    globaldata.m_dpbuffer.ShowList.showtemp.iDetSetTemp = gctemp[8];
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                {
                    globaldata.m_dpbuffer.ShowList.showtemp.bDetActualTemp = gctemp[5];
                    globaldata.m_dpbuffer.ShowList.showtemp.bDetSetTemp = gctemp[11];
                }

                if (CProfileDevice.m_DevParam.syspara.u16AuxHeaterNum > 0)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16AuxHeater1 == 1)  //TCD
                    {
                        globaldata.m_dpbuffer.ShowList.showtemp.AuxActualTemp1 = gctemp[2];
                        globaldata.m_dpbuffer.ShowList.showtemp.AuxSetTemp1 = gctemp[8];
                    }

                    if (CProfileDevice.m_DevParam.syspara.u16AuxHeater2 == 1)
                    {
                        globaldata.m_dpbuffer.ShowList.showtemp.AuxActualTemp2 = gctemp[5];
                        globaldata.m_dpbuffer.ShowList.showtemp.AuxSetTemp2 = gctemp[11];
                    }
                }
            }

            if (GetValveState(ref valvestate))
            {
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveOne = (((valvestate[0] >> 0) & 0x1) == 1) ? true : false;
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveTwo = (((valvestate[0] >> 1) & 0x1) == 1) ? true : false;
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveThree = (((valvestate[0] >> 2) & 0x1) == 1) ? true : false;
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveFour = (((valvestate[0] >> 3) & 0x1) == 1) ? true : false;
            }

            if (GetALLSWState(ref tenstate))
            {
                for (int i = 0; i < 12; i++)
                {
                    if (i < 8)
                        globaldata.m_hmibuffer.gcinfo.TenRelay[i] = (byte)((((tenstate[0] >> i) & 0x1) == 1) ? 1 : 0);
                    else
                        globaldata.m_hmibuffer.gcinfo.TenRelay[i] = (byte)((((tenstate[1] >> (i - 8)) & 0x1) == 1) ? 1 : 0);
                }
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveFive = (((tenstate[1] >> 1) & 0x1) == 1) ? true : false;
                globaldata.m_dpbuffer.ShowList.showValve.u16ValveSix = (((tenstate[1] >> 0) & 0x1) == 1) ? true : false;
            }


            if (GetFireState(ref detstate))
            {
                if (CProfileDevice.m_DevParam.syspara.u16DetFrontType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.fDetSwitch = ((((detstate[0] >> 0) & 0x1) == 1) ? true : false);
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetInterType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.iDetSwitch = ((((detstate[0] >> 0) & 0x1) == 1) ? true : false);
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetBehindType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.bDetSwitch = ((((detstate[0] >> 0) & 0x1) == 1) ? true : false);
                }
            }

            if (GetFireState2(ref firestatus))
            {
                for (int i = 0; i < 4; i++)
                {
                    globaldata.m_hmibuffer.gcinfo.FireStatus[i] = firestatus[i];
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetFrontType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.fDetStatue = globaldata.m_hmibuffer.gcinfo.FireStatus[0];
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetInterType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.iDetStatue = globaldata.m_hmibuffer.gcinfo.FireStatus[2];
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetBehindType != 0)
                {
                    globaldata.m_dpbuffer.ShowList.showDet.bDetStatue = globaldata.m_hmibuffer.gcinfo.FireStatus[3];
                }
            }

            if (GetTCDSWState(ref detstate))
            {
                if (CProfileDevice.m_DevParam.syspara.u16DetFront == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 0)
                    {
                        globaldata.m_dpbuffer.ShowList.showDet.fDetSwitch = ((((detstate[0] >> 0) & 0x1) == 1) ? true : false);

                    }
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 0)
                    {
                        globaldata.m_dpbuffer.ShowList.showDet.iDetSwitch = ((((detstate[0] >> 1) & 0x1) == 1) ? true : false);

                    }
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 0)
                    {
                        globaldata.m_dpbuffer.ShowList.showDet.bDetSwitch = ((((detstate[0] >> 2) & 0x1) == 1) ? true : false);

                    }
                }
            }


            if (GetDACValState(ref dacstatus))
            {
                for (int i = 0; i < 6; i++)
                {
                    globaldata.m_hmibuffer.gcinfo.DACStatus[i] = dacstatus[i];
                }
                if (CProfileDevice.m_DevParam.syspara.u16DetFront == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 0)
                    {

                        globaldata.m_dpbuffer.ShowList.showDet.fDetStatue = globaldata.m_hmibuffer.gcinfo.DACStatus[0];
                    }
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 0)
                    {

                        globaldata.m_dpbuffer.ShowList.showDet.iDetStatue = globaldata.m_hmibuffer.gcinfo.DACStatus[2];
                    }
                }

                if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                {
                    if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 0)
                    {

                        globaldata.m_dpbuffer.ShowList.showDet.bDetStatue = globaldata.m_hmibuffer.gcinfo.DACStatus[4];
                    }
                }
            }

            if (GetErrorState(ref errstate))
            {
                for (int i = 0; i < 9; i++)
                {
                    globaldata.m_hmibuffer.gcinfo.ErrorStatus[i] = (byte)((((errstate[0] >> i) & 0x1) == 1) ? 1 : 0);

                }
            }

            if (GetEPC1(ref epc1))
            {
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_1_Cur = (epc1[0] > 64000 ? (ushort)0 : epc1[0]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_2_Cur = (epc1[1] > 64000 ? (ushort)0 : epc1[1]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_3_Cur = (epc1[2] > 64000 ? (ushort)0 : epc1[2]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_1_Set = (epc1[3] > 64000 ? (ushort)0 : epc1[3]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_2_Set = (epc1[4] > 64000 ? (ushort)0 : epc1[4]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC1_3_Set = (epc1[5] > 64000 ? (ushort)0 : epc1[5]);

                #region
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel1_Cur = (epcdata[0] > 64000 ? (ushort)0 : epcdata[0]);
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel2_Cur = (epcdata[1] > 64000 ? (ushort)0 : epcdata[1]);
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel3_Cur = (epcdata[2] > 64000 ? (ushort)0 : epcdata[2]);
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel1_Set = (epcdata[3] > 64000 ? (ushort)0 : epcdata[3]);
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel2_Set = (epcdata[4] > 64000 ? (ushort)0 : epcdata[4]);
                //globaldata.m_dpbuffer.ShowList.showepc.FSChannel3_Set = (epcdata[5] > 64000 ? (ushort)0 : epcdata[5]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel1_Cur = (epcdata[6] > 64000 ? (ushort)0 : epcdata[6]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel2_Cur = (epcdata[7] > 64000 ? (ushort)0 : epcdata[7]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel3_Cur = (epcdata[8] > 64000 ? (ushort)0 : epcdata[8]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel1_Set = (epcdata[9] > 64000 ? (ushort)0 : epcdata[9]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel2_Set = (epcdata[10] > 64000 ? (ushort)0 : epcdata[10]);
                //globaldata.m_dpbuffer.ShowList.showepc.FDChannel3_Set = (epcdata[11] > 64000 ? (ushort)0 : epcdata[11]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel1_Cur = (epcdata[12] > 64000 ? (ushort)0 : epcdata[12]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel2_Cur = (epcdata[13] > 64000 ? (ushort)0 : epcdata[13]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel3_Cur = (epcdata[14] > 64000 ? (ushort)0 : epcdata[14]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel1_Set = (epcdata[15] > 64000 ? (ushort)0 : epcdata[15]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel2_Set = (epcdata[16] > 64000 ? (ushort)0 : epcdata[16]);
                //globaldata.m_dpbuffer.ShowList.showepc.ISChannel3_Set = (epcdata[17] > 64000 ? (ushort)0 : epcdata[17]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel1_Cur = (epcdata[18] > 64000 ? (ushort)0 : epcdata[18]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel2_Cur = (epcdata[19] > 64000 ? (ushort)0 : epcdata[19]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel3_Cur = (epcdata[20] > 64000 ? (ushort)0 : epcdata[20]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel1_Set = (epcdata[21] > 64000 ? (ushort)0 : epcdata[21]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel2_Set = (epcdata[22] > 64000 ? (ushort)0 : epcdata[22]);
                //globaldata.m_dpbuffer.ShowList.showepc.IDChannel3_Set = (epcdata[23] > 64000 ? (ushort)0 : epcdata[23]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel1_Cur = (epcdata[24] > 64000 ? (ushort)0 : epcdata[24]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel2_Cur = (epcdata[25] > 64000 ? (ushort)0 : epcdata[25]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel3_Cur = (epcdata[26] > 64000 ? (ushort)0 : epcdata[26]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel1_Set = (epcdata[27] > 64000 ? (ushort)0 : epcdata[27]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel2_Set = (epcdata[28] > 64000 ? (ushort)0 : epcdata[28]);
                //globaldata.m_dpbuffer.ShowList.showepc.BSChannel3_Set = (epcdata[29] > 64000 ? (ushort)0 : epcdata[29]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel1_Cur = (epcdata[30] > 64000 ? (ushort)0 : epcdata[30]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel2_Cur = (epcdata[31] > 64000 ? (ushort)0 : epcdata[31]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel3_Cur = (epcdata[32] > 64000 ? (ushort)0 : epcdata[32]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel1_Set = (epcdata[33] > 64000 ? (ushort)0 : epcdata[33]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel2_Set = (epcdata[34] > 64000 ? (ushort)0 : epcdata[34]);
                //globaldata.m_dpbuffer.ShowList.showepc.BDChannel3_Set = (epcdata[35] > 64000 ? (ushort)0 : epcdata[35]);
                #endregion
            }
            if (GetEPC2(ref epc2))
            {
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_1_Cur = (epc2[0] > 64000 ? (ushort)0 : epc2[0]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_2_Cur = (epc2[1] > 64000 ? (ushort)0 : epc2[1]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_3_Cur = (epc2[2] > 64000 ? (ushort)0 : epc2[2]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_1_Set = (epc2[3] > 64000 ? (ushort)0 : epc2[3]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_2_Set = (epc2[4] > 64000 ? (ushort)0 : epc2[4]);
                globaldata.m_dpbuffer.ShowList.showepc.EPC2_3_Set = (epc2[5] > 64000 ? (ushort)0 : epc2[5]);
            }

            if (GetHmiAbnormal(ref hmiabnormal) == 0)
            {
                globaldata.m_hmibuffer.gcinfo.HmiAbnormal = hmiabnormal;
            }

            if (GetHmiPower(ref hmipower) == 0)
            {
                globaldata.m_dpbuffer.ShowList.pwmInfo.fDPower = hmipower[0];
                globaldata.m_dpbuffer.ShowList.pwmInfo.iDPower = hmipower[1];
                globaldata.m_dpbuffer.ShowList.pwmInfo.bDPower = hmipower[2];
                globaldata.m_dpbuffer.ShowList.pwmInfo.fSPower = hmipower[3];
                globaldata.m_dpbuffer.ShowList.pwmInfo.fSPower = hmipower[4];
                globaldata.m_dpbuffer.ShowList.pwmInfo.CbPower = hmipower[5];
            }
        }

        private int GetHmiPower(ref ushort[] hmipower)
        {
            int ret = -1;
            try
            {
                ret = GetHmiPowerHoldingReg(1150, 6, ref hmipower);
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
            return ret;
        }

        private int GetHmiPowerHoldingReg(ushort address, ushort num, ref ushort[] state)
        {
            int ret = -1;
            int count = 0;
            if (setflag)
            {
                ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, netPost, ref state);
                while (ret != 0)
                {
                    count++;
                    if (count > 3)
                        break;
                    ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, netPost, ref state);
                }
            }
            return ret;
        }


        /// <summary>
        /// 更新HMI状态数据
        /// </summary>
        private void update_hmidata()
        {
            bool ret = false;
            UInt16[] hmistate = new UInt16[9];
            UInt16[] epcdata = new UInt16[16];

            ret = GetHMIState(ref hmistate);

            if (ret)
            {
                globaldata.m_hmibuffer.gcinfo.batno = hmistate[0];
                globaldata.m_hmibuffer.gcinfo.batruncount = hmistate[1];
                globaldata.m_hmibuffer.gcinfo.oneruntime = hmistate[2];
                globaldata.m_hmibuffer.gcinfo.singlerun = hmistate[3];
                globaldata.m_hmibuffer.gcinfo.batrun = hmistate[4];
                globaldata.m_hmibuffer.gcinfo.methodstatus = hmistate[5];
                globaldata.m_hmibuffer.gcinfo.waitstart = hmistate[6];
                globaldata.m_hmibuffer.gcinfo.hmistatus = hmistate[7];
                globaldata.m_hmibuffer.gcinfo.runtime = hmistate[8];

                //额外设置
                globaldata.m_dpbuffer.ShowList.showtime.runtimenow = hmistate[2];
                globaldata.m_dpbuffer.ShowList.showtime.alltime = hmistate[8];

                //获取开始时间
                if (globaldata.m_hmibuffer.gcinfo.methodstatus == 4 || globaldata.m_hmibuffer.gcinfo.methodstatus == 5)
                {
                    globaldata.m_hmibuffer.gcinfo.NowStartTime = Get_Method_StartTime();
                }

                //获取结束时间
                if (globaldata.m_hmibuffer.gcinfo.methodstatus <= 3 || globaldata.m_hmibuffer.gcinfo.methodstatus > 5)
                {
                    globaldata.m_hmibuffer.gcinfo.NowEndTime = Get_Method_EndTime();
                }
            }

            ret = GetEPCStatusData(ref epcdata);
            if (ret)
            {
                globaldata.m_hmibuffer.gcinfo.EPCStatus = epcdata;
            }
        }

        private int GetEPCHoldingReg(ushort address, ushort num, ref UInt16[] state)
        {
            int ret = -1;
            int count = 0;
            if (setflag)
            {
                ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref state);
                while (ret != 0)
                {
                    count++;
                    if (count > 3)
                        break;
                    ret = globaldata.global_moudbus.ReadHoldingReg(address, num, this.strIP, this.netPost, ref state);
                }
            }
            return ret;
        }

        public bool GetEPCStatusData(ref ushort[] data)
        {
            int ret = -1;
            try
            {
                ret = GetEPCHoldingReg(700, 16, ref data);
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
            return ret == 0;
        }

        /// <summary>
        /// 更新通道采集数据
        /// </summary>
        public void update_channeldata()
        {
            get_channel_data();
        }

        #region  显示数据更新
        ManualResetEvent showmre = new ManualResetEvent(false);
        public void ShowDataStart()
        {
            if (showmre != null)
            {
                showmre.Reset();
            }
            t_GetShowData = new Thread(GetShowDataProc);
            t_GetShowData.IsBackground = true;
            t_GetShowData.Start();
        }

        public void ShowDataStop()
        {
            showmre.Set();
        }

        private void GetShowDataProc()
        {
            while (!showmre.WaitOne(500))
            {
                update_showdata();
                Thread.Sleep(500);
            }
        }
        #endregion


        #region HMI状态通道状态
        ManualResetEvent channelmre = new ManualResetEvent(false);
        public void ChannelDataStart()
        {
            if (channelmre != null)
            {
                channelmre.Reset();
            }
            t_GetChannelData = new Thread(GetChannelDataProc);
            t_GetChannelData.IsBackground = true;
            t_GetChannelData.Start();

        }

        public void ChannelDataStop()
        {
            channelmre.Set();
        }

        private void GetChannelDataProc()
        {
            while (!channelmre.WaitOne(500))
            {
                lock (globaldata.m_signalbuffer.Draw_Port)
                {
                    update_channeldata();
                }
            }
        }
        #endregion


        #region HMI状态采集线程
        ManualResetEvent hmimre = new ManualResetEvent(false);
        public void HmiStatueStart()
        {
            if (hmimre != null)
            {
                hmimre.Reset();
            }
            t_GetStatusData = new Thread(HmiStatueProc);
            t_GetStatusData.IsBackground = true;
            t_GetStatusData.Start();
        }

        public void HmiStatueStop()
        {
            hmimre.Set();
        }

        private void HmiStatueProc()
        {
            while (!hmimre.WaitOne(500))
            {
                update_hmidata();
            }
        }
        #endregion


    }
}
