/// <summary> /// Fetches the available locales. /// </summary> public int[] QueryAvailableLocales() { string methodName = "IOPCCommon.QueryAvailableLocales"; try { IOPCCommon server = BeginComCall <IOPCCommon>(methodName, true); // query for available locales. int count = 0; IntPtr pLocaleIDs = IntPtr.Zero; server.QueryAvailableLocaleIDs(out count, out pLocaleIDs); // unmarshal results. return(ComUtils.GetInt32s(ref pLocaleIDs, count, true)); } catch (Exception e) { ComCallError(methodName, e); return(null); } finally { EndComCall(methodName); } }
public void Disconnect() { if (m_ShutdownPoint != null) { if (m_ShutdownCookie != 0) { m_ShutdownPoint.Unadvise(m_ShutdownCookie); m_ShutdownCookie = 0; } Marshal.ReleaseComObject(m_ShutdownPoint); m_ShutdownPoint = null; } m_PointContainer = null; m_IfBrowse = null; m_IfItmProps = null; m_IfCommon = null; m_IfServer = null; if (m_OPCServerObj != null) { Marshal.ReleaseComObject(m_OPCServerObj); m_OPCServerObj = null; } }
public void RemoteConnect(string p_ClsIDOPCServer, string p_MachineName) { Disconnect(); Type l_TypeOfOPCServer = Type.GetTypeFromProgID(p_ClsIDOPCServer, p_MachineName); if (l_TypeOfOPCServer == null) { Marshal.ThrowExceptionForHR(HResults.OPC_E_NOTFOUND); } m_OPCServerObj = Activator.CreateInstance(l_TypeOfOPCServer); m_IfServer = (IOPCServer)m_OPCServerObj; if (m_IfServer == null) { Marshal.ThrowExceptionForHR(HResults.CONNECT_E_NOCONNECTION); } // Connect all interfaces m_IfCommon = (IOPCCommon)m_OPCServerObj; m_IfBrowse = (IOPCBrowseServerAddressSpace)m_IfServer; m_IfItmProps = (IOPCItemProperties)m_IfServer; m_PointContainer = (IConnectionPointContainer)m_OPCServerObj; AdviseIOPCShutdown(); }
/*------------------------------------------------------ * Connect OPC Server * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool Connect(DEF_OPCDA OpcdaVer, string sNodeName, string sSvrName, string sGrpName, int iUpdateRate) { if (m_OPCServer != null) { return(true); } m_OpcdaVer = OpcdaVer; try { // instantiate the serverlist using CoCreateInstanceEx. IOPCServerList svrList = (IOPCServerList)CreateInstance(CLSID_SERVERLIST, sNodeName); Guid clsidList; svrList.CLSIDFromProgID(sSvrName, out clsidList); m_OPCServer = (IOPCServer)CreateInstance(clsidList, sNodeName); if (m_OPCServer != null) { // 2011/11/14 シャットダウンイベントを受けれるようにする ( IConnectionPointContainer OPCConnPointCntnr = (IConnectionPointContainer)m_OPCServer; Guid guidShutdown = Marshal.GenerateGuidForType(typeof(IOPCShutdown)); OPCConnPointCntnr.FindConnectionPoint(ref guidShutdown, out m_OpcShutdownConnectionPoint); m_OpcShutdownConnectionPoint.Advise(this, out m_iShutdownConnectionCookie); // 2011/11/14 シャットダウンイベントを受けれるようにする ) if (AddGroup(sGrpName, iUpdateRate)) { IOPCCommon m_com = (IOPCCommon)m_OPCServer; m_com.SetClientName("TestClient"); m_bConnect = true; Marshal.ReleaseComObject(svrList); // 1.0.0.5 10/06/21 Kishimoto Release Com Object svrList = null; // 1.0.0.5 10/06/21 Kishimoto Release Com Object return(true); } } Marshal.ReleaseComObject(svrList); // 1.0.0.5 10/06/21 Kishimoto Release Com Object svrList = null; // 1.0.0.5 10/06/21 Kishimoto Release Com Object MessageBox.Show("Cannot connect OPC Server.", "Connect"); return(false); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "Connect"); return(false); } }
public void Disconnect() { int num; try { if (this.shutdowncpoint != null) { if (this.shutdowncookie != 0) { try { this.shutdowncpoint.Unadvise(this.shutdowncookie); } catch (Exception exception) { LogHelper.Error("OPCTrendLib.OPCData.OPCServer", "Disconnect", exception); } this.shutdowncookie = 0; } num = Marshal.ReleaseComObject(this.shutdowncpoint); this.shutdowncpoint = null; } this.cpointcontainer = null; this.ifBrowse = null; this.ifItmProps = null; this.ifCommon = null; this.ifServer = null; if (this.OPCserverObj != null) { num = Marshal.ReleaseComObject(this.OPCserverObj); this.OPCserverObj = null; } } catch (Exception exception) { LogHelper.Error("OPCTrendLib.OPCData.OPCServer", "Disconnect", exception); } finally { if (this.shutdowncpoint != null) { this.shutdowncpoint = null; } if (this.OPCserverObj != null) { this.OPCserverObj = null; } } }
/// <summary>Establishes the connection to the S韒aticNET OPC Server.</summary> /// <param name="localeID"> The locale ID returned from IOPCCommon the OPC-Server </param> /// <exception cref="InvalidOperationException"> if the opcserver is already connected </exception> /// <exception cref="Exception">throws and forwards any exception (with short error description)</exception> public void connectOPCServer(string progID) { if (!m_connected) { Type typeofOPCserver = null; try { //Get the Type from ProgID.Text typeofOPCserver = Type.GetTypeFromProgID(progID); string strDummy = "Couldn't find a server with the ProgID '" + progID + "'!"; if (typeofOPCserver == null) { throw new Exception(strDummy); } // Must be freed with Marshal.ReleaseComObject(myOPCServer) m_OPCServer = (IOPCServer)Activator.CreateInstance(typeofOPCserver); if (m_OPCServer == null) { strDummy = "CreateInstance failed - server not connected!"; throw new Exception(strDummy); } // Don't free it - we don't AddRef it!!! m_OPCCommon = (IOPCCommon)m_OPCServer; if (m_OPCCommon == null) { strDummy = "No 'IOPCCommon' Interface found!"; throw new Exception(strDummy); } m_OPCCommon.GetLocaleID(out m_LocaleID); m_connected = true; } catch (Exception) { throw; } } else { throw new InvalidOperationException("Already connected to OPC-Server!"); } }
/// <summary> /// Sets the current locale. /// </summary> public void SetLocale(int localeId) { string methodName = "IOPCCommon.SetLocaleID"; try { IOPCCommon server = BeginComCall <IOPCCommon>(methodName, true); server.SetLocaleID(localeId); } catch (Exception e) { ComCallError(methodName, e); } finally { EndComCall(methodName); } }
public void Connect(string clsidOPCserver) { this.Disconnect(); Type typeFromProgID = Type.GetTypeFromProgID(clsidOPCserver); if (typeFromProgID == null) { Marshal.ThrowExceptionForHR(-1073479663); } this.OPCserverObj = Activator.CreateInstance(typeFromProgID); this.ifServer = (IOPCServer)this.OPCserverObj; if (this.ifServer == null) { Marshal.ThrowExceptionForHR(-2147220992); } this.ifCommon = (IOPCCommon)this.OPCserverObj; this.ifBrowse = (IOPCBrowseServerAddressSpace)this.ifServer; this.ifItmProps = (IOPCItemProperties)this.ifServer; this.cpointcontainer = (IConnectionPointContainer)this.OPCserverObj; this.AdviseIOPCShutdown(); }
/// <summary> /// Fetches the error string from the server. /// </summary> public string GetErrorString(int error) { string methodName = "IOPCCommon.GetErrorString"; try { IOPCCommon server = BeginComCall <IOPCCommon>(methodName, true); string ppString = null; server.GetErrorString(error, out ppString); return(ppString); } catch (Exception e) { ComCallError(methodName, e); return(null); } finally { EndComCall(methodName); } }
/// <summary> /// Sets the current locale. /// </summary> public void SetLocale(IList <string> preferredLocales) { // select the best locale. int localeId = SelectLocaleId(m_availableLocaleIds, preferredLocales); string methodName = "IOPCCommon.SetLocaleID"; try { IOPCCommon server = BeginComCall <IOPCCommon>(methodName, true); server.SetLocaleID(localeId); } catch (Exception e) { ComCallError(methodName, e); } finally { EndComCall(methodName); } }
/// <summary>adds a IOPCGroupStateMgt-object to the OPC-server</summary> /// <param name="commonInterface">the common interface</param> /// <returns> the revisedUpdateRate for further use </returns> /// <exception cref="Exception">throws and forwards any exception (with short error description)</exception> public int addOPCGroup(IOPCCommon commonInterface) { // initialize arguments. Guid iid = Guid.Empty; object objGroup = null; IntPtr pTimeBias = IntPtr.Zero; IntPtr pDeadband = IntPtr.Zero; if (m_disposed) { throw new NullReferenceException("This object has been disposed!"); } m_revUpdateRate = 0; try { // get the default locale for the server. commonInterface.GetLocaleID(out m_LocaleID); iid = typeof(IOPCGroupStateMgt).GUID; if (iid == Guid.Empty) { throw new Exception("Could not get the interface 'IOPCGroupStateMgt'!"); } // Add a group object "m_OPCGroup" and query for interface IOPCItemMgt // Parameter as following: // [in] active, so do OnDataChange callback // [in] Request this Update Rate from Server // [in] Client Handle, not necessary in this sample // [in] No time interval to system UTC time // [in] No Deadband, so all data changes are reported // [in] Server uses english language to for text values // [out] Server handle to identify this group in later calls // [out] The answer from Server to the requested Update Rate // [in] requested interface type of the group object // [out] pointer to the requested interface m_OPCServer.AddGroup(m_grpName, Convert.ToInt32(m_isActive), m_reqUpdateRate, m_grpClntHndl, pTimeBias, pDeadband, m_LocaleID, out m_grpSrvHndl, out m_revUpdateRate, ref iid, out objGroup); if (objGroup == null) { string strMsg = "Couldn't add the group '" + m_grpName + "' to the server!"; throw new Exception(strMsg); } // Get our reference from the created group m_OPCGroupStateMgt = (IOPCGroupStateMgt)objGroup; if (m_OPCGroupStateMgt == null) { throw new Exception("Could not get the interface 'IOPCGroupStateMgt'!"); } } catch (Exception) { throw; } return(m_revUpdateRate); }
public void Disconnect() { if( ! (shutdowncpoint == null) ) { if( shutdowncookie != 0 ) { shutdowncpoint.Unadvise( shutdowncookie ); shutdowncookie = 0; } int rc = Marshal.ReleaseComObject( shutdowncpoint ); shutdowncpoint = null; } cpointcontainer = null; ifBrowse = null; ifItmProps = null; ifCommon = null; ifServer = null; if( ! (OPCserverObj == null) ) { int rc = Marshal.ReleaseComObject( OPCserverObj ); OPCserverObj = null; } }
public void Connect( string clsidOPCserver ) { Disconnect(); Type typeofOPCserver = Type.GetTypeFromProgID( clsidOPCserver ); if( typeofOPCserver == null ) Marshal.ThrowExceptionForHR( HRESULTS.OPC_E_NOTFOUND ); OPCserverObj = Activator.CreateInstance( typeofOPCserver ); ifServer = (IOPCServer) OPCserverObj; if( ifServer == null ) Marshal.ThrowExceptionForHR( HRESULTS.CONNECT_E_NOCONNECTION ); // connect all interfaces ifCommon = (IOPCCommon) OPCserverObj; ifBrowse = (IOPCBrowseServerAddressSpace) ifServer; ifItmProps = (IOPCItemProperties) ifServer; cpointcontainer = (UCOMIConnectionPointContainer) OPCserverObj; AdviseIOPCShutdown(); }