public static List <OPCServer> BrowseServers(string hostname, NetworkCredential networkCredential = null, bool DAVersion3 = false) { List <OPCServer> servers = new List <OPCServer>(); Guid clsidcat; if (DAVersion3) { clsidcat = new Guid("{CC603642-66D7-48F1-B69A-B625E73652D7}"); //OPC Data Access Servers Version 3. } else { clsidcat = new Guid("{63D5F432-CFE4-11D1-B2C8-0060083BA1FB}"); //OPC Data Access Servers Version 2. } Guid clsidenum = new Guid("{13486D51-4821-11D2-A494-3CB306C10000}"); //OPCEnum.exe try { IOPCServerList2 serverList = (IOPCServerList2)Internal.Interop.CreateInstance(clsidenum, hostname, networkCredential); IOPCEnumGUID pIOPCEnumGuid; serverList.EnumClassesOfCategories(1, ref clsidcat, 0, ref clsidcat, out pIOPCEnumGuid); string pszProgID; string pszUserType; string pszVerIndProgID; Guid guid = new Guid(); int nServerCnt = 0; uint iRetSvr; pIOPCEnumGuid.Next(1, out guid, out iRetSvr); while (iRetSvr != 0) { nServerCnt++; serverList.GetClassDetails(ref guid, out pszProgID, out pszUserType, out pszVerIndProgID); OPCServer server = new OPCServer() { Guid = guid, ProgId = pszProgID, HostName = hostname, }; servers.Add(server); pIOPCEnumGuid.Next(1, out guid, out iRetSvr); } Internal.Interop.ReleaseServer(serverList); } catch (System.Exception ex) { Console.Out.WriteLine(ex.Message); } return(servers); }
/*------------------------------------------------------ * 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. IOPCServerList2 svrList = (IOPCServerList2)CreateInstance(CLSID_SERVERLIST, sNodeName); Guid clsidList; svrList.CLSIDFromProgID(sSvrName, out clsidList); //Error m_OPCServer = (IOPCServer)CreateInstance(clsidList, sNodeName); if (m_OPCServer != null) { 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); 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); } }
void FillServerList() { string hostName = localServerButton.Checked ? "localhost" : serverTextBox.Text; serversComboBox.Items.Clear(); try { Type serverListType = Type.GetTypeFromProgID("OPC.ServerList", hostName); IOPCServerList2 serverList = (IOPCServerList2)Activator.CreateInstance(serverListType); Guid[] categories = { typeof(CATID_OPCDAServer10).GUID }; IOPCEnumGUID enumGuids; serverList.EnumClassesOfCategories(categories.Length, categories, 0, null, out enumGuids); int fetched; enumGuids.Reset(); do { Guid[] ids = new Guid[10]; enumGuids.Next(ids.Length, ids, out fetched); for (int i = 0; i < fetched; i++) { string progId; string name; string vendorId; serverList.GetClassDetails(ref ids[i], out progId, out name, out vendorId); serversComboBox.Items.Add(progId); } } while (fetched > 0); if (serversComboBox.Items.Count > 0) { serversComboBox.Text = serversComboBox.Items[0].ToString(); } } catch (System.Exception) { } }
public Guid CLSIDFromProgID(string progID, string host, ConnectData connectData) { lock (this) { Guid empty; NetworkCredential credential = (connectData != null) ? connectData.GetCredential(null, null) : null; this.m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credential); this.m_host = host; try { this.m_server.CLSIDFromProgID(progID, out empty); } catch { empty = Guid.Empty; } finally { Interop.ReleaseServer(this.m_server); this.m_server = null; } return(empty); } }
public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData) { Opc.Server[] serverArray; lock (this) { NetworkCredential credential = (connectData != null) ? connectData.GetCredential(null, null) : null; this.m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credential); this.m_host = host; try { ArrayList list = new ArrayList(); Guid guid = new Guid(specification.ID); IOPCEnumGUID ppenumClsid = null; Guid[] rgcatidImpl = new Guid[] { guid }; this.m_server.EnumClassesOfCategories(1, rgcatidImpl, 0, null, out ppenumClsid); Guid[] guidArray = this.ReadClasses(ppenumClsid); Interop.ReleaseServer(ppenumClsid); ppenumClsid = null; foreach (Guid guid2 in guidArray) { OpcCom.Factory factory = new OpcCom.Factory(); try { URL url = this.CreateUrl(specification, guid2); Opc.Server server = null; if (specification == Specification.COM_DA_30) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_DA_20) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_AE_10) { server = new Opc.Ae.Server(factory, url); } else if (specification == Specification.COM_HDA_10) { server = new Opc.Hda.Server(factory, url); } else if (specification == Specification.COM_DX_10) { server = new Opc.Dx.Server(factory, url); } list.Add(server); } catch (Exception) { } } serverArray = (Opc.Server[])list.ToArray(typeof(Opc.Server)); } finally { Interop.ReleaseServer(this.m_server); this.m_server = null; } } return(serverArray); }
/// <summary> /// Return a list of local OPC A&E servers URLs. /// </summary> public static string[] GetLocalAeServers() { IOPCServerList2 m_server = null; // Establish connection to the OPC Server m_server = (IOPCServerList2)Helper.CreateInstance(CLSID, "localhost", null); try { ArrayList servers = new ArrayList(); string[] urls = null; // convert the interface version to a guid. Guid catid = new Guid("58E13251-AC87-11d1-84D5-00608CB8A7E9"); // get list of servers in the specified specification. IOPCEnumGUID enumerator = null; m_server.EnumClassesOfCategories( 1, new Guid[] { catid }, 0, null, out enumerator); // read clsids. Guid[] clsids = ReadClasses(enumerator); enumerator = null; urls = new string[clsids.Length]; int i = 0; // fetch class descriptions. foreach (Guid clsid in clsids) { string progId; string sTemp1; string sTemp2; Guid tempClsid = clsid; try { m_server.GetClassDetails( ref tempClsid, out progId, out sTemp1, out sTemp2); sTemp1 = "opcae://localhost/" + progId; urls[i] = sTemp1; i++; } catch (Exception) { // ignore bad clsids. } } // free the server. Helper.ReleaseServer(m_server); m_server = null; return(urls); } finally { // free the server. Helper.ReleaseServer(m_server); m_server = null; } }