QuickOPC User's Guide and Reference
OPC UA Complex Data subscribing
View with Navigation Tools
Extensions > Integrated Extensions > OPC UA Complex Data Extension > Basic OPC UA Complex Data operations > OPC UA Complex Data subscribing

To subscribe to complex data information, call one of the Subscribe methods on the EasyUAClient Class as usual. When complex data is returned by the client, a DataChangeNotification Event or EventNotification Event will be raised (or a corresponding callback will be made). The value in the event arguments will be of type UAGenericObject Class, and you will process its contents.

How precisely you process the received generic data depends on the application. In general, there can be two usage cases:

The example below shows how to subscribe to OPC UA complex data and display the incoming values.

.NET

// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples.ComplexData._EasyUAClient
{
    class SubscribeDataChange
    {
        public static void Main1()
        {
            // Define which server and node we will work with.
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"
            UANodeDescriptor nodeDescriptor =
                "nsu=http://test.org/UA/Data/ ;i=10867"; // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue

            // Instantiate the client object and hook the event handler.
            var client = new EasyUAClient();
            client.DataChangeNotification += client_DataChangeNotification;

            // Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just 
            // the data returned is different.
            Console.WriteLine("Subscribing...");
            client.SubscribeDataChange(endpointDescriptor, nodeDescriptor, samplingInterval:1000);

            Console.WriteLine("Processing data change events for 20 seconds...");
            System.Threading.Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            client.UnsubscribeAllMonitoredItems();

            Console.WriteLine("Waiting for 5 seconds...");
            System.Threading.Thread.Sleep(5 * 1000);

            // Unhook the event handler.
            client.DataChangeNotification -= client_DataChangeNotification;


            // Example output:
            //
            //Subscribing...
            //Processing data change events for 20 seconds...
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ArrayValueDataType) structured
            //(ArrayValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //(ScalarValueDataType) structured
            //Unsubscribing...
            //Waiting for 5 seconds...
        }

        static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
            // Display value
            if (e.Succeeded)
                Console.WriteLine("Value: {0}", e.AttributeData.Value);
            else
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);

            // For processing the internals of the data, refer to examples for GenericData and DataType classes.
        }
    }
}

COM

// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.

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

procedure TClientEventHandlers33.OnDataChangeNotification(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
  // Display value
  if eventArgs.Succeeded then
      WriteLn('Value: ', eventArgs.AttributeData.Value.ToString)
  else
      WriteLn('*** Failure: ', eventArgs.ErrorMessageBrief);

  // For processing the internals of the data, refer to examples for GenericData and DataType classes.
end;

class procedure SubscribeDataChange.Main;
var
  Client: TEasyUAClient;
  ClientEventHandlers: TClientEventHandlers33;
  EndpointDescriptor: string;
  NodeDescriptor: string;
begin
  // Define which server and node we will work with.
  EndpointDescriptor := 
    //'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';
  NodeDescriptor := 'nsu=http://test.org/UA/Data/ ;i=10867';  // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue

  // Instantiate the client object and hook events
  Client := TEasyUAClient.Create(nil);
  ClientEventHandlers := TClientEventHandlers33.Create;
  Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification;

  // Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
  // the data returned is different.
  WriteLn('Subscribing...');
  Client.SubscribeDataChange(EndpointDescriptor, NodeDescriptor, 1000);

  WriteLn('Processing data change events for 20 seconds...');
  PumpSleep(20*1000);

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

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

  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);

  // Example output:
  //
  //Subscribing...
  //Processing data change events for 20 seconds...
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ArrayValueDataType) structured
  //(ArrayValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //(ScalarValueDataType) structured
  //Unsubscribing...
  //Waiting for 5 seconds...
end;

 

 

Complex data is also supported and can also be returned in notifications for OPC UA Alarms&Conditions, i.e. in event arguments of type EasyUAEventNotificationEventArgs, used in the EventNotification Event .

See Also

Reference