OPC Studio User's Guide and Reference
DiscoverLocalServers Method (_EasyUAClient)
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.ComTypes Namespace > _EasyUAClient Interface : DiscoverLocalServers Method
The name of the machine that runs the discovery server that will be used for discovery task.

The value of this parameter can be null (Nothing in Visual Basic).

Discovers OPC-UA servers available on the discovery host.
Discovers OPC-UA servers available on the discovery host.

This method discovers OPC-UA servers and returns information about them, using the specified discovery host. It uses the so-called Local Discovery Server (LDS). You can influence how the discovery works using the properties in static SharedParameters.

Invokes the FindServers service.

See also in Knowledge Base: Technical note-OPC UA Discovery in QuickOPC.

Syntax
'Declaration
 
<NotNullAttribute()>
Function DiscoverLocalServers( _
   ByVal discoveryHost As String _
) As UADiscoveryElementCollection
'Usage
 
Dim instance As _EasyUAClient
Dim discoveryHost As String
Dim value As UADiscoveryElementCollection
 
value = instance.DiscoverLocalServers(discoveryHost)
[NotNull()]
UADiscoveryElementCollection DiscoverLocalServers( 
   string discoveryHost
)
[NotNull()]
UADiscoveryElementCollection^ DiscoverLocalServers( 
   String^ discoveryHost
) 

Parameters

discoveryHost
The name of the machine that runs the discovery server that will be used for discovery task.

The value of this parameter can be null (Nothing in Visual Basic).

Return Value

Returns a collection of application elements (servers, OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement).

This method never returns null (Nothing in Visual Basic).

The individual elements of the returned value are never null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

The OPC UA operation has failed. This operation exception in uniformly used to allow common handling of various kinds of errors. The System.Exception.InnerException always contains information about the actual error cause.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

This member or type is for use from COM. It is not meant to be used from .NET or Python. Refer to the corresponding .NET member or type instead, if you are developing in .NET or Python.

