QuickOPC User's Guide and Reference
SubscribeDataChange Method (_EasyUAClient)
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.ComTypes Namespace > _EasyUAClient Interface : SubscribeDataChange Method
Endpoint descriptor. Identifies the OPC-UA server.
Node descriptor. Identifies the node in OPC server's address space.
The sampling interval (in milliseconds) indicates the fastest rate at which the Server should sample its underlying source for data changes.
Subscribe to a monitored item. Specify an endpoint descriptor, node id, and sampling interval.
Syntax
'Declaration
 
Function SubscribeDataChange( _
   ByVal endpointDescriptorString As String, _
   ByVal nodeDescriptorString As String, _
   ByVal samplingInterval As Integer _
) As Integer
'Usage
 
Dim instance As _EasyUAClient
Dim endpointDescriptorString As String
Dim nodeDescriptorString As String
Dim samplingInterval As Integer
Dim value As Integer
 
value = instance.SubscribeDataChange(endpointDescriptorString, nodeDescriptorString, samplingInterval)

Parameters

endpointDescriptorString
Endpoint descriptor. Identifies the OPC-UA server.
nodeDescriptorString
Node descriptor. Identifies the node in OPC server's address space.
samplingInterval
The sampling interval (in milliseconds) indicates the fastest rate at which the Server should sample its underlying source for data changes.

Return Value

The method returns an integer handle that uniquely identifies the monitored item subscription.
Remarks

You can obtain nodeDescriptorString e.g. by calling one of the browsing methods on OpcLabs.EasyOpc.UA.EasyUAClientCore object.

Example

COM

// This example shows how to subscribe to changes of a single monitored item and display each change.

#include "stdafx.h"    // Includes "QuickOpc.h", and other commonly used files
#include <atlcom.h>
#include "SubscribeDataChange.h"

namespace _EasyUAClient
{
    // CEasyUAClientEvents

    class CEasyUAClientEvents : public IDispEventImpl<1, CEasyUAClientEvents>
    {
    public:
    BEGIN_SINK_MAP(CEasyUAClientEvents)
        // Event handlers must have the __stdcall calling convention
        SINK_ENTRY(1, DISPID_EASYUACLIENTEVENTS_DATACHANGENOTIFICATION, &CEasyUAClientEvents::DataChangeNotification)
    END_SINK_MAP()

    public:
        // The handler for EasyUAClient.DataChangeNotification event
        STDMETHOD(DataChangeNotification)(VARIANT varSender, _EasyUADataChangeNotificationEventArgs* pEventArgs)
        {
            // Display the data
            // Remark: Production code would check EventArgsPtr->Exception before accessing EventArgsPtr->AttributeData.
            _UAAttributeDataPtr AttributeDataPtr(pEventArgs->AttributeData);
            _tprintf(_T("%s\n"), (LPCTSTR)CW2CT(AttributeDataPtr->ToString));

            return S_OK;
        }
    };



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

            // Hook events
            CEasyUAClientEvents* pClientEvents = new CEasyUAClientEvents();
            AtlGetObjectSourceInterface(ClientPtr, &pClientEvents->m_libid, &pClientEvents->m_iid, 
                &pClientEvents->m_wMajorVerNum, &pClientEvents->m_wMinorVerNum);
            pClientEvents->m_iid = _uuidof(DEasyUAClientEvents);
            pClientEvents->DispEventAdvise(ClientPtr, &pClientEvents->m_iid);

            //
            _tprintf(_T("Subscribing...\n"));
            ClientPtr->SubscribeDataChange(
                //L"http://opcua.demo-this.com:51211/UA/SampleServer", 
                L"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer",
                L"nsu=http://test.org/UA/Data/ ;i=10853",
                1000);

            _tprintf(_T("Processing monitored item changed events for 1 minute...\n"));
            Sleep(60*1000);

            // Unhook events
            pClientEvents->DispEventUnadvise(ClientPtr, &pClientEvents->m_iid);
        }
         // Release all interface pointers BEFORE calling CoUninitialize()
        CoUninitialize();
    }
}
// This example shows how to subscribe to changes of a single monitored item
// and display each change.

type
  TClientEventHandlers = class
    procedure OnDataChangeNotification(
      Sender: TObject;
      sender0: OleVariant;
      eventArgs: _EasyUADataChangeNotificationEventArgs);
  end;

