Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #3
0
 protected bool FormatCommand(ref UCView.DATAFRAME DataFrame, ref byte[] buf, ref int nLen, ref int nReceLen)
 {
     try
     {
         if (DataFrame.WRType == UCView.FT_READ)
         {
             if (DataFrame.RegType == 0)
             {
                 return(true);
             }
             if (DataFrame.RegType >= 1 && DataFrame.RegType <= 4)
             {
                 return(true);
             }
             if (DataFrame.RegType >= 5 && DataFrame.RegType <= 16)
             {
                 return(true);
             }
             if (DataFrame.RegType == 17)
             {
                 return(true);
             }
         }
         else
         {
             IntPtr        pos         = DataFrame.TagregList.GetHeadPosition();
             UCView.TAGREG reg         = DataFrame.TagregList.GetTagAt(pos);
             DoorControl   doorControl = new DoorControl(@"protocol=TCP,ipaddress=192.168.0.200,port=4370,timeout=5000,passwd=");
             #region 连接/断开门禁控制器
             if (DataFrame.RegType == 0)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, "正在连接门禁控制器");
                     if (!doorControl.ConnectToDevice())
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("连接门禁控制器失败,错误代码:{0}", doorControl.ErrorID));
                         reg.dwTagID = 0;
                     }
                     else
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("连接门禁控制器成功"));
                     }
                 }
                 if (reg.RegValue.longVal == 0)
                 {
                     doorControl.DisConnect();
                     Utility.Trace2Logger(atomMsgSrcName, "断开连接成功");
                 }
             }
             #endregion
             #region 开关门操作
             if (DataFrame.RegType >= 1 && DataFrame.RegType <= 4)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, string.Format("正在执行操作:打开门{0}", DataFrame.RegType));
                     if (!doorControl.OpneDoor(Convert.ToInt32(DataFrame.RegType), 10, true, false))
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("打开门{0}操作失败,错误代码:{1}", DataFrame.RegType, doorControl.ErrorID));
                     }
                     else
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("打开门{0}操作成功", DataFrame.RegType));
                     }
                 }
                 if (reg.RegValue.longVal == 0)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, string.Format("正在执行操作:关闭门{0}", DataFrame.RegType));
                     if (!doorControl.OpneDoor(Convert.ToInt32(DataFrame.RegType), 10, true, false))
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("关闭门{0}操作失败,错误代码:{1}", DataFrame.RegType, doorControl.ErrorID));
                     }
                     else
                     {
                         Utility.Trace2Logger(atomMsgSrcName, String.Format("关闭门{0}操作成功", DataFrame.RegType));
                     }
                 }
             }
             #endregion
             if (DataFrame.RegType == 17)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, string.Format("正在执行操作:重启设备"));
                     if (!doorControl.RestartDevice())
                     {
                         Utility.Trace2Logger(atomMsgSrcName, string.Format("重启设备出错,错误代码:{0}", doorControl.ErrorID));
                     }
                     else
                     {
                         Utility.Trace2Logger(atomMsgSrcName, string.Format("重启设备成功"));
                     }
                 }
             }
         }
         return(true);
     }
     catch
     {
         Utility.Trace2Logger(atomMsgSrcName, "FormatCommand 遇到异常");
         return(false);
     }
 }
Пример #4
0
 protected bool FormatCommand(ref UCView.DATAFRAME DataFrame, ref byte[] buf, ref int nLen, ref int nReceLen)
 {
     try
     {
         if (DataFrame.WRType == UCView.FT_READ)
         {
             if (DataFrame.RegType == 0)
             {
                 return(true);
             }
             if (DataFrame.RegType >= 1 && DataFrame.RegType <= 4)
             {
                 return(true);
             }
             if (DataFrame.RegType >= 5 && DataFrame.RegType <= 16)
             {
                 return(true);
             }
             if (DataFrame.RegType == 17)
             {
                 return(true);
             }
         }
         else
         {
             IntPtr        pos = DataFrame.TagregList.GetHeadPosition();
             UCView.TAGREG reg = DataFrame.TagregList.GetTagAt(pos);
             #region 连接/断开门禁控制器
             if (DataFrame.RegType == 0)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, "正在连接门禁控制器");
                 }
                 if (reg.RegValue.longVal == 0)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, "断开连接成功");
                 }
             }
             #endregion
             #region 开关门操作
             if (DataFrame.RegType >= 1 && DataFrame.RegType <= 4)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, string.Format("正在执行操作:打开门{0}", DataFrame.RegType));
                 }
                 if (reg.RegValue.longVal == 0)
                 {
                     Utility.Trace2Logger(atomMsgSrcName, string.Format("正在执行操作:关闭门{0}", DataFrame.RegType));
                 }
             }
             #endregion
             if (DataFrame.RegType == 17)
             {
                 if (reg.RegValue.longVal == 1)
                 {
                 }
             }
         }
         return(true);
     }
     catch
     {
         Utility.Trace2Logger(atomMsgSrcName, "FormatCommand 遇到异常");
         return(false);
     }
 }