//C++ syntax : HRESULT STDMETHODCALLTYPE OptimizeAccess(DATAFRAME* lpFrame, DEVREG* lpDevReg, VARIANT_BOOL * pRetVal); public bool OptimizeAccess(IntPtr pDataFrame, IntPtr pDevReg) { strLastError = ""; if (pDataFrame == IntPtr.Zero || pDevReg == IntPtr.Zero) { strLastError = "调用参数错误。"; return(false); } UCView.DATAFRAME DataFrame = (UCView.DATAFRAME)Marshal.PtrToStructure(pDataFrame, typeof(UCView.DATAFRAME)); UCView.DEVREG DevReg = (UCView.DEVREG)Marshal.PtrToStructure(pDevReg, typeof(UCView.DEVREG)); //一定要首先判断地址 try { if (DataFrame.DevAddr.nAddr != DevReg.devAddr.nAddr) { return(false); } if (DataFrame.WRType == UCView.FT_WRITE) { return(DevReg.regAddr.nAddr == DataFrame.StartRegAddr.nAddr); } //Check if we can read this register together with this frame if (DevReg.regType == 0) { return(DataFrame.RegType == 0); } if (DevReg.regType >= 1 && DevReg.regType <= 4) { return(DataFrame.RegType >= 1 && DataFrame.RegType <= 4); } if (DevReg.regType >= 5 && DevReg.regType <= 16) { return(DataFrame.RegType >= 5 && DataFrame.RegType <= 16); } if (DevReg.regType == 17) { return(DataFrame.RegType == 17); } return(true); } catch { Utility.Trace2Logger(atomMsgSrcName, "OptimizeAccess 遇到异常"); return(false); } }
//C++ syntax : HRESULT STDMETHODCALLTYPE AccessDevice(DATAFRAME* lpFrame, VARIANT_BOOL * pRetVal); public bool AccessDevice(IntPtr pDataFrame) { strLastError = ""; if (pDataFrame == IntPtr.Zero) { strLastError = "调用参数错误。"; return(false); } try { UCView.DATAFRAME DataFrame = (UCView.DATAFRAME)Marshal.PtrToStructure(pDataFrame, typeof(UCView.DATAFRAME)); return(AccessDevice(ref DataFrame)); } catch { Utility.Trace2Logger(atomMsgSrcName, "AccessDevice 遇到异常"); return(false); } }
//C++ syntax : HRESULT STDMETHODCALLTYPE Diagnose(LPSTR szLibName, DEVADDR * lpDevAddr, VARIANT_BOOL * pRetVal); public bool Diagnose([MarshalAs(UnmanagedType.LPStr)] string LibName, IntPtr pDevAddr) { strLastError = ""; if (string.IsNullOrEmpty(LibName) || pDevAddr == IntPtr.Zero) { strLastError = "调用参数错误。"; return(false); } UCView.DEVADDR DevAddr = (UCView.DEVADDR)Marshal.PtrToStructure(pDevAddr, typeof(UCView.DEVADDR)); // if (time(NULL)-pThis->m_tLastInitComm > 3600) { if (InitComm() == false) { return(false); } WinApi.Sleep(500); } try { //ToDo: check your device to see if it is all right UCView.DATAFRAME commonFrame = new UCView.DATAFRAME(); //Format youe Common Frame according to szLibName commonFrame.pszLibName = Marshal.StringToHGlobalAnsi(LibName); commonFrame.DevAddr = DevAddr; commonFrame.RegType = 0;//A very common register commonFrame.StartRegAddr.nAddr = 0; commonFrame.EndRegAddr.nAddr = 0; commonFrame.WRType = UCView.FT_READ; return(AccessDevice(ref commonFrame)); } catch { Utility.Trace2Logger(atomMsgSrcName, "Diagnose 遇到异常"); return(false); } }
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); } }
protected bool CommitCommand(ref UCView.DATAFRAME DataFrame, ref byte[] buf, int nLen, int nWantLen, ref int nReceLen) { return(true); }
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); } }
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); } }
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); } }