This is an extension method (info: C#, VB.NET). In languages that have support for extensions methods (such as C# and VB.NET), you can use the extension method as if it were a regular method on the object that is its first parameter. In other languages (such as with Python.NET), you will call the extension as a static method, and pass it the object on which it acts as its first parameter.

This method discovers OPC-UA servers and returns information about them, using the specified discovery host. It uses the so-called Local Discovery Server (LDS). You can influence how the discovery works using the properties in static SharedParameters.

Invokes the FindServers service.

See also in Knowledge Base: Technical note-OPC UA Discovery in QuickOPC.

Example

COM

// This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

#include "stdafx.h"    // Includes "QuickOpc.h", and other commonly used files
#include "DiscoverLocalServers.h"

namespace _EasyUAClient
{
    void DiscoverLocalServers::Main()
    {
        // Initialize the COM library
        CoInitializeEx(NULL, COINIT_MULTITHREADED);
        {
            // Instantiate the client object
            _EasyUAClientPtr ClientPtr(__uuidof(EasyUAClient));

            // Obtain collection of server elements
            _UADiscoveryElementCollectionPtr DiscoveryElementsPtr = ClientPtr->DiscoverLocalServers(L"opcua.demo-this.com");
    
            // Display results
            IEnumVARIANTPtr EnumDiscoveryElementPtr = DiscoveryElementsPtr->GetEnumerator();
            _variant_t vDiscoveryElement;
            while (EnumDiscoveryElementPtr->Next(1, &vDiscoveryElement, NULL) == S_OK)
            {
                _UADiscoveryElementPtr DiscoveryElementPtr(vDiscoveryElement);
                _tprintf(_T("DiscoveryElementCollection[\"%s\"]: "), (LPCTSTR)CW2CT(DiscoveryElementPtr->DiscoveryUriString));
                _tprintf(_T("%s\n"), (LPCTSTR)CW2CT(DiscoveryElementPtr->ApplicationUriString));
                vDiscoveryElement.Clear();
            }
        }
         // Release all interface pointers BEFORE calling CoUninitialize()
        CoUninitialize();
    }
}
// This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

class procedure DiscoverLocalServers.Main;
var
  Client: EasyUAClient;
  Count: Cardinal;
  Element: OleVariant;
  DiscoveryElement: _UADiscoveryElement;
  DiscoveryElementEnumerator: IEnumVariant;
  DiscoveryElements: _UADiscoveryElementCollection;
begin
  // Instantiate the client object
  Client := CoEasyUAClient.Create;

  // Obtain collection of server elements
  DiscoveryElements := Client.DiscoverLocalServers('opcua.demo-this.com');

  // Display results
  DiscoveryElementEnumerator := DiscoveryElements.GetEnumerator;
  while (DiscoveryElementEnumerator.Next(1, Element, Count) = S_OK) do
  begin
    DiscoveryElement := IUnknown(Element) as _UADiscoveryElement;
    WriteLn(
      'DiscoveryElements["',
      DiscoveryElement.DiscoveryUriString,
      '".ApplicationUriString: ',
      DiscoveryElement.ApplicationUriString);
  end;
end;
// This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

class procedure DiscoverLocalServers.Main;
var
  Client: OpcLabs_EasyOpcUA_TLB._EasyUAClient;
  Count: Cardinal;
  Element: OleVariant;
  DiscoveryElement: _UADiscoveryElement;
  DiscoveryElementEnumerator: IEnumVariant;
  DiscoveryElements: _UADiscoveryElementCollection;
begin
  // Instantiate the client object
  Client := CoEasyUAClient.Create;

  // Obtain collection of server elements
  try
    DiscoveryElements := Client.DiscoverLocalServers('opcua.demo-this.com');
  except
    on E: EOleException do
    begin
      WriteLn(Format('*** Failure: %s', [E.GetBaseException.Message]));
      Exit;
    end;
  end;

  // Display results
  DiscoveryElementEnumerator := DiscoveryElements.GetEnumerator;
  while (DiscoveryElementEnumerator.Next(1, Element, Count) = S_OK) do
  begin
    DiscoveryElement := IUnknown(Element) as _UADiscoveryElement;
    WriteLn(
      'DiscoveryElements["',
      DiscoveryElement.DiscoveryUriString,
      '".ApplicationUriString: ',
      DiscoveryElement.ApplicationUriString);
  end;
end;
// This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in PHP on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PHP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

// Instantiate the client object
$Client = new COM("OpcLabs.EasyOpc.UA.EasyUAClient");

// Obtain collection of server elements
try
{
    $DiscoveryElementCollection = $Client->DiscoverLocalServers("opcua.demo-this.com");
}
catch (com_exception $e)
{
    printf("*** Failure: %s\n", $e->getMessage());
}

// Display results
foreach ($DiscoveryElementCollection as $DiscoveryElement)
{
    printf("DiscoveryElementCollection[\"s\"].ApplicationUriStrings\n", 
        $DiscoveryElement->DiscoveryUriString, $DiscoveryElement->ApplicationUriString);
}
# This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
#
# The Python for Windows (pywin32) extensions package is needed. Install it using "pip install pypiwin32".
# CAUTION: We now recommend using Python.NET package instead. Full set of examples with Python.NET is available!
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
import win32com.client
from pywintypes import com_error

# Instantiate the client object
client = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.EasyUAClient') 

# Obtain collection of server elements
try:
    discoveryElementCollection = client.DiscoverLocalServers('opcua.demo-this.com')
except com_error as e:
    print('*** Failure: ' + e.args[2][1] + ': ' + e.args[2][2])
    exit()

# Display results
for discoveryElement in discoveryElementCollection:
    print('DiscoveryElementCollection["', discoveryElement.DiscoveryUriString, '"].ApplicationUriString: ', 
          discoveryElement.ApplicationUriString)
REM This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
REM
REM Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
REM OPC client and subscriber examples in Visual Basic on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VB .
REM Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
REM a commercial license in order to use Online Forums, and we reply to every post.

Public Sub DiscoverLocalServers_Main_Command_Click()
    OutputText = ""

    ' Instantiate the client object
    Dim Client As New EasyUAClient

    ' Obtain collection of server elements
    On Error Resume Next
    Dim DiscoveryElementCollection As UADiscoveryElementCollection
    Set DiscoveryElementCollection = Client.DiscoverLocalServers("opcua.demo-this.com")
    If Err.Number <> 0 Then
        OutputText = OutputText & "*** Failure: " & Err.Source & ": " & Err.Description & vbCrLf
        Exit Sub
    End If
    On Error GoTo 0
    
    ' Display results
    Dim DiscoveryElement As UADiscoveryElement: For Each DiscoveryElement In DiscoveryElementCollection
        OutputText = OutputText & "DiscoveryElementCollection[""" & DiscoveryElement.DiscoveryUriString & """].ApplicationUriString: " & _
            DiscoveryElement.applicationUriString & vbCrLf
    Next
End Sub
Rem This example shows how to obtain application URLs of all OPC Unified Architecture servers on the specified host.
Rem
Rem Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
Rem OPC client and subscriber examples in VBScript on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBScript .
Rem Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
Rem a commercial license in order to use Online Forums, and we reply to every post.

Option Explicit

' Instantiate the client object
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.UA.EasyUAClient")

' Obtain collection of server elements
On Error Resume Next
Dim DiscoveryElementCollection: Set DiscoveryElementCollection = Client.DiscoverLocalServers("opcua.demo-this.com")
If Err.Number <> 0 Then
    WScript.Echo "*** Failure: " & Err.Source & ": " & Err.Description
    WScript.Quit
End If
On Error Goto 0

' Display results
Dim DiscoveryElement: For Each DiscoveryElement In DiscoveryElementCollection
    WScript.Echo "DiscoveryElementCollection[""" & DiscoveryElement.DiscoveryUriString & """].ApplicationUriString: " & _
        DiscoveryElement.ApplicationUriString
Next
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also