/// <summary> /// Sets whether data change callbacks are enabled. /// </summary> public override void SetEnabled(bool enabled) { if (subscription_ == null) { throw new NotConnectedException(); } lock (lock_) { string methodName = "IOPCAsyncIO2.SetEnable"; try { IOPCAsyncIO2 subscription = BeginComCall <IOPCAsyncIO2>(methodName, true); subscription.SetEnable((enabled) ? 1 : 0); } catch (Exception e) { ComCallError(methodName, e); throw Utilities.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } } }
public void Release() { try { if (dwCookie != 0) { //pIConnectionPoint.Unadvise(dwCookie); dwCookie = 0; } // Free the unmanaged COM memory //Marshal.ReleaseComObject(pIConnectionPoint); pIConnectionPoint = null; //Marshal.ReleaseComObject(pIConnectionPointContainer); pIConnectionPointContainer = null; if (pIOPCAsyncIO2 != null) { //Marshal.ReleaseComObject(pIOPCAsyncIO2); pIOPCAsyncIO2 = null; } if (pIOPCGroupStateMgt != null) { //Marshal.ReleaseComObject(pIOPCGroupStateMgt); pIOPCGroupStateMgt = null; } if (pobjGroup1 != null) { //Marshal.ReleaseComObject(pobjGroup1); pobjGroup1 = null; } } catch { } }
/// <summary> /// Gets whether data change callbacks are enabled. /// </summary> public override bool GetEnabled() { if (subscription_ == null) { throw new NotConnectedException(); } lock (lock_) { string methodName = "IOPCAsyncIO2.GetEnable"; try { int enabled = 0; IOPCAsyncIO2 subscription = BeginComCall <IOPCAsyncIO2>(methodName, true); subscription.GetEnable(out enabled); return(enabled != 0); } catch (Exception e) { ComCallError(methodName, e); throw Utilities.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } } }
/// <summary> /// Tells the server to send an data change update for all subscription items containing the cached values. /// </summary> public override void Refresh() { if (subscription_ == null) { throw new NotConnectedException(); } lock (lock_) { string methodName = "IOPCAsyncIO2.Refresh2"; try { int cancelID = 0; IOPCAsyncIO2 subscription = BeginComCall <IOPCAsyncIO2>(methodName, true); subscription.Refresh2(OPCDATASOURCE.OPC_DS_CACHE, ++_counter, out cancelID); } catch (Exception e) { ComCallError(methodName, e); throw Utilities.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } } }
private void GetInterfaces() { m_IfItems = (IOPCItemMgt)m_IfMgt; m_IfSync = (IOPCSyncIO)m_IfMgt; m_IfAsync = (IOPCAsyncIO2)m_IfMgt; m_PointContainer = (System.Runtime.InteropServices.ComTypes.IConnectionPointContainer)m_IfMgt; }
/// <summary> /// 增加一个group /// </summary> /// <param name="Form_Main"></param> /// <returns></returns> public string PLCGroupAdd(object Form_Main) { string errText = string.Empty; Int32 dwRequestedUpdateRate = 250; Int32 pRevUpdateRate; float deadband = 0; int TimeBias = 0; GCHandle hTimeBias, hDeadband; hTimeBias = GCHandle.Alloc(TimeBias, GCHandleType.Pinned); hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned); Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID; try { ServerObj.AddGroup("MyOPCGroup1",//组对象 0, dwRequestedUpdateRate, hClientGroup, hTimeBias.AddrOfPinnedObject(), hDeadband.AddrOfPinnedObject(), LOCALE_ID, out pSvrGroupHandle, out pRevUpdateRate, ref iidRequiredInterface, out MyobjGroup1); IOPCAsyncIO2Obj = (IOPCAsyncIO2)MyobjGroup1; //Query interface for Async calls on group object IOPCGroupStateMgtObj = (IOPCGroupStateMgt)MyobjGroup1; pIConnectionPointContainer = (IConnectionPointContainer)MyobjGroup1; //定义特定组的异步调用连接 Guid iid = typeof(IOPCDataCallback).GUID; // Establish Callback for all async operations pIConnectionPointContainer.FindConnectionPoint(ref iid, out pIConnectionPoint); // Creates a connection between the OPC servers's connection point and this client's sink (the callback object) pIConnectionPoint.Advise(Form_Main, out dwCookie); } catch (Exception ex) { errText = ex.Message.ToString(); } return(errText); }
/// <summary> /// 断开PLC的连接 /// </summary> /// <returns></returns> public string DisConnection() { string errText = string.Empty; try { if (dwCookie != 0) { pIConnectionPoint.Unadvise(dwCookie); dwCookie = 0; } // Free unmanaged code Marshal.ReleaseComObject(pIConnectionPoint); pIConnectionPoint = null; Marshal.ReleaseComObject(pIConnectionPointContainer); pIConnectionPointContainer = null; if (IOPCAsyncIO2Obj != null) { Marshal.ReleaseComObject(IOPCAsyncIO2Obj); IOPCAsyncIO2Obj = null; } ServerObj.RemoveGroup(pSvrGroupHandle, 0); if (IOPCGroupStateMgtObj != null) { Marshal.ReleaseComObject(IOPCGroupStateMgtObj); IOPCGroupStateMgtObj = null; } if (MyobjGroup1 != null) { Marshal.ReleaseComObject(MyobjGroup1); MyobjGroup1 = null; } if (ServerObj != null) { Marshal.ReleaseComObject(ServerObj); ServerObj = null; } } catch (System.Exception error) { errText = error.Message.ToString().Trim(); } return(errText); }
/// <summary> /// 释放对象 /// </summary> public void Dispose() { try { if (dwCookie != 0) { ConnectionPoint.Unadvise(dwCookie); dwCookie = 0; } } catch (Exception) { dwCookie = 0; } Marshal.ReleaseComObject(ConnectionPoint); ConnectionPoint = null; Marshal.ReleaseComObject(connectionPointContainer); connectionPointContainer = null; if (oPCSyncIO != null) { Marshal.ReleaseComObject(oPCSyncIO);//减与指定的 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。 oPCSyncIO = null; } if (oPCAsyncIO != null) { Marshal.ReleaseComObject(oPCAsyncIO);//减与指定的 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。 oPCAsyncIO = null; } if (oPCGroupStateMgt != null) { Marshal.ReleaseComObject(oPCGroupStateMgt); oPCGroupStateMgt = null; } if (oPCItemMgt != null) { Marshal.ReleaseComObject(oPCItemMgt); oPCItemMgt = null; } if (ObjGroup != null) { Marshal.ReleaseComObject(ObjGroup); ObjGroup = null; } }
public bool AddGroup(object Form_Main) { int dwRequestedUpdateRate = 0x3e8; int hClientGroup = 1; float num4 = 0f; int num5 = 0; GCHandle handle = GCHandle.Alloc(num5, GCHandleType.Pinned); GCHandle handle2 = GCHandle.Alloc(num4, GCHandleType.Pinned); Guid gUID = typeof(IOPCItemMgt).GUID; if (!this.isConnected && !this.Connect()) { return(false); } try { int num3; this.ServerObj.AddGroup("OPCGroup", 0, dwRequestedUpdateRate, hClientGroup, handle.AddrOfPinnedObject(), handle2.AddrOfPinnedObject(), 0x804, out this.pSvrGroupHandle, out num3, ref gUID, out this.GroupObj); this.IOPCAsyncIO2Obj = (IOPCAsyncIO2)this.GroupObj; this.IOPCGroupStateMgtObj = (IOPCGroupStateMgt)this.GroupObj; this.pIConnectionPointContainer = (IConnectionPointContainer)this.GroupObj; Guid riid = typeof(IOPCDataCallback).GUID; this.pIConnectionPointContainer.FindConnectionPoint(ref riid, out this.pIConnectionPoint); this.pIConnectionPoint.Advise(Form_Main, out this.dwCookie); this.isAddGroup = true; } catch (Exception exception) { this.isAddGroup = false; MessageBox.Show($"创建组对象时出错:-{exception.Message}", "建组出错", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { if (handle2.IsAllocated) { handle2.Free(); } if (handle.IsAllocated) { handle.Free(); } } return(this.isAddGroup); }
public string DisConnection() { string str = string.Empty; try { if (this.dwCookie != 0) { this.pIConnectionPoint.Unadvise(this.dwCookie); this.dwCookie = 0; } Marshal.ReleaseComObject(this.pIConnectionPoint); this.pIConnectionPoint = null; Marshal.ReleaseComObject(this.pIConnectionPointContainer); this.pIConnectionPointContainer = null; if (this.IOPCAsyncIO2Obj != null) { Marshal.ReleaseComObject(this.IOPCAsyncIO2Obj); this.IOPCAsyncIO2Obj = null; } this.ServerObj.RemoveGroup(this.pSvrGroupHandle, 0); if (this.IOPCGroupStateMgtObj != null) { Marshal.ReleaseComObject(this.IOPCGroupStateMgtObj); this.IOPCGroupStateMgtObj = null; } if (this.GroupObj != null) { Marshal.ReleaseComObject(this.GroupObj); this.GroupObj = null; } if (this.ServerObj != null) { Marshal.ReleaseComObject(this.ServerObj); this.ServerObj = null; } } catch (Exception exception) { str = exception.Message.ToString().Trim(); } return(str); }
public void Remove(bool bForce) { try { int num; if (this.callbackcpoint != null) { if (this.callbackcookie != 0) { this.callbackcpoint.Unadvise(this.callbackcookie); this.callbackcookie = 0; } num = Marshal.ReleaseComObject(this.callbackcpoint); this.callbackcpoint = null; } this.cpointcontainer = null; this.ifItems = null; this.ifSync = null; this.ifAsync = null; if (this.ifMgt != null) { num = Marshal.ReleaseComObject(this.ifMgt); this.ifMgt = null; } if (this.ifServer != null) { if (!this.state.Public) { this.ifServer.RemoveGroup(this.state.HandleServer, bForce); } this.ifServer = null; } this.state.HandleServer = 0; } catch (Exception exception) { LogHelper.Error("OPCTrendLib.OPCData.OpcGroup", "Remove", exception); Console.WriteLine(exception.ToString()); } }
public int ASyncRead() { IOPCAsyncIO2 m_pASyncIO = null; int iRet = 0; try { //IntPtr iptrItemValue = IntPtr.Zero; IntPtr iptrErrors = IntPtr.Zero; uint dwTransactionID = GroupID; uint pdwCancelID; m_pASyncIO = (IOPCAsyncIO2)m_pItemMgt; m_pASyncIO.Read((uint)m_dwReadCount, ref m_hItems[0], dwTransactionID, out pdwCancelID, out iptrErrors); Marshal.Copy(iptrErrors, m_hRes, 0, m_dwReadCount); } catch (ApplicationException ex) { Console.Out.WriteLine(ex.Message); } return(iRet); }
private void InitReqIOInterfaces() { try { //Query interface for async calls on group object pIOPCAsyncIO2 = (IOPCAsyncIO2)pobjGroup1; pIOPCSyncIO = (IOPCSyncIO)pobjGroup1; pIOPCGroupStateMgt = (IOPCGroupStateMgt)pobjGroup1; // Query interface for callbacks on group object pIConnectionPointContainer = (IConnectionPointContainer)pobjGroup1; // Establish Callback for all async operations Guid iid = typeof(IOPCDataCallback).GUID; pIConnectionPointContainer.FindConnectionPoint(ref iid, out pIConnectionPoint); // Creates a connection between the OPC servers's connection point and // this client's sink (the callback object). pIConnectionPoint.Advise(this, out dwCookie); } catch (System.Exception error) // catch for group adding { } }
public void Remove( bool bForce ) { if( ! (callbackcpoint == null) ) { if( callbackcookie != 0 ) { callbackcpoint.Unadvise( callbackcookie ); callbackcookie = 0; } int rc = Marshal.ReleaseComObject( callbackcpoint ); callbackcpoint = null; } cpointcontainer = null; ifItems = null; ifSync = null; ifAsync = null; if( ! (ifMgt == null) ) { int rc = Marshal.ReleaseComObject( ifMgt ); ifMgt = null; } if( ! (ifServer == null) ) { if( ! state.Public ) ifServer.RemoveGroup( state.HandleServer, bForce ); ifServer = null; } state.HandleServer = 0; }
/// <summary> /// Begins an asynchronous write for a set of items using DA2.0 interfaces. /// </summary> protected override OpcItemResult[] BeginWrite( OpcItem[] itemIDs, TsCDaItemValue[] items, int requestID, out int cancelID) { cancelID = 0; ArrayList validItems = new ArrayList(); ArrayList validValues = new ArrayList(); // construct initial result list. OpcItemResult[] results = new OpcItemResult[itemIDs.Length]; for (int ii = 0; ii < itemIDs.Length; ii++) { results[ii] = new OpcItemResult(itemIDs[ii]); results[ii].Result = OpcResult.S_OK; results[ii].DiagnosticInfo = null; if (items[ii].QualitySpecified || items[ii].TimestampSpecified) { results[ii].Result = OpcResult.Da.E_NO_WRITEQT; results[ii].DiagnosticInfo = null; continue; } validItems.Add(results[ii]); validValues.Add(Utilities.Interop.GetVARIANT(items[ii].Value)); } // check if any valid items exist. if (validItems.Count == 0) { return(results); } string methodName = "IOPCAsyncIO2.Write"; try { // initialize input parameters. int[] serverHandles = new int[validItems.Count]; for (int ii = 0; ii < validItems.Count; ii++) { serverHandles[ii] = (int)((OpcItemResult)validItems[ii]).ServerHandle; } // write to sever. IntPtr pErrors = IntPtr.Zero; IOPCAsyncIO2 subscription = BeginComCall <IOPCAsyncIO2>(methodName, true); subscription.Write( validItems.Count, serverHandles, (object[])validValues.ToArray(typeof(object)), requestID, out cancelID, out pErrors); // unmarshal results. int[] errors = Utilities.Interop.GetInt32s(ref pErrors, validItems.Count, true); // create result list. for (int ii = 0; ii < validItems.Count; ii++) { OpcItemResult result = (OpcItemResult)validItems[ii]; result.Result = Utilities.Interop.GetResultId(errors[ii]); result.DiagnosticInfo = null; // convert COM code to unified DA code. if (errors[ii] == Result.E_BADRIGHTS) { results[ii].Result = new OpcResult(OpcResult.Da.E_READONLY, Result.E_BADRIGHTS); } } } catch (Exception e) { ComCallError(methodName, e); throw Utilities.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } // return results. return(results); }
// -------------------------- private --------------------- private void getinterfaces() { ifItems = (IOPCItemMgt) ifMgt; ifSync = (IOPCSyncIO) ifMgt; ifAsync = (IOPCAsyncIO2) ifMgt; cpointcontainer = (UCOMIConnectionPointContainer) ifMgt; }
/// <summary> /// Begins an asynchronous read of a set of items using DA2.0 interfaces. /// </summary> protected override OpcItemResult[] BeginRead( OpcItem[] itemIDs, TsCDaItem[] items, int requestID, out int cancelID) { string methodName = "IOPCAsyncIO2.Read"; try { // marshal input parameters. int[] serverHandles = new int[itemIDs.Length]; for (int ii = 0; ii < itemIDs.Length; ii++) { serverHandles[ii] = (int)itemIDs[ii].ServerHandle; } // initialize output parameters. IntPtr pErrors = IntPtr.Zero; IOPCAsyncIO2 subscription = BeginComCall <IOPCAsyncIO2>(methodName, true); subscription.Read( itemIDs.Length, serverHandles, requestID, out cancelID, out pErrors); // unmarshal output parameters. int[] errors = Utilities.Interop.GetInt32s(ref pErrors, itemIDs.Length, true); // create item results. OpcItemResult[] results = new OpcItemResult[itemIDs.Length]; for (int ii = 0; ii < itemIDs.Length; ii++) { results[ii] = new OpcItemResult(itemIDs[ii]); results[ii].Result = Utilities.Interop.GetResultId(errors[ii]); results[ii].DiagnosticInfo = null; // convert COM code to unified DA code. if (errors[ii] == Result.E_BADRIGHTS) { results[ii].Result = new OpcResult(OpcResult.Da.E_WRITEONLY, Result.E_BADRIGHTS); } } // return results. return(results); } catch (Exception e) { ComCallError(methodName, e); throw Utilities.Interop.CreateException(methodName, e); } finally { EndComCall(methodName); } }
public bool WriteChannel(int channelHandle, object value) { IOPCAsyncIO2 asyncIO = null; IOPCSyncIO syncIO = null; lock (this) { if (serverWriteCapabilities == ServerWriteCapabilities.Unknown) { try { asyncIO = (IOPCAsyncIO2)group; if (asyncIO != null) { serverWriteCapabilities = ServerWriteCapabilities.Async; } } catch (System.Runtime.InteropServices.COMException) { } if (asyncIO == null) { try { syncIO = (IOPCSyncIO)group; if (syncIO != null) { serverWriteCapabilities = ServerWriteCapabilities.Sync; } } catch (System.Runtime.InteropServices.COMException) { } } if (asyncIO == null && syncIO == null) { serverWriteCapabilities = ServerWriteCapabilities.None; } } try { switch (serverWriteCapabilities) { case ServerWriteCapabilities.Async: syncIO = (IOPCSyncIO)group; break; case ServerWriteCapabilities.Sync: syncIO = (IOPCSyncIO)group; break; default: return(false); } } catch (System.Runtime.InteropServices.COMException) { } if (asyncIO == null && syncIO == null) { return(false); } if (asyncIO != null) { int cancelID; IntPtr ppErrors; asyncIO.Write(1, new int[] { channelHandle }, new object[] { value }, 0, out cancelID, out ppErrors); Marshal.FreeCoTaskMem(ppErrors); } else if (syncIO != null) { IntPtr ppErrors; syncIO.Write(1, new int[] { channelHandle }, new object[] { value }, out ppErrors); Marshal.FreeCoTaskMem(ppErrors); } return(true); } }