Ejemplo n.º 1
0
        protected bool AccessDevice(ref UCView.DATAFRAME DataFrame)
        {
            IntPtr    pos;
            string    strLog;
            int       i = 0, nCmdLen = 0, nWantLen = 0;
            const int nMaxRetry = 3;

            try
            {
                //Utility.ReceiveData(hComPort, ref combuf, Utility.COM_BUFF_SIZE, ref i);

                if (!FormatCommand(ref DataFrame, ref combuf, ref nCmdLen, ref nWantLen))
                {
                    strLog = String.Format("{0} FormatCommand fail. rw={1},type={2},start={3},end={4}", Marshal.PtrToStringAnsi(DataFrame.pszLibName), DataFrame.WRType, DataFrame.RegType, DataFrame.StartRegAddr.nAddr, DataFrame.EndRegAddr.nAddr);
                    Utility.Trace2Logger(atomMsgSrcName, strLog);
                    Debug.WriteLine(strLog);
                    if (DataFrame.WRType == UCView.FT_READ)
                    {
                        DataFrame.DiscardFrame();
                        return(true);
                    }
                    return(false);
                }
                if (i >= nMaxRetry)
                {
                    return(false);
                }
            }
            catch
            {
                Utility.Trace2Logger(atomMsgSrcName, "AccessDevice 遇到异常");
                return(false);
            }
            //OK, DATA is valid.
            try
            {
                if (DataFrame.WRType == UCView.FT_READ)
                {
                    DoorControl doorControl = new DoorControl(@"protocol=TCP,ipaddress=192.168.0.200,port=4370,timeout=5000,passwd=");
                    //Translate receive data to registers.
                    pos = DataFrame.TagregList.GetHeadPosition();
                    while (pos != IntPtr.Zero)
                    {
                        UCView.TAGREG reg = DataFrame.TagregList.GetTagAt(pos);
                        if (reg.devReg.regType == 17 || reg.devReg.regType == 0)
                        {
                            reg.dwTagID = 0;
                        }
                        #region 获取门开关状态
                        if (reg.devReg.regType >= 1 && reg.devReg.regType <= 4)
                        {
                            int nResult = doorControl.GetDoorStatus(Convert.ToInt32(reg.devReg.regType));
                            if (nResult == 1)
                            {
                                DataFrame.TagregList.SetTagValue(pos, 0);
                            }
                            else if (nResult == 2)
                            {
                                DataFrame.TagregList.SetTagValue(pos, 1);
                            }
                            else
                            {
                                Utility.Trace2Logger(atomMsgSrcName, string.Format("获取门{0}状态出错,错误代码:{1}", reg.devReg.regType, nResult));
                                reg.dwTagID = 0;
                            }
                        }
                        #endregion
                        #region 获取门报警状态
                        if (reg.devReg.regType >= 5 && reg.devReg.regType <= 16)
                        {
                            int nResult   = 0;
                            int DoorIndex = 0;
                            if (reg.devReg.regType >= 5 && reg.devReg.regType <= 7)
                            {
                                DoorIndex = 1;
                                nResult   = doorControl.GetAlarmStatus(DoorIndex);
                                switch (reg.devReg.regType)
                                {
                                case 5:
                                    if (nResult == 1)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 6:
                                    if (nResult == 2)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 7:
                                    if (nResult == 3)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;
                                }
                            }
                            if (reg.devReg.regType >= 8 && reg.devReg.regType <= 10)
                            {
                                DoorIndex = 2;
                                nResult   = doorControl.GetAlarmStatus(DoorIndex);
                                switch (reg.devReg.regType)
                                {
                                case 8:
                                    if (nResult == 1)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 9:
                                    if (nResult == 2)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 10:
                                    if (nResult == 3)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;
                                }
                            }
                            if (reg.devReg.regType >= 11 && reg.devReg.regType <= 13)
                            {
                                DoorIndex = 3;
                                nResult   = doorControl.GetAlarmStatus(DoorIndex);
                                switch (reg.devReg.regType)
                                {
                                case 11:
                                    if (nResult == 1)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 12:
                                    if (nResult == 2)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 13:
                                    if (nResult == 3)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;
                                }
                            }
                            if (reg.devReg.regType >= 14 && reg.devReg.regType <= 16)
                            {
                                DoorIndex = 4;
                                nResult   = doorControl.GetAlarmStatus(DoorIndex);
                                switch (reg.devReg.regType)
                                {
                                case 14:
                                    if (nResult == 1)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 15:
                                    if (nResult == 2)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;

                                case 16:
                                    if (nResult == 3)
                                    {
                                        DataFrame.TagregList.SetTagValue(pos, 1);
                                    }
                                    break;
                                }
                            }
                            if (nResult > 3 || nResult <= 0)
                            {
                                Utility.Trace2Logger(atomMsgSrcName, String.Format("获取门{0}警告信息出错,错误代码:{1}", DoorIndex, nResult));
                            }
                        }
                        #endregion
                        pos = DataFrame.TagregList.GetNextPosition(pos);
                    }
                }
                else
                {
                }
                return(true);
            }
            catch
            {
                Utility.Trace2Logger(atomMsgSrcName, "AccessDevice 遇到异常");
                return(false);
            }
        }
