Пример #1
0
        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);
        }
Пример #2
0
        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)
            {
            }
        }
        /// <summary>
        /// Reads the server details from the enumerator.
        /// </summary>
        URL CreateUrl(Specification specification, Guid clsid)
        {
            // initialize the server url.
            URL url = new URL();

            url.HostName = m_host;
            url.Port     = 0;
            url.Path     = null;

            if (specification == Specification.COM_DA_30)
            {
                url.Scheme = UrlScheme.DA;
            }
            else if (specification == Specification.COM_DA_20)
            {
                url.Scheme = UrlScheme.DA;
            }
            else if (specification == Specification.COM_DA_10)
            {
                url.Scheme = UrlScheme.DA;
            }
            else if (specification == Specification.COM_DX_10)
            {
                url.Scheme = UrlScheme.DX;
            }
            else if (specification == Specification.COM_AE_10)
            {
                url.Scheme = UrlScheme.AE;
            }
            else if (specification == Specification.COM_HDA_10)
            {
                url.Scheme = UrlScheme.HDA;
            }
            else if (specification == Specification.COM_BATCH_10)
            {
                url.Scheme = UrlScheme.BATCH;
            }
            else if (specification == Specification.COM_BATCH_20)
            {
                url.Scheme = UrlScheme.BATCH;
            }

            try
            {
                // fetch class details from the enumerator.
                string progID       = null;
                string description  = null;
                string verIndProgID = null;

                m_server.GetClassDetails(
                    ref clsid,
                    out progID,
                    out description,
                    out verIndProgID);

                // create the server URL path.
                if (verIndProgID != null)
                {
                    url.Path = String.Format("{0}/{1}", verIndProgID, "{" + clsid.ToString() + "}");
                }
                else if (progID != null)
                {
                    url.Path = String.Format("{0}/{1}", progID, "{" + clsid.ToString() + "}");
                }
            }
            catch (Exception)
            {
                // bad value in registry.
            }
            finally
            {
                // default to the clsid if the prog is not known.
                if (url.Path == null)
                {
                    url.Path = String.Format("{0}", "{" + clsid.ToString() + "}");
                }
            }

            // return the server url.
            return(url);
        }
Пример #4
0
        /// <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;
            }
        }