public int GetNodeValue(string nodeStrGuid, out object nodeValue) { int rtc = -1; nodeValue = null; try { OPCDATASOURCE opcDataSource = OPCDATASOURCE.OPC_DS_DEVICE; OPCItemState opcItemState = new OPCItemState(); DriveNodeEntity driveNode = allOPCItemNodes.Find(p => nodeStrGuid.Equals(p.nodeId)); ItemDef itemdef = syncIOGroup.Item(driveNode.nodeName); rtc = syncIOGroup.Read(opcDataSource, itemdef, out opcItemState); if (HRESULTS.Succeeded(rtc)) { nodeValue = opcItemState.DataValue; //txtItemQuality.Text = syncIOGroup.GetQualityString(opcItemState.Quality); //txtTimeStamp.Text = DateTime.FromFileTime(opcItemState.TimeStamp).ToString(); } } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "GetNodeValue param:'{1}' ,exception:{0}", ex.Message, nodeStrGuid); } return(rtc); }
private void Monitor() { int i = 0; var dc = this.ItemPoints.Where(x => x.business_code == "03").FirstOrDefault(); if (dc == null) //没有配置状态点 { return; } while (true) { try { OPCItemState rslt; OPCDATASOURCE dsrc = OPCDATASOURCE.OPC_DS_DEVICE; int j = mySyncIOGroup.Read(dsrc, dc.Item_name, out rslt); } catch (Exception exc) { dc.val = "-1"; OnDataReceiveEvent(dc); } System.Threading.Thread.Sleep(3000); } }
public override void Init(XmlNode xmlconf) { base.Init(xmlconf); _progid = xmlconf.Attributes["progid"].Value; _host = xmlconf.Attributes["host"].Value; _grpname = xmlconf.Attributes["group"].Value; //_callback = Convert.ToBoolean(xmlconf.Attributes["callback"].Value); try { string opcds = xmlconf.Attributes["opcds"].Value; if ("cache".Equals(opcds.ToLower())) { _opcds = OPCDATASOURCE.OPC_DS_CACHE; } else if ("device".Equals(opcds.ToLower())) { _opcds = OPCDATASOURCE.OPC_DS_DEVICE; } }catch (Exception e) { _opcds = OPCDATASOURCE.OPC_DS_DEVICE; } XmlNodeList nodes = xmlconf.SelectNodes("map"); _maps = new string[nodes.Count, 2]; //_opcitms = new OPCItemDef[nodes.Count]; int i = 0; foreach (XmlNode node in nodes) { _maps[i, 0] = node.Attributes["name"].Value; _maps[i, 1] = node.Attributes["opctag"].Value; i++; } }
/// <summary> /// 单标签同步读 /// </summary> /// <param name="sGroupName"></param> /// <param name="itemsV"></param> public object SyncReadItem(string sGroupName, int ItemIdx, OPCDATASOURCE OpcDataSrc) { object TmpItemsVal = null; if (pOpcSrv.CheckOpcGroupExist(sGroupName) == true) { int[] ItemsIdx = new int[1]; object[] ItemsVal = null; ItemsIdx[0] = ItemIdx; // OPCDATASOURCE.OPC_DS_CACHE, OPCDATASOURCE.OPC_DS_DEVICE ItemsVal = pOpcSrv.SyncReadItems(sGroupName, ItemsIdx, OpcDataSrc); if (ItemsVal != null) { TmpItemsVal = ItemsVal[0]; } } else { MessageBox.Show(sGroupName + "组没有创建!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); TmpItemsVal = null; } return(TmpItemsVal); }
//************************************************************************************************ #region 标签同步 /// <summary> /// 整个组标签同步读 /// </summary> /// <param name="sGroupName"></param> /// <param name="itemsV"></param> public object[] SyncReadItemsGroup(string sGroupName, OPCDATASOURCE OpcDataSrc) //缓存,设备 { object[] TmpItemsVal = null; if (pOpcSrv.CheckOpcGroupExist(sGroupName) == true) { //根据组名获取相应组标签表 string OpcItemTblName = "OpcItemTbl_" + sGroupName; DataTable dt = TSqlDbClass.RetnTblBySqlCmd(TGlobalVar.sSqlConn, "Select * From " + OpcItemTblName); if (dt != null) { int[] ItemsIndex = new int[dt.Rows.Count]; for (int k = 0; k < dt.Rows.Count; k++) { ItemsIndex[k] = k; } // OPCDATASOURCE.OPC_DS_CACHE, OPCDATASOURCE.OPC_DS_DEVICE TmpItemsVal = pOpcSrv.SyncReadItems(sGroupName, ItemsIndex, OpcDataSrc); } } else { MessageBox.Show(sGroupName + "组没有创建!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); TmpItemsVal = null; } return(TmpItemsVal); }
/// <summary> /// 启动函数 /// </summary> /// <returns></returns> public IResult init() { OPCItemState rslt; OPCDATASOURCE dsrc = OPCDATASOURCE.OPC_DS_DEVICE; ILE.LEResult res = new LEResult(); ///将所有数据点放进RefreshGroup容器 foreach (ILE.IDataPoint po in this.ItemPoints) { if (po.business_code == "01" | po.business_code == "02") //如果是采集完成数与异常数,要把初始值拿到用于扣减 { int j = mySyncIOGroup.Read(dsrc, po.Item_name, out rslt); po.InitVal = rslt.DataValue.ToString(); } int i = myRefreshGroup.Add(po.Item_name); if (!HRESULTS.Succeeded(i)) { res.Result = false; res.ExtMessage = "新增监测点失败"; this.server.Disconnect(); this.server = null; return(res); } } //Action action = new Action(Monitor); //action.BeginInvoke(null, null); thread.Start(); res.Result = true; return(res); }
public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat) { arrStat = null; int count = arrHSrv.Length; IntPtr ptrStat; IntPtr ptrErr; // FIXME: check if ifSync valid? int hresult = ifSync.Read(src, count, arrHSrv, out ptrStat, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runErr = (int)ptrErr; int runStat = (int)ptrStat; if ((runErr == 0) || (runStat == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } arrStat = new OPCItemState[count]; for (int i = 0; i < count; i++) { // WORKAROUND !!! arrStat[i] = new OPCItemState(); arrStat[i].Error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)runStat); if (HRESULTS.Succeeded(arrStat[i].Error)) { short vt = Marshal.ReadInt16((IntPtr)(runStat + 16)); if (vt == (short)VarEnum.VT_ERROR) { arrStat[i].Error = Marshal.ReadInt32((IntPtr)(runStat + 24)); } arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(runStat + 4)); arrStat[i].Quality = Marshal.ReadInt16((IntPtr)(runStat + 12)); arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16)); VariantClear((IntPtr)(runStat + 16)); } else { arrStat[i].DataValue = null; } runStat += 32; } Marshal.DestroyStructure(ptrStat, typeof(tagOPCITEMSTATE)); Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public int Refresh2(OPCDATASOURCE dataSource, int transactionId) { return(DoComCall(ComObject, "IOpcAsyncIO3::Refresh2", () => { int dwCancelId; ComObject.Refresh2(dataSource, transactionId, out dwCancelId); return dwCancelId; }, dataSource, transactionId)); }
public int Refresh2(OPCDATASOURCE dataSource, int transactionId) { return DoComCall(ComObject, "IOpcAsyncIO3::Refresh2", () => { int dwCancelId; ComObject.Refresh2(dataSource, transactionId, out dwCancelId); return dwCancelId; }, dataSource, transactionId); }
public void Refresh2(OPCDATASOURCE sourceMode, int transactionID, out int cancelID) { cancelID = 0; try { ifAsync.Refresh2(sourceMode, transactionID, out cancelID); } catch { } }
/// <summary> /// Read values /// </summary> /// <param name="dataSource">Read data source</param> /// <param name="serverHandles">Server handles of OPC items to read</param> /// <param name="itemStates">OPC Item states array</param> /// <returns>Bool</returns> public bool Read(OPCDATASOURCE dataSource, int[] serverHandles, out OpcItemState[] itemStates) { ThrowIfDisposed(); itemStates = null; int hResult = opcSyncIO.Read(dataSource, serverHandles.Length, serverHandles, out IntPtr ptrStat, out IntPtr ptrErr); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } if ((ptrErr == IntPtr.Zero) || (ptrStat == IntPtr.Zero)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } try { itemStates = new OpcItemState[serverHandles.Length]; for (int i = 0; i < serverHandles.Length; i++) { itemStates[i] = new OpcItemState { Error = Extensions.ReadInt32(ptrErr, i), HandleClient = Marshal.ReadInt32(ptrStat, i * sizeOPCREADRESULT) }; if (HRESULTS.Succeeded(itemStates[i].Error)) { short vt = Marshal.ReadInt16(ptrStat, i * sizeOPCREADRESULT + 16); if (vt == (short)VarEnum.VT_ERROR) { itemStates[i].Error = Marshal.ReadInt32(ptrStat, i * sizeOPCREADRESULT + 24); } itemStates[i].TimeStamp = Marshal.ReadInt64(ptrStat, i * sizeOPCREADRESULT + 4); itemStates[i].Quality = Marshal.ReadInt16(ptrStat, i * sizeOPCREADRESULT + 12); itemStates[i].DataValue = Marshal.GetObjectForNativeVariant(IntPtr.Add(ptrStat, i * sizeOPCREADRESULT + 16)); Extensions.VariantClear(IntPtr.Add(ptrStat, i * sizeOPCREADRESULT + 16)); } else { itemStates[i].DataValue = null; } } } finally { Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); } return(hResult == HRESULTS.S_OK); }
public OPCITEMSTATE[] Read(OPCDATASOURCE dataSource, int[] serverHandles, out HRESULT[] errors) { HRESULT[] _errors = {}; OPCITEMSTATE[] results = DoComCall(ComObject, "IOpcSyncIO::Read", () => { OPCITEMSTATE[] ppItemValues; ComObject.Read(dataSource, serverHandles.Length, serverHandles, out ppItemValues, out _errors); return ppItemValues; }, dataSource, serverHandles.Length, serverHandles); errors = _errors; return results; }
public OPCITEMSTATE[] Read(OPCDATASOURCE dataSource, int[] serverHandles, out HRESULT[] errors) { HRESULT[] _errors = {}; OPCITEMSTATE[] results = DoComCall(ComObject, "IOpcSyncIO::Read", () => { OPCITEMSTATE[] ppItemValues; ComObject.Read(dataSource, serverHandles.Length, serverHandles, out ppItemValues, out _errors); return(ppItemValues); }, dataSource, serverHandles.Length, serverHandles); errors = _errors; return(results); }
void IOPCAsyncIO.Read(int dwConnection, OPCDATASOURCE dwSource, int dwCount, int[] phServer, out int pTransactionID, out IntPtr ppErrors) { var methodName = nameof(IOPCAsyncIO) + "." + nameof(IOPCAsyncIO.Read); try { var server = BeginComCall <IOPCAsyncIO>(methodName, true); server.Read(dwConnection, dwSource, dwConnection, phServer, out pTransactionID, out ppErrors); } finally { EndComCall(methodName); } }
void IOPCAsyncIO.Refresh(int dwConnection, OPCDATASOURCE dwSource, out int pTransactionID) { var methodName = nameof(IOPCAsyncIO) + "." + nameof(IOPCAsyncIO.Refresh); try { var server = BeginComCall <IOPCAsyncIO>(methodName, true); server.Refresh(dwConnection, dwSource, out pTransactionID); } finally { EndComCall(methodName); } }
void IOPCSyncIO.Read(OPCDATASOURCE dwSource, int dwCount, int[] phServer, out IntPtr ppItemValues, out IntPtr ppErrors) { var methodName = nameof(IOPCSyncIO) + "." + nameof(IOPCSyncIO.Read); try { var server = BeginComCall <IOPCSyncIO>(methodName, true); server.Read(dwSource, dwCount, phServer, out ppItemValues, out ppErrors); } finally { EndComCall(methodName); } }
public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat) { IntPtr ptr; IntPtr ptr2; arrStat = null; int length = arrHSrv.Length; int hresultcode = this.ifSync.Read(src, length, arrHSrv, out ptr, out ptr2); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } int num3 = (int)ptr2; int num4 = (int)ptr; if ((num3 == 0) || (num4 == 0)) { Marshal.ThrowExceptionForHR(-2147467260); } arrStat = new OPCItemState[length]; for (int i = 0; i < length; i++) { arrStat[i] = new OPCItemState(); arrStat[i].Error = Marshal.ReadInt32((IntPtr)num3); num3 += 4; arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)num4); if (HRESULTS.Succeeded(arrStat[i].Error)) { if (Marshal.ReadInt16((IntPtr)(num4 + 0x10)) == 10) { arrStat[i].Error = Marshal.ReadInt32((IntPtr)(num4 + 0x18)); } arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(num4 + 4)); arrStat[i].Quality = Marshal.ReadInt16((IntPtr)(num4 + 12)); arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(num4 + 0x10)); DUMMY_VARIANT.VariantClear((IntPtr)(num4 + 0x10)); } else { arrStat[i].DataValue = null; } num4 += 0x20; } Marshal.FreeCoTaskMem(ptr); Marshal.FreeCoTaskMem(ptr2); return(hresultcode == 0); }
/// <summary> /// 多标签同步读 /// </summary> /// <param name="sGroupName"></param> /// <param name="itemsV"></param> public object[] SyncReadItems(string sGroupName, int[] ItemsIdx, OPCDATASOURCE OpcDataSrc) { object[] TmpItemsVal = null; if (pOpcSrv.CheckOpcGroupExist(sGroupName) == true) { // OPCDATASOURCE.OPC_DS_CACHE, OPCDATASOURCE.OPC_DS_DEVICE TmpItemsVal = pOpcSrv.SyncReadItems(sGroupName, ItemsIdx, OpcDataSrc); } else { MessageBox.Show(sGroupName + "组没有创建!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); TmpItemsVal = null; } return(TmpItemsVal); }
/// <summary> /// This function reads a analog double tag value from OPC, read will be from Device, Return ERROR in case of any error /// </summary> /// <param name="tagName"></param> /// <returns></returns> public int ReadBooleanItem(String tagName) { ItemDef ItemData; ItemData = readGroup.Item(tagName); OPCItemState state = new OPCItemState(); // Read from device OPCDATASOURCE dsrc = OPCDATASOURCE.OPC_DS_DEVICE; int rtc = readGroup.Read(dsrc, ItemData, out state); if (HRESULTS.Succeeded(rtc)) // read from OPC server successful { if (state != null) { if (HRESULTS.Succeeded(state.Error)) // item read successful { if (Convert.ToBoolean(state.DataValue) == true) { return(1); } else { return(0); } } else // the item could not be read { // hf.LogException("Item cannot be readable: " + tagName); return(-10000); } } else // State not valid { // hf.LogException("Item cannot be readable due to State: " + tagName); return(-10000); } } else // OPC server read error { // hf.LogException("Item cannot be readable due to OPC: " + tagName); return(-10000); } }
int IOPCSyncIO.Read([In, MarshalAs(UnmanagedType.U4)] OPCDATASOURCE dwSource, [In] int dwCount, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int[] phServer, out IntPtr ppItemValues, out IntPtr ppErrors) { string currentTestMethod = Dottest.Framework.Stubs.CurrentTestMethod.Name; if (currentTestMethod.Equals("TestErr_Read01")) { ppItemValues = new IntPtr(1); ppErrors = new IntPtr(0); return(HRESULTS.S_OK); } else if (currentTestMethod.Equals("TestErr_Read02")) { ppItemValues = new IntPtr(1); ppErrors = new IntPtr(0); return(HRESULTS.OPC_E_NOTFOUND); } else { ppItemValues = new IntPtr(1); ppErrors = new IntPtr(1); return(HRESULTS.S_OK); } }
/// <summary> /// This function reads a tag value from OPC, read will be from Device, Return ERROR in case of any error /// </summary> /// <param name="tagName"></param> /// <returns></returns> public string ReadStringItem(String tagName) { ItemDef ItemData = new ItemDef(); ItemData = readGroup.Item(tagName); OPCItemState state = new OPCItemState(); // Read from device OPCDATASOURCE dsrc = OPCDATASOURCE.OPC_DS_CACHE; //?????????? int rtc = readGroup.Read(dsrc, ItemData, out state); if (HRESULTS.Succeeded(rtc)) // read from OPC server successful { if (state != null) { if (HRESULTS.Succeeded(state.Error)) // item read successful { return(state.DataValue.ToString()); } else // the item could not be read { // hf.LogException("Item cannot be readable: " + tagName); return("ERROR"); } } else // State not valid { // hf.LogException("Item cannot be readable due to state: " + tagName); return("ERROR"); } } else // OPC server read error { // hf.LogException("Item cannot be readable due to OPC: " + tagName); return("ERROR"); } }
public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat) { arrStat = null; int count = arrHSrv.Length; IntPtr ptrStat; IntPtr ptrErr; // FIXME: check if ifSync valid? int hresult = ifSync.Read(src, count, arrHSrv, out ptrStat, out ptrErr); if (HRESULTS.Failed(hresult)) Marshal.ThrowExceptionForHR(hresult); int runErr = (int)ptrErr; int runStat = (int)ptrStat; if ((runErr == 0) || (runStat == 0)) Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); arrStat = new OPCItemState[count]; for (int i = 0; i < count; i++) { // WORKAROUND !!! arrStat[i] = new OPCItemState(); arrStat[i].Error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)runStat); if (HRESULTS.Succeeded(arrStat[i].Error)) { short vt = Marshal.ReadInt16((IntPtr)(runStat + 16)); if (vt == (short)VarEnum.VT_ERROR) arrStat[i].Error = Marshal.ReadInt32((IntPtr)(runStat + 24)); arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(runStat + 4)); arrStat[i].Quality = Marshal.ReadInt16((IntPtr)(runStat + 12)); arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16)); VariantClear((IntPtr)(runStat + 16)); } else arrStat[i].DataValue = null; runStat += 32; } Marshal.DestroyStructure(ptrStat, typeof(tagOPCITEMSTATE)); Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); return hresult == HRESULTS.S_OK; }
void IOPCAsyncIO2.Refresh2([In, MarshalAs(UnmanagedType.U4)] OPCDATASOURCE dwSource, [In] int dwTransactionID, out int pdwCancelID) { pdwCancelID = 1; }
public void Refresh2(OPCDATASOURCE sourceMode, int transactionID, out int cancelID) { this.ifAsync.Refresh2(sourceMode, transactionID, out cancelID); }
public void Refresh2( OPCDATASOURCE sourceMode, int transactionID, out int cancelID ) { ifAsync.Refresh2( sourceMode, transactionID, out cancelID ); }
/// <summary> /// Refresh 2 /// </summary> /// <param name="dataSource">Source mode</param> /// <param name="transactionID">Transaction ID</param> /// <param name="cancelID">Cancel ID</param> public void Refresh2(OPCDATASOURCE dataSource, int transactionID, out int cancelID) { ThrowIfDisposed(); opcAsyncIO.Refresh2(dataSource, transactionID, out cancelID); }