procedure TClientEventHandlers.OnDataChangeNotification(
  Sender: TObject;
  sender0: OleVariant;
  eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
  // Display the data
  // Remark: Production code would check eventArgs.Exception before accessing
  // eventArgs.AttributeData.
    WriteLn(eventArgs.Arguments.NodeDescriptor.ToString, ': ',
    eventArgs.AttributeData.ToString);
end;

class procedure SubscribeDataChange.Main;
var
  Client: EasyUAClient;
  EvsClient: TEvsEasyUAClient;
  ClientEventHandlers: TClientEventHandlers;
begin
  // Instantiate the client object and hook events
  EvsClient := TEvsEasyUAClient.Create(nil);
  Client := EvsClient.ComServer;
  ClientEventHandlers := TClientEventHandlers.Create;
  EvsClient.OnDataChangeNotification := @ClientEventHandlers.OnDataChangeNotification;

  WriteLn('Subscribing...');
  Client.SubscribeDataChange(
    //'http://opcua.demo-this.com:51211/UA/SampleServer',
    'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
    'nsu=http://test.org/UA/Data/ ;i=10853',
    1000);

  WriteLn('Processing data change events for 1 minute...');
  PumpSleep(60*1000);
end;
// This example shows how to subscribe to changes of a single monitored item
// and display each change.

type
  TClientEventHandlers121 = class
    procedure OnDataChangeNotification(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyUADataChangeNotificationEventArgs);
  end;

procedure TClientEventHandlers121.OnDataChangeNotification(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
  // Display the data
  if eventArgs.Succeeded then
      WriteLn(eventArgs.Arguments.NodeDescriptor.ToString, ': ',
      eventArgs.AttributeData.ToString)
  else
      WriteLn(eventArgs.Arguments.NodeDescriptor.ToString, ' *** Failure: ',
      eventArgs.ErrorMessageBrief);
end;

class procedure SubscribeDataChange.Main;
var
  Client: TEasyUAClient;
  ClientEventHandlers: TClientEventHandlers121;
begin
  // Instantiate the client object and hook events
  Client := TEasyUAClient.Create(nil);
  ClientEventHandlers := TClientEventHandlers121.Create;
  Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification;

  WriteLn('Subscribing...');
  Client.SubscribeDataChange(
      //'http://opcua.demo-this.com:51211/UA/SampleServer',
      //'https://opcua.demo-this.com:51212/UA/SampleServer/',
      'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
    'nsu=http://test.org/UA/Data/ ;i=10853',
    1000);

  WriteLn('Processing data change events for 1 minute...');
  PumpSleep(60*1000);

  WriteLn('Unsubscribing...');
  Client.UnsubscribeAllMonitoredItems;

  WriteLn('Waiting for 5 seconds...');
  PumpSleep(5*1000);

  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);
end;
// This example shows how to subscribe to changes of a single monitored item and display each change.

class ClientEvents {
    function DataChangeNotification($Sender, $E)
    {
        // Display the data
        if ($E->Succeeded)
        printf("%s\n", $E->AttributeData);
        else
        printf("*** Failure : %s\n", $E->ErrorMessageBrief);
    }
}



// Instantiate the client object and hook events
$Client = new COM("OpcLabs.EasyOpc.UA.EasyUAClient");
$ClientEvents = new ClientEvents();
com_event_sink($Client, $ClientEvents, "DEasyUAClientEvents");

printf("Subscribing...\n");
$Client->SubscribeDataChange(
    //"http://opcua.demo-this.com:51211/UA/SampleServer", 
    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", 
   "nsu=http://test.org/UA/Data/ ;i=10853", 
    1000);

printf("Processing monitored item changed events for 1 minute...\n");
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 60);
Rem This example shows how to subscribe to changes of a single monitored item and display each change.

' The client object, with events
'Public WithEvents Client1 As EasyUAClient

Public Sub SubscribeDataChange_Main_Command_Click()
    OutputText = ""
    
    Set Client1 = New EasyUAClient

    OutputText = OutputText & "Subscribing..." & vbCrLf
    Call Client1.SubscribeDataChange("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853", 1000)

    OutputText = OutputText & "Processing data changed notification events for 1 minute..." & vbCrLf
    Pause 60000

    OutputText = OutputText & "Unsubscribing..." & vbCrLf
    Client1.UnsubscribeAllMonitoredItems

    OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf
    Pause 5000

    OutputText = OutputText & "Finished." & vbCrLf
    Set Client1 = Nothing
End Sub

Public Sub Client1_DataChangeNotification(ByVal sender As Variant, ByVal eventArgs As EasyUADataChangeNotificationEventArgs)
    ' Display the data
    If eventArgs.Exception Is Nothing Then
        OutputText = OutputText & eventArgs.AttributeData & vbCrLf
    Else
        OutputText = OutputText & eventArgs.ErrorMessageBrief & vbCrLf
    End If
End Sub
Rem This example shows how to subscribe to changes of a single monitored item and display each change.

Option Explicit

' Instantiate the client object and hook events
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.UA.EasyUAClient")
WScript.ConnectObject Client, "Client_"

WScript.Echo "Subscribing..."
Client.SubscribeDataChange "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853", 1000

WScript.Echo "Processing monitored item changed events for 1 minute..."
WScript.Sleep 60*1000



Sub Client_DataChangeNotification(Sender, e)
    ' Display the data
    Dim display: If e.Exception Is Nothing Then display = e.AttributeData Else display = e.ErrorMessageBrief
    WScript.Echo display
End Sub
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