using GcDevicePc.IniParam;
using Modbus.Data;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GcDevicePc.Common
{
    class NModbusSlave
    {
       private ModbusSlave slave;

        public NModbusSlave()
        {
            listener = new TcpListener(IPAddress.Any, 502);// 端口根据需要确定
            listener.Start();

            slave = ModbusTcpSlave.CreateTcp(1, listener);

            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

            slave.DataStore.DataStoreWrittenTo += new EventHandler<DataStoreEventArgs>(Modbus_DataStoreWriteTo);

            slave.ModbusSlaveRequestReceived += new EventHandler<ModbusSlaveRequestEventArgs>(Modbus_Request_Event);

            slave.DataStore.DataStoreReadFrom += new EventHandler<DataStoreEventArgs>(DataStore_DataStoreReadFrom);

            slave.DataStore.DataStoreWrittenTo += new EventHandler<DataStoreEventArgs>(DataStore_DataStoreWrittenTo);

            slave.Listen();

        }

        public void NModbusSlaveClose()
        {

        }

        ~NModbusSlave()
        {
            listener.Stop();
            slave.Dispose();
        }

        private readonly TcpListener listener;
        // 网络流列表
        //private List<NetworkStream> streams = new List<NetworkStream>();

        private void DataStore_DataStoreWrittenTo(object sender, DataStoreEventArgs e)
        {
        }

        private void DataStore_DataStoreReadFrom(object sender, DataStoreEventArgs e)
        {
            
            try
            {

                switch (e.ModbusDataType)
                {
                    case ModbusDataType.HoldingRegister:
                        for (int i = 0; i < e.Data.B.Count; i++)
                        {
                            
                            switch (globaldata.Foreign)
                            {
                                case 0:
                                    slave.DataStore.HoldingRegisters[e.StartAddress + i + 1] = slavereadhreg(Convert.ToUInt16(e.StartAddress + i + 1));
                                    break;
                                case 1:
                                    slave.DataStore.HoldingRegisters[e.StartAddress + i + 1] = newslavereadhreg1(Convert.ToUInt16(e.StartAddress + i + 1));
                                    break;
                                default:
                                    //slave.DataStore.HoldingRegisters[e.StartAddress + i + 1] = slavereadhreg(Convert.ToUInt16(e.StartAddress + i + 1));
                                    break;
                            }
                        }
                        break;
                    case ModbusDataType.Coil:
                        for (int i = 0; i < e.Data.A.Count; i++)
                        {
                            switch (globaldata.Foreign)
                            {
                                case 0:
                                    slave.DataStore.CoilDiscretes[e.StartAddress + i + 1] = slavereadcreg(Convert.ToUInt16(e.StartAddress + i + 1)) == 0 ? false : true;

                                    break;
                                case 1:
                                    slave.DataStore.CoilDiscretes[e.StartAddress + i + 1] = slavereadcreg(Convert.ToUInt16(e.StartAddress + i + 1)) == 0 ? false : true;

                                    break;

                                default:
                                    break;
                            }
                            
                        }
                        break;
                }
            }
            catch
            {


            }

        }


        private void Modbus_DataStoreWriteTo(object sender, DataStoreEventArgs e)
        {
            try
            {

                switch (e.ModbusDataType)
                {
                    case ModbusDataType.HoldingRegister:
                        if (globaldata.Foreign == 1)
                        {
                            slavewirtehreg(e.StartAddress, e.Data.B.ToArray());
                        }
                        break;
                    case ModbusDataType.Coil:

                        break;
                }
            }
            catch
            {

            }
        }

        //trigger Modbus_Request_Event
        private void Modbus_Request_Event(object sender, ModbusSlaveRequestEventArgs e)
        {


        }

        /// <summary>
        /// 辰科污染源VOC
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        static ushort slavereadhreg(ushort address)
        {
            try
            {
                ushort value = 0;
                ushort signalddress = 17;
                ushort baseaddress = 23;
                address = (ushort)(address - 1);
                int listnum = SharedSpace.Data.listValue.Count;
                if (address >= 17 && address < 150)
                {
                    ushort[] s = new ushort[2];
                    float[] f = new float[2];

                    if (address >= 17 && address <= 22)   //信号输出
                    {
                        if (address == 17 || address == 18)
                            f[0] = float.Parse(globaldata.m_dpbuffer.ShowList.showDet.fDetValue.ToString("0.000"));
                        if (address == 19 || address == 20)
                            f[0] = float.Parse(globaldata.m_dpbuffer.ShowList.showDet.iDetValue.ToString("0.000"));
                        if (address == 21 || address == 22)
                            f[0] = float.Parse(globaldata.m_dpbuffer.ShowList.showDet.bDetValue.ToString("0.000"));
                        Buffer.BlockCopy(f, 2, s, 0, 2);
                        Buffer.BlockCopy(f, 0, s, 2, 2);
                        value = s[(address - signalddress) % 2];
                    }
                    else if (address >= 23 && listnum > 0)  //组分输出
                    {
                        int readnum = (address - baseaddress) / 2;
                        if (readnum <= listnum - 1)
                        {
                            f[0] = float.Parse(SharedSpace.Data.listValue[readnum].vale.ToString());
                            Buffer.BlockCopy(f, 2, s, 0, 2);
                            Buffer.BlockCopy(f, 0, s, 2, 2);
                            value = s[(address - baseaddress) % 2];
                            //value = address;
                        }
                        else
                        {
                            value = 0;
                        }
                    }
                    else
                    {
                        value = 0;
                    }
                }
                //分析仪状态
                if (address >= 1 && address < 20)
                {
                    switch (address)
                    {
                        case 1:   //进度
                            {
                                if ((ushort)SharedSpace.Data.typesint == 1 || (ushort)SharedSpace.Data.typesint == 2)
                                {
                                    if (SharedSpace.MControl.dateend != 0 && SharedSpace.MControl.zhouqi != 0)
                                    {
                                        value = (ushort)((globaldata.m_hmibuffer.gcinfo.batruncount * 1.0f) /
                                                          (ushort)(SharedSpace.MControl.dateend * 1.0f /
                                                                    SharedSpace.MControl.zhouqi) * 1000);
                                    }
                                    else
                                    {
                                        value = 0;
                                    }

                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 2: // 0-正在采样(默认)  1-手动  2-定时  3-校准成功  4-校准失败  5-校准成功(正在采样)  6-校准失败(正在采样)
                            {
                                if (!SharedSpace.MControl.SetOut && !SharedSpace.MControl.SetRun)
                                {
                                    if ((ushort)SharedSpace.Data.typesint == 3)
                                        value = 5;
                                    else if ((ushort)SharedSpace.Data.typesint == 4)
                                        value = 6;
                                    else
                                        value = (ushort)SharedSpace.Data.typesint;
                                }
                                else
                                {
                                    value = (ushort)SharedSpace.Data.typesint;
                                }
                            }
                            break;
                        case 3:
                            {
                                value = SharedSpace.Data.change == true ? (ushort)1 : (ushort)0;
                            }
                            break;
                        case 4:
                            {
                                value = globaldata.m_hmibuffer.gcinfo.HMIMac[0];
                            }
                            break;
                        case 5:
                            {
                                value = globaldata.m_hmibuffer.gcinfo.HMIMac[1];
                            }
                            break;
                        case 6:
                            {
                                value = globaldata.m_hmibuffer.gcinfo.HMIMac[2];
                            }
                            break;
                        case 7: //设备联机状态
                            {
                                value = (ushort)(globaldata.m_hmibuffer.gcinfo.hmioffline ? 1 : 0);
                            }
                            break;
                        case 8: //HMI状态
                            {
                                value = globaldata.m_hmibuffer.gcinfo.methodstatus;
                            }
                            break;
                        case 9: //前进样口
                            {
                                //if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1)
                                if (globaldata.m_hwconfig.hwconfiginfo.u16InjPortFront == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.FPActualTemp * 10);
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 10: //后进样口
                            {
                                //if (CProfileDevice.m_DevParam.syspara.u16InjPortBehind == 1)
                                if (globaldata.m_hwconfig.hwconfiginfo.u16InjPortBehind == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.BPActualTemp * 10);
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 11: // 柱箱
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16Col == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.ColActualTemp * 10);
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 12: //前检测器
                            {
                                ////if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1)
                                //if (globaldata.m_hwconfig.hwconfiginfo.u16InjPortFront == 1)
                                //{
                                //    //if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 0)
                                //    if (globaldata.m_hwconfig.hwconfiginfo.u16DetFrontType == 0)
                                //    {
                                //        value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.fDetActualTemp * 10);
                                //    }
                                //    //else if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 1)
                                //    else if (globaldata.m_hwconfig.hwconfiginfo.u16DetFrontType == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.fDetActualTemp * 10);
                                }
                                //    else
                                //    {
                                //        value = 0;
                                //    }
                                //}
                                //else
                                //{
                                //    value = 0;
                                //}
                            }
                            break;
                        case 13: //中检测器
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                                //if (globaldata.m_hwconfig.hwconfiginfo.u16DetInter == 1)
                                {
                                    if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 0)
                                    //if (globaldata.m_hwconfig.hwconfiginfo.u16DetInterType == 0)
                                    {
                                        value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.iDetActualTemp * 10);
                                    }
                                    else if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 1)
                                    //else if (globaldata.m_hwconfig.hwconfiginfo.u16DetInterType == 1)
                                    {
                                        value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.iDetActualTemp * 10);
                                    }
                                    else
                                    {
                                        value = 0;
                                    }
                                }
                                else if (CProfileDevice.m_DevParam.syspara.u16AuxHeater1 == 1)
                                //else if (globaldata.m_hwconfig.hwconfiginfo.u16AuxHeater1 == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.AuxActualTemp1 * 10);
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 14: //后检测器
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                                //if (globaldata.m_hwconfig.hwconfiginfo.u16DetBehind == 1)
                                {
                                    if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 0)
                                    //if (globaldata.m_hwconfig.hwconfiginfo.u16DetBehindType == 0)
                                    {
                                        value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.bDetActualTemp * 10);
                                    }
                                    else if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 1)
                                    //else if (globaldata.m_hwconfig.hwconfiginfo.u16DetBehindType == 1)
                                    {
                                        value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.bDetActualTemp * 10);
                                    }
                                    else
                                    {
                                        value = 0;
                                    }
                                }
                                else if (CProfileDevice.m_DevParam.syspara.u16AuxHeater2 == 1)
                                //else if (globaldata.m_hwconfig.hwconfiginfo.u16AuxHeater2 == 1)
                                {
                                    value = (ushort)(globaldata.m_dpbuffer.ShowList.showtemp.AuxActualTemp2 * 10);
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 15: //前点火信号
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 1)
                                {
                                    value = globaldata.m_dpbuffer.ShowList.showDet.fDetStatue;
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                        case 16: //后点火信号
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 1)
                                {
                                    value = globaldata.m_dpbuffer.ShowList.showDet.fDetStatue;
                                }
                                else
                                {
                                    value = 0;
                                }
                            }
                            break;
                    }
                }

                if (address >= 1000 && address < 1500)
                {
                    switch (address)
                    {
                        case 1000:
                            if (CKVocAnalyzer.GlobalCKV.adjustType == CKVocAnalyzer.GlobalCKV.AdjustType.AT_Zero)
                            {
                                value = 0;
                            }
                            else if (CKVocAnalyzer.GlobalCKV.adjustType == CKVocAnalyzer.GlobalCKV.AdjustType.AT_Biao)
                            {
                                value = 1;
                            }
                            else if (CKVocAnalyzer.GlobalCKV.adjustType == CKVocAnalyzer.GlobalCKV.AdjustType.AT_Yang)
                            {
                                value = 2;
                            }
                            break;
                        case 1001:
                            value = globaldata.m_dpbuffer.ShowList.showepc.EPC1_2_Cur;
                            break;
                        case 1002:
                            value = globaldata.m_dpbuffer.ShowList.showepc.EPC1_3_Cur;
                            break;
                        case 1003:
                            //value = CKVocAnalyzer.GlobalCKV.adjustTwo.;
                            value = 0;
                            break;
                        case 1004:
                            value = 0;

                            //value = globaldata.m_dpbuffer.ShowList.showepc.EPC1_3_Cur;
                            break;
                        case 1020:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.fDPower;
                            break;
                        case 1021:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.iDPower;
                            break;
                        case 1022:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.bDPower;
                            break;
                        case 1023:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.fSPower;
                            break;
                        case 1024:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.bSPower;
                            break;
                        case 1025:
                            value = (ushort)globaldata.m_dpbuffer.ShowList.pwmInfo.CbPower;
                            break;
                    }
                }
                if (address >= 1500)
                {
                    switch (address)
                    {
                        case 1500:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[0];
                            break;
                        case 1501:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[2];
                            break;
                        case 1502:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[4];
                            break;
                        case 1503:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[6];
                            break;
                        case 1504:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[8];
                            break;
                        case 1505:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[10];
                            break;
                        case 1506:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[12];
                            break;
                        case 1507:
                            value = globaldata.m_hmibuffer.gcinfo.EPCStatus[14];
                            break;
                        case 1508:
                            value = globaldata.m_hmibuffer.gcinfo.HmiAbnormal[0];
                            break;
                        case 1509:
                            value = globaldata.m_hmibuffer.gcinfo.HmiAbnormal[1];
                            break;
                        case 1510:
                            value = globaldata.m_hmibuffer.gcinfo.HmiAbnormal[2];
                            break;
                        default:
                            break;
                    }
                }
                return value;
            }
            catch (Exception e)
            {
                Console.WriteLine("异常" + e.Message);
                return 0;
            }

        }

        static void slavewirtehreg(ushort address, ushort[] value)
        {
            if (address >= 50 && address < 99)
            {
                switch (address)
                {
                    case 50:
                        globaldata.m_hmibuffer.gcadjust.setpotency = ushortofloat(value);
                        //int index = CKVocAnalyzer.GlobalCKV.autoVlue.FindIndex(s => s.name == "NMHC");
                        CKVocAnalyzer.GlobalCKV.nmhcsavevale.biaozhun = Math.Round(globaldata.m_hmibuffer.gcadjust.setpotency, 2);
                        CKVocAnalyzer.ExteConfig.Instance.Save();
                        CKVocAnalyzer.AutoAdjust.IsSaveConfig = true;
                        break;
                    case 52:
                        globaldata.m_hmibuffer.gcadjust.trackpotency = ushortofloat(value);
                        int tindex = CKVocAnalyzer.GlobalCKV.autoVlue.FindIndex(s => s.name == "CH4");
                        CKVocAnalyzer.GlobalCKV.autoVlue[tindex].biaozhun = Math.Round(globaldata.m_hmibuffer.gcadjust.trackpotency, 2);
                        CKVocAnalyzer.ExteConfig.Instance.Save();
                        CKVocAnalyzer.AutoAdjust.IsSaveConfig = true;

                        break;
                    case 54:
                        globaldata.m_hmibuffer.gcadjust.settime = ushortolong(value);
                        CKVocAnalyzer.GlobalCKV.AutoTimes = (int)globaldata.m_hmibuffer.gcadjust.settime / 60;
                        CKVocAnalyzer.ExteConfig.Instance.Save();
                        CKVocAnalyzer.AutoAdjust.IsSaveConfig = true;

                        break;
                    case 56:
                        globaldata.m_hmibuffer.gcadjust.executefun = ushortolong(value);
                        break;
                    case 58:
                        globaldata.m_hmibuffer.gcadjust.funrun = ushortolong(value);
                        executefuncation();
                        globaldata.m_hmibuffer.gcadjust.funrun = 0;
                        break;
                    case 60:
                        //globaldata.m_hmibuffer.gcadjust.runtime = ushortolong(value);
                        //SharedSpace.MControl.zhouqi* SharedSpace.MControl.runcount <= SharedSpace.MControl.dateend
                        break;
                    case 62:
                        //globaldata.m_hmibuffer.gcadjust.executestat = ushortolong(value);
                        break;
                    default:
                        break;
                }
            }

            //if (address == 200)
            //{
            //    if (value[0] == 0)
            //    {
            //        globaldata.usable = false;
            //    }
            //    if (value[0] == 1)
            //    {
            //        globaldata.usable = true;
            //    }
            //}


            //if (address == 100 || address == 99)
            //{
            //    if (value[0] == 1)
            //    {
            //        CurveDisPlay.curdisp.SetAutoAdjust(value[1]);
            //    }
            //    else if (value[0] == 0)
            //    {
            //        CurveDisPlay.curdisp.StopAutoAdjust();
            //    }
            //}

            //if (address == 102)
            //{
            //    if (value[0] == 1)
            //    {
            //        globaldata.IsStopOs = true;
            //    }
            //    else if (value[0] == 0)
            //    {
            //        globaldata.IsStopOs = false;
            //    }

            //}

            Log.Info("MoudbusSlave Wirte adder:" + address.ToString() + " value:" + value.ToString());
        }

        public static void executefuncation()
        {
            //if (globaldata.m_hmibuffer.gcadjust.funrun==1)
            //{
            //    switch (globaldata.m_hmibuffer.gcadjust.executefun)
            //    {
            //        case 0x02:
            //        case 0x03:
            //        case 0x00:
            //            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Ling;
            //            break;

            //        case 0x0A:
            //        case 0x0E:
            //        case 0x0B:
            //        case 0x0F:
            //        case 0x08:
            //        case 0x0C:
            //            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Xiao;
            //            break;

            //        case 0x06:
            //        case 0x07:
            //        case 0x04:
            //            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Biao;
            //            break;
            //        default:
            //            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Default;
            //            break;
            //    }
            //}
            //else
            //{
            //    CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_fang;
            //}

            switch (globaldata.m_hmibuffer.gcadjust.funrun)
            {
                case 1:
                    switch (globaldata.m_hmibuffer.gcadjust.executefun)
                    {
                        case 0x02:
                        case 0x03:
                        case 0x00:
                            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Ling;
                            break;

                        case 0x0A:
                        case 0x0E:
                        case 0x0B:
                        case 0x0F:
                        case 0x08:
                        case 0x0C:
                            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Xiao;
                            break;

                        case 0x06:
                        case 0x07:
                        case 0x04:
                            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Biao;
                            break;
                        default:
                            CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Default;
                            break;
                    }
                    break;
                case 2:
                    CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_fang;
                    break;
                case 3:
                    CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_fang;
                    break;
                default:
                    //CKVocAnalyzer.AutoAdjust.Af_Option = CKVocAnalyzer.AutoAdjust.AdjustFromSet.AF_Default;
                    break;
            }
        }

        public static ushort slavereadcreg(ushort address)
        {

            ushort value = 0;
            switch (address)
            {
                case 16:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[0];
                    break;
                case 17:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[1];
                    break;
                case 18:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[2];
                    break;
                case 19:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[3];
                    break;
                case 20:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[4];
                    break;
                case 21:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[5];
                    break;
                case 22:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[6];
                    break;
                case 23:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[7];
                    break;
                case 24:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[8];
                    break;
                case 25:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[9];
                    break;
                case 26:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[10];
                    break;
                case 27:
                    value = globaldata.m_hmibuffer.gcinfo.TenRelay[11];
                    break;
                default:
                    value = 0;
                    break;
            }
            //   Console.WriteLine("MoudbusSlave Read Holding adder is " + CKVocAnalyzer.GlobalCKV.valueparamlist.ToString());
            return value;


        }


        /// <summary>
        /// 林工新通讯协议
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        static ushort newslavereadhreg1(ushort address)
        {
            try
            {
                ushort value = 0;
                //ushort signalddress = 36;
                ushort baseaddress = 0;
                address = (ushort)(address - 1);
                int listnum = SharedSpace.Data.listValue.Count;
                ushort[] s = new ushort[2];
                float[] f = new float[2];
                long[] l = new long[1];

                if (address<40)
                {
                    if (address < 6 && listnum>0)
                    {
                        int readnum = (address - baseaddress) / 2;
                        if (readnum <= listnum - 1)
                        {
                            f[0] = float.Parse(SharedSpace.Data.listValue[readnum].vale.ToString());
                            Buffer.BlockCopy(f, 2, s, 0, 2);
                            Buffer.BlockCopy(f, 0, s, 2, 2);
                            value = s[(address - baseaddress) % 2];
                        }
                        else
                        {
                            value = 0;
                        }
                    }
                    else
                    {
                        value = 0;
                    }

                    if (address >= 8)
                    {
                        if (address == 8 || address == 9)
                        {
                            if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1)
                            {
                                f[0] = globaldata.m_dpbuffer.ShowList.showtemp.FPActualTemp;
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        if (address == 10 || address == 11)
                        {
                            if (CProfileDevice.m_DevParam.syspara.u16InjPortBehind == 1)
                            {
                                f[0] = globaldata.m_dpbuffer.ShowList.showtemp.BPActualTemp;
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        if (address == 12 || address == 13)
                        {
                            if (CProfileDevice.m_DevParam.syspara.u16Col == 1)
                            {
                                f[0] = globaldata.m_dpbuffer.ShowList.showtemp.ColActualTemp;
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        if (address == 14 || address == 15)
                        {
                            //if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1)
                            {
                                if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 0)
                                {
                                    f[0] = globaldata.m_dpbuffer.ShowList.showtemp.fDetActualTemp;
                                }
                                else if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 1)
                                {
                                    f[0] = globaldata.m_dpbuffer.ShowList.showtemp.fDetActualTemp;
                                }
                                else
                                {
                                    f[0] = 0;
                                }
                            }
                            //else
                            //{
                            //    value = 0;
                            //}
                        }
                        if (address == 20 || address == 21)
                        {
                            f[0] = float.Parse(globaldata.m_dpbuffer.ShowList.showDet.fDetValue.ToString("0.000"));
                        }

                        Buffer.BlockCopy(f, 2, s, 0, 2);
                        Buffer.BlockCopy(f, 0, s, 2, 2);
                        value = s[(address - 8) % 2];
                    }
                }

                if (address >= 36 && address < 50)
                {
                    if (address >= 36 && address < 48)
                    {
                        if (address == 36 || address == 37)
                        { }
                        if (address == 38 || address == 39)
                        { }
                        //jiawan
                        if (address == 40 || address == 41)
                        {
                            int index = CKVocAnalyzer.GlobalCKV.adjustTwo.FindIndex(p => p.name == "CH4");
                            if (index!=-1)
                            {
                                f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.adjustTwo[index].BValue.ToString("0.00"));
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        if (address == 42 || address == 43)
                        {
                            int index = CKVocAnalyzer.GlobalCKV.adjustTwo.FindIndex(p => p.name == "CH4");
                            if (index != -1)
                            {
                                f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.adjustTwo[index].CValue.ToString("0.00"));
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        //zongting
                        if (address == 44 || address == 45)
                        {
                            int index = CKVocAnalyzer.GlobalCKV.adjustTwo.FindIndex(p => p.name == "THC");
                            if (index != -1)
                            {
                                f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.adjustTwo[index].BValue.ToString("0.00"));
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }
                        if (address == 46 || address == 47)
                        {
                            int index = CKVocAnalyzer.GlobalCKV.adjustTwo.FindIndex(p => p.name == "THC");
                            if (index != -1)
                            {
                                f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.adjustTwo[index].CValue.ToString("0.00"));
                            }
                            else
                            {
                                f[0] = 0;
                            }
                        }

                        Buffer.BlockCopy(f, 2, s, 0, 2);
                        Buffer.BlockCopy(f, 0, s, 2, 2);
                        value = s[(address - 42) % 2];
                    }
                    if (address >= 48 && address < 50)
                    {
                        if (address == 48 || address == 49)
                            l[0] = globaldata.m_hmibuffer.gcadjust.executefun;


                        Buffer.BlockCopy(l, 2, s, 0, 2);
                        Buffer.BlockCopy(l, 0, s, 2, 2);

                        value = s[(address - 46) % 2];
                    }
                }

                if (address >= 50 && address < 54)
                {
                    if (address == 50 || address == 51)
                        f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.nmhcsavevale.biaozhun.ToString("0.000"));
                    if (address == 52 || address == 53)
                    {
                        int tindex = CKVocAnalyzer.GlobalCKV.autoVlue.FindIndex(p => p.name == "CH4");
                        f[0] = float.Parse(CKVocAnalyzer.GlobalCKV.autoVlue[tindex].biaozhun.ToString("0.000"));
                    }
                    Buffer.BlockCopy(f, 2, s, 0, 2);
                    Buffer.BlockCopy(f, 0, s, 2, 2);
                    value = s[(address - 50) % 2];
                }

                if (address >= 54 && address < 64)
                {
                    switch (address)
                    {
                        case 54:
                        case 55:
                            l[0] = CKVocAnalyzer.GlobalCKV.AutoTimes * 60;
                            break;
                        case 56:
                        case 57:
                            l[0] = globaldata.m_hmibuffer.gcadjust.executefun;
                            break;
                        case 58:
                        case 59:
                            l[0] = globaldata.m_hmibuffer.gcadjust.funrun;
                            break;
                        case 60:
                        case 61:
                            //if ((ushort)SharedSpace.Data.typesint == 1 || (ushort)SharedSpace.Data.typesint == 2)
                            //{
                            //    if (SharedSpace.MControl.dateend != 0 && SharedSpace.MControl.zhouqi != 0)
                            //    {
                            //        l[0]= globaldata.m_hmibuffer.gcadjust.runtime;
                            //    }
                            //    else
                            //    {
                            //        l[0] = 0;
                            //    }
                            //}
                            //else
                            //{
                            //    l[0] = 0;
                            //}

                            l[0] = CKVocAnalyzer.GlobalCKV.TimeCount;
                            break;
                        case 62:
                        case 63:
                            if (!CKVocAnalyzer.GlobalCKV.IsAutoAdjust)
                            {
                                l[0] = 0;
                            }
                            else if (CKVocAnalyzer.GlobalCKV.IsAutoAdjust || (SharedSpace.MControl.SetOut && SharedSpace.MControl.SetRun == false))
                            {
                                if (SharedSpace.MControl.zhouqi * SharedSpace.MControl.runcount <= SharedSpace.MControl.dateend)
                                {
                                    l[0] = 2;
                                }
                                else
                                {
                                    l[0] = 1;
                                }
                            }
                            else if (SharedSpace.MControl.zhouqi * SharedSpace.MControl.runcount >= SharedSpace.MControl.dateend && SharedSpace.MControl.SetOut && SharedSpace.MControl.SetRun)
                            {

                                if (!SharedSpace.MControl.SetOut && !SharedSpace.MControl.SetRun)
                                {
                                    l[0] = 4;
                                }
                                else
                                {
                                    l[0] = 3;
                                }
                            }
                            else if (SharedSpace.MControl.forceEnd)
                            {
                                l[0] = 5;
                            }
                            //l[0] = globaldata.m_hmibuffer.gcadjust.settime;
                            break;
                    }

                    Buffer.BlockCopy(l, 2, s, 0, 2);
                    Buffer.BlockCopy(l, 0, s, 2, 2);

                    value = s[(address - 54) % 2];

                }

                if (address >= 1000)
                {
                    switch (address)
                    {
                        case 1000:
                            value = globaldata.m_hmibuffer.gcinfo.HMIMac[0];

                            break;
                        case 1001:
                            value = globaldata.m_hmibuffer.gcinfo.HMIMac[1];

                            break;
                        case 1002:
                            value = globaldata.m_hmibuffer.gcinfo.HMIMac[2];

                            break;
                        case 1003:
                            if (!SharedSpace.MControl.SetOut && !SharedSpace.MControl.SetRun)
                            {
                                if ((ushort)SharedSpace.Data.typesint == 3)
                                    value = 5;
                                else if ((ushort)SharedSpace.Data.typesint == 4)
                                    value = 6;
                                else
                                    value = (ushort)SharedSpace.Data.typesint;
                            }
                            else
                            {
                                value = (ushort)SharedSpace.Data.typesint;
                            }
                            break;
                        case 1004:
                            value = (ushort)(globaldata.m_hmibuffer.gcinfo.hmioffline ? 1 : 0);

                            break;
                        case 1005:
                            value = globaldata.m_hmibuffer.gcinfo.methodstatus;

                            break;
                        case 1006:
                            value = globaldata.m_dpbuffer.ShowList.showDet.fDetStatue;
                            break;
                        case 1007:
                            value = globaldata.m_dpbuffer.ShowList.showDet.iDetStatue;
                            break;
                        default:
                            break;
                    }
                }

                return value;
            }
            catch 
            {
                return 0;
            }
        }

        //public struct state
        //{
        //    public ushort blvalve;             //标零阀开关
        //    public ushort yblvalve;            //样气阀标零阀切换
        //    public ushort biaoling;            //零/标
        //    public ushort Sample_Adjust;       //走气/校气
        //    public ushort jzgz;                //校准/跟踪
        //    public ushort jzcl_stat;           //校准状态/测量状态
        //    public ushort normal_error;        //正常/报警
        //    public ushort Long_Short_range;    //本地/遥控指示位
        //}

        private static float ushortofloat(ushort[] us)
        {
            int intSign, intSignRest, intExponent, intExponentRest;
            float faResult, faDigit;
            intSign = us[0] / 32768;
            intSignRest = us[0] % 32768;
            intExponent = intSignRest / 128;
            intExponentRest = intSignRest % 128;
            faDigit = (float)(intExponentRest * 65536 + us[1]) / 8388608;
            return faResult = (float)Math.Pow(-1, intSign) * (float)Math.Pow(2, intExponent - 127) * (faDigit + 1);

        }

        private static long ushortolong(ushort[] us)
        {
            for (int i = 0; i < us.Length; i++)
            {
                if (us[i] != 0)
                {
                    return us[i];
                }
            }
            return 0;
        }
    }

}
