/// <summary> /// Returns a list of servers that support the specified specification. /// </summary> /// <param name="specification">Unique identifier for one OPC specification.</param> /// <param name="discoveryServerUrl">The URL of the discovery server to be used.</param> /// <param name="identity">The user identity to use when discovering the servers.</param> /// <returns>Returns a list of found OPC servers.</returns> public static List <OpcServer> GetServers(OpcSpecification specification, string discoveryServerUrl, OpcUserIdentity identity) { List <OpcServer> serverList = new List <OpcServer>(); bool dcomDiscovery = false; if (specification == OpcSpecification.OPC_AE_10) { dcomDiscovery = true; } if (specification == OpcSpecification.OPC_DA_20 || specification == OpcSpecification.OPC_DA_30) { dcomDiscovery = true; } if (specification == OpcSpecification.OPC_HDA_10) { dcomDiscovery = true; } if (specification == null) { return(serverList); } else if (dcomDiscovery) { if (_discovery == null || _hostName != discoveryServerUrl) { if (_discovery != null) { _discovery.Dispose(); } _hostName = discoveryServerUrl; _discovery = new Technosoftware.DaAeHdaClient.Com.ServerEnumerator(); } OpcServer[] servers = _discovery.GetAvailableServers(specification); if (servers != null) { foreach (OpcServer server in servers) { serverList.Add(server); } } } return(serverList); }
/// <summary> /// Connects to the specified COM server. /// </summary> public static object Connect(OpcUrl url, OpcConnectData connectData) { // parse path to find prog id and clsid. string progID = url.Path; string clsid = null; int index = url.Path.IndexOf('/'); if (index >= 0) { progID = url.Path.Substring(0, index); clsid = url.Path.Substring(index + 1); } // look up prog id if clsid not specified in the url. Guid guid; if (clsid == null) { // use OpcEnum to lookup the prog id. guid = new ServerEnumerator().CLSIDFromProgID(progID, url.HostName, connectData); // check if prog id is actually a clsid string. if (guid == Guid.Empty) { try { guid = new Guid(progID); } catch { throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID)); } } } // convert clsid string to a guid. else { try { guid = new Guid(clsid); } catch { throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID)); } } // get the credentials. OpcUserIdentity credentials = (connectData != null) ? connectData.UserIdentity : null; // instantiate the server using CoCreateInstanceEx. if (connectData == null || connectData.LicenseKey == null) { try { return(Utilities.Interop.CreateInstance(guid, url.HostName, credentials)); } catch (Exception e) { throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e); } } // instantiate the server using IClassFactory2. else { try { return(null); //return Technosoftware.DaAeHdaClient.Utilities.Interop.CreateInstanceWithLicenseKey(guid, url.HostName, credentials, connectData.LicenseKey); } catch (Exception e) { throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e); } } }