/// <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>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 GetLocaleID(out int p_LocalID) { m_IfCommon.GetLocaleID(out p_LocalID); }