/// <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. /// </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 discovery = specification == OpcSpecification.OPC_AE_10 || (specification == OpcSpecification.OPC_DA_20 || specification == OpcSpecification.OPC_DA_30) || specification == OpcSpecification.OPC_HDA_10; if (discovery) { if (discovery_ == null || hostName_ != discoveryServerUrl) { discovery_?.Dispose(); hostName_ = discoveryServerUrl; discovery_ = new ServerEnumerator(); } OpcServer[] servers = discovery_.GetAvailableServers(specification); if (servers != null) { foreach (OpcServer server in servers) { serverList.Add(server); } } } return(serverList); }
/// <summary> /// 获取OpcServer /// </summary> /// <returns></returns> private Opc.Da.Server GetOpcServer() { try { //"OPC.IwSCP/{108fb1cf-f509-4d86-b1da-54bf1dd67a8d}" IDiscovery opcDiscovery = new ServerEnumerator(); Opc.Server[] opcServers = opcDiscovery.GetAvailableServers(Specification.COM_DA_20); return(new Opc.Da.Server(new OpcCom.Factory(), new Opc.URL(Opc.UrlScheme.DA + "://localhost/" + opcServers[0].Url.Path))); } catch (Exception ex) { throw new Exception(ex.Message); } }
private void GetOpcServers() { try { ServerEnumerator se = new ServerEnumerator(); Opc.Server[] servers = se.GetAvailableServers(Specification.COM_DA_20); serversComboBox.Items.Clear(); foreach (Opc.Server item in servers) { serversComboBox.Items.Add(item.Name); } } catch (Exception) { } }
private void OpcOptionsEditor_Loaded(object sender, RoutedEventArgs e) { Dispatcher dispatcher = Dispatcher; var action = new Action(() => { try { IDiscovery discovery = new ServerEnumerator(); var localservers = discovery.GetAvailableServers(Specification.COM_DA_20) .Select(s => s.Url) .ToArray(); dispatcher.Invoke(new Action(() => { ItemsSource = localservers; })); } catch (Exception exception) { MessageBox.Show(exception.Message); } }); action.BeginInvoke(action.EndInvoke, null); }
public Form1() { InitializeComponent(); OpcCom.ServerEnumerator se = new ServerEnumerator(); Opc.Server[] Servers; String sErrFunc = ""; Opc.ConnectData cd = new Opc.ConnectData(new System.Net.NetworkCredential()); sErrFunc = "GetAvailableServers"; Servers = se.GetAvailableServers(Opc.Specification.COM_DA_20, "localhost", cd); Console.WriteLine(Servers[1].Name.ToString()); Opc.URL url = new Opc.URL("opcda://localhost/" + Servers[0].Name); OpcCom.Factory fact = new OpcCom.Factory(); server = new Opc.Da.Server(fact, null); server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); }
public ServerOnNetworkCollection(ServerEnumerator enumerator) { m_enumerator = enumerator; }