Ejemplo n.º 2
0
        protected bool AccessDevice(ref UCView.DATAFRAME DataFrame)
        {
            IntPtr    pos;
            string    strLog;
            int       i = 0, nCmdLen = 0, nWantLen = 0;
            const int nMaxRetry = 3;

            try
            {
                //Utility.ReceiveData(hComPort, ref combuf, Utility.COM_BUFF_SIZE, ref i);

                if (!FormatCommand(ref DataFrame, ref combuf, ref nCmdLen, ref nWantLen))
                {
                    strLog = String.Format("{0} FormatCommand fail. rw={1},type={2},start={3},end={4}", Marshal.PtrToStringAnsi(DataFrame.pszLibName), DataFrame.WRType, DataFrame.RegType, DataFrame.StartRegAddr.nAddr, DataFrame.EndRegAddr.nAddr);
                    Utility.Trace2Logger(atomMsgSrcName, strLog);
                    Debug.WriteLine(strLog);
                    if (DataFrame.WRType == UCView.FT_READ)
                    {
                        DataFrame.DiscardFrame();
                        return(true);
                    }
                    return(false);
                }
                if (i >= nMaxRetry)
                {
                    return(false);
                }
            }
            catch
            {
                Utility.Trace2Logger(atomMsgSrcName, "AccessDevice 遇到异常");
                return(false);
            }
            //OK, DATA is valid.
            try
            {
                if (DataFrame.WRType == UCView.FT_READ)
                {
                    pos = DataFrame.TagregList.GetHeadPosition();
                    while (pos != IntPtr.Zero)
                    {
                        UCView.TAGREG reg = DataFrame.TagregList.GetTagAt(pos);
                        if (reg.devReg.regType == 17 || reg.devReg.regType == 0)
                        {
                            reg.dwTagID = 0;
                        }
                        #region 获取门开关状态
                        if (reg.devReg.regType >= 1 && reg.devReg.regType <= 4)
                        {
                        }
                        #endregion
                        #region 获取门报警状态
                        if (reg.devReg.regType >= 5 && reg.devReg.regType <= 16)
                        {
                            if (reg.devReg.regType >= 5 && reg.devReg.regType <= 7)
                            {
                            }
                            if (reg.devReg.regType >= 8 && reg.devReg.regType <= 10)
                            {
                            }
                            if (reg.devReg.regType >= 11 && reg.devReg.regType <= 13)
                            {
                            }
                            if (reg.devReg.regType >= 14 && reg.devReg.regType <= 16)
                            {
                            }
                        }
                        #endregion
                        pos = DataFrame.TagregList.GetNextPosition(pos);
                    }
                }
                else
                {
                }
                return(true);
            }
            catch
            {
                Utility.Trace2Logger(atomMsgSrcName, "AccessDevice 遇到异常");
                return(false);
            }
        }