Exemple #1
0
        /// <summary>
        /// 连接OPC
        /// </summary>
        /// <returns></returns>
        public bool Connect(string opcServerName, string opcServerIp)
        {
            try
            {
                Opc.Server[] serverArray = new ServerEnumerator().GetAvailableServers(Specification.COM_DA_20, opcServerIp, null);
                if (serverArray != null)
                {
                    Opc.Server server = serverArray.Where(a => a.Name == opcServerName).FirstOrDefault();
                    if (server != null)
                    {
                        server.Connect();
                        this.Server = (Opc.Da.Server)server;
                        return(true);
                    }
                }
            }
            catch (Exception ex) { if (OnScanError != null)
                                   {
                                       OnScanError(ex);
                                   }
            }

            this.Close();
            return(false);
        }
        /// <summary>
        /// Returns a list of servers that support the specified specification on the specified host.
        /// </summary>
        public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData)
        {
            lock (this)
            {
                NetworkCredential credentials = (connectData != null)?connectData.GetCredential(null, null):null;

                // connect to the server.
                m_server = (IOPCServerList2)OpcCom.Interop.CreateInstance(CLSID, host, credentials);
                m_host   = host;

                try
                {
                    ArrayList servers = new ArrayList();

                    // convert the interface version to a guid.
                    Guid catid = new Guid(specification.ID);

                    // 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);

                    // release enumerator object.
                    OpcCom.Interop.ReleaseServer(enumerator);
                    enumerator = null;

                    // fetch class descriptions.
                    foreach (Guid clsid in clsids)
                    {
                        Factory factory = new OpcCom.Factory();

                        try
                        {
                            URL url = CreateUrl(specification, clsid);

                            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);
                            }

                            servers.Add(server);
                        }
                        catch (Exception)
                        {
                            // ignore bad clsids.
                        }
                    }

                    return((Opc.Server[])servers.ToArray(typeof(Opc.Server)));
                }
                finally
                {
                    // free the server.
                    OpcCom.Interop.ReleaseServer(m_server);
                    m_server = null;
                }
            }
        }
 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);
 }
Exemple #4
0
 // Token: 0x06000004 RID: 4 RVA: 0x000020E4 File Offset: 0x000010E4
 public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData)
 {
     Opc.Server[] result;
     lock (this)
     {
         NetworkCredential credential = (connectData != null) ? connectData.GetCredential(null, null) : null;
         this.m_server = (IOPCServerList2)Interop.CreateInstance(ServerEnumerator.CLSID, host, credential);
         this.m_host   = host;
         try
         {
             ArrayList    arrayList    = new ArrayList();
             Guid         guid         = new Guid(specification.ID);
             IOPCEnumGUID iopcenumGUID = null;
             this.m_server.EnumClassesOfCategories(1, new Guid[]
             {
                 guid
             }, 0, null, out iopcenumGUID);
             Guid[] array = this.ReadClasses(iopcenumGUID);
             Interop.ReleaseServer(iopcenumGUID);
             iopcenumGUID = null;
             foreach (Guid clsid in array)
             {
                 Factory factory = new Factory();
                 try
                 {
                     URL        url   = this.CreateUrl(specification, clsid);
                     Opc.Server value = null;
                     if (specification == Specification.COM_DA_30)
                     {
                         value = new Opc.Da.Server(factory, url);
                     }
                     else if (specification == Specification.COM_DA_20)
                     {
                         value = new Opc.Da.Server(factory, url);
                     }
                     else if (specification == Specification.COM_AE_10)
                     {
                         value = new Opc.Ae.Server(factory, url);
                     }
                     else if (specification == Specification.COM_HDA_10)
                     {
                         value = new Opc.Hda.Server(factory, url);
                     }
                     else if (specification == Specification.COM_DX_10)
                     {
                         value = new Opc.Dx.Server(factory, url);
                     }
                     arrayList.Add(value);
                 }
                 catch (Exception)
                 {
                 }
             }
             result = (Opc.Server[])arrayList.ToArray(typeof(Opc.Server));
         }
         finally
         {
             Interop.ReleaseServer(this.m_server);
             this.m_server = null;
         }
     }
     return(result);
 }