QuickOPC User's Guide and Reference
UseUdpCaptureFile Method (_UAPubSubConnectionDescriptor)
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.PubSub.ComTypes Namespace > _UAPubSubConnectionDescriptor Interface : UseUdpCaptureFile Method
The name of the packet capture file.
Sets the PubSub connection descriptor to use UDP packet capture data from a given file.
Syntax
'Declaration
 
Sub UseUdpCaptureFile( _
   ByVal captureFileName As String _
) 
'Usage
 
Dim instance As _UAPubSubConnectionDescriptor
Dim captureFileName As String
 
instance.UseUdpCaptureFile(captureFileName)
void UseUdpCaptureFile( 
   string captureFileName
)
void UseUdpCaptureFile( 
   String^ captureFileName
) 

Parameters

captureFileName
The name of the packet capture file.
Exceptions
ExceptionDescription
The file image of a dynamic link library (DLL) or an executable program is invalid.
A managed assembly was found but could not be loaded.

An attempt to access a file that does not exist on disk has failed.

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.

A path or fully qualified file name is longer than the system-defined maximum length.

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.

A security error was detected.

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

 

In many cases, all code needed to perform OPC UA PubSub functionality is contained directly in core QuickOPC assemblies. There are some situations, however, when one or more additional pieces of software ("package") is needed.

For more information about the use of communication packages in general, see http://kb.opclabs.com/Using_communication_packages .

OpcLabs.Pcap communication package

OpcLabs.Pcap is an external assembly/package of QuickOPC that is only needed for: 

QuickOPC switches to the OpcLabs.Pcap communication package whenever it detects that any of the above is required. Although OpcLabs.Pcap use does not require anything different from the coding point of view, it requires special handling in several respects - e.g. with regard to licensing, installation, and configuration.

For more information about OpcLabs.Pcap, see http://kb.opclabs.com/OpcLabs.Pcap_communication_package .

MQTT communication packages

MQTT communication is not built-into core QuickOPC assemblies, but is rather always provided externally, by means of a package that uses some specific MQTT communication library. QuickOPC automatically uses a default MQTT communication package unless you make a different choice. In case you are fine with the default MQTT communication package selection, you do not need to do anything special in your code, but you still need to make sure that the package is installed and referenced, and you may also want to configure it in some specific way.

For more information about the packages available, see http://kb.opclabs.com/MQTT_communication_packages .

Besides packages that provide "true" MQTT communication, a File-based MQTT emulation, useful for experiments and troubleshooting, is also available.

 

Example

.NET

COM

// This example shows how to feed the packet capture file into the PubSub subscriber, instead of connecting to the message
// oriented middleware (receiving the messages from the network).
//
// The OpcLabs.Pcap assembly needs to be referenced in your project (or otherwise made available, together with its
// dependencies) for the capture files to work. Refer to the documentation for more information.

using System;
using System.Collections.Generic;
using System.Threading;
using OpcLabs.EasyOpc.UA.PubSub;
using OpcLabs.EasyOpc.UA.PubSub.Extensions;
using OpcLabs.EasyOpc.UA.PubSub.OperationModel;

namespace UADocExamples.PubSub._EasyUASubscriber
{
    partial class SubscribeDataSet
    {
        public static void CaptureFile()
        {
            // Define the PubSub connection we will work with. Uses implicit conversion from a string.
            UAPubSubConnectionDescriptor pubSubConnectionDescriptor = "opc.eth://FF-FF-FF-FF-FF-FF";
            // Use packets from the specified Ethernet capture file. The file itself is at the root of the project, and we
            // have specified that it has to be copied to the project's output directory.
            // Note that .pcap is the default file name extension, and can thus be omitted.
            pubSubConnectionDescriptor.UseEthernetCaptureFile("UADemoPublisher-Ethernet.pcap");

            // Alternative setup for Ethernet with VLAN tagging:
            //UAPubSubConnectionDescriptor pubSubConnectionDescriptor = "opc.eth://FF-FF-FF-FF-FF-FF:2";
            //pubSubConnectionDescriptor.UseEthernetCaptureFile("UADemoPublisher-EthernetVlan.pcap");

            // Alternative setup for UDP over IPv4:
            //UAPubSubConnectionDescriptor pubSubConnectionDescriptor = "opc.udp://239.0.0.1";
            //pubSubConnectionDescriptor.UseUdpCaptureFile("UADemoPublisher-UDP.pcap");

            // Alternative setup for UDP over IPv6:
            //UAPubSubConnectionDescriptor pubSubConnectionDescriptor = "opc.udp://[ff02::1]";
            //pubSubConnectionDescriptor.UseUdpCaptureFile("UADemoPublisher-UDP6.pcap");

            // Instantiate the subscriber object.
            var subscriber = new EasyUASubscriber();

            // Define the arguments for subscribing to the dataset, where the filter is (unsigned 64-bit) publisher Id 31.
            var subscribeDataSetArguments = new UASubscribeDataSetArguments(
                pubSubConnectionDescriptor, UAPublisherId.CreateUInt64(31));

            Console.WriteLine("Subscribing...");
            subscriber.SubscribeDataSet(subscribeDataSetArguments, (sender, args) =>
            {
                // Display the dataset.
                if (args.Succeeded)
                {
                    // An event with null DataSetData just indicates a successful connection.
                    if (!(args.DataSetData is null))
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Dataset data: {args.DataSetData}");
                        foreach (KeyValuePair<string, UADataSetFieldData> pair in args.DataSetData.FieldDataDictionary)
                            Console.WriteLine(pair);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine($"*** Failure: {args.ErrorMessageBrief}");
                }
            });

            Console.WriteLine("Processing dataset message events for 20 seconds...");
            Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            subscriber.UnsubscribeAllDataSets();

            Console.WriteLine("Waiting for 1 second...");
            // Unsubscribe operation is asynchronous, messages may still come for a short while.
            Thread.Sleep(1 * 1000);

            Console.WriteLine("Finished.");
        }

        // Example output:
        //
        //Subscribing...
        //Processing dataset message events for 20 seconds...
        //
        //Dataset data: 2019-10-31T16:04:59.145,266,700,00; Good; Data; publisher=(UInt64)31, writer=1, fields: 4
        //[#0, True {System.Boolean} @2019-10-31T16:04:59.145,266,700,00; Good]
        //[#1, 0 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
        //[#2, 767 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
        //[#3, 10/31/2019 4:04:59 PM {System.DateTime} @2019-10-31T16:04:59.145,266,700,00; Good]
        //
        //Dataset data: 2019-10-31T16:04:59.170,047,500,00; Good; Data; publisher=(UInt64)31, writer=3, fields: 100
        //[#0, 0 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#1, 100 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#2, 200 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#3, 300 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#4, 400 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#5, 500 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#6, 600 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#7, 700 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#8, 800 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#9, 900 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //[#10, 1000 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
        //...
    }
}
# This example shows how to feed the packet capture file into the PubSub subscriber, instead of connecting to the
# message oriented middleware (receiving the messages from the network).
#
# The OpcLabs.Pcap assembly needs to be referenced in your project (or otherwise made available, together with its
# dependencies) for the capture files to work. Refer to the documentation for more information.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import opclabs_pcap
import time

# Import .NET namespaces.
from System import *
from OpcLabs.EasyOpc.UA.PubSub import *
from OpcLabs.EasyOpc.UA.PubSub.Extensions import *
from OpcLabs.EasyOpc.UA.PubSub.OperationModel import *


def dataSetMessage(sender, e):
    # Display the dataset.
    if e.Succeeded:
        # An event with null DataSetData just indicates a successful connection.
        if e.DataSetData is not None:
            print('')
            print('Dataset data: ', e.DataSetData, sep='')
            for pair in e.DataSetData.FieldDataDictionary:
                print(pair)
    else:
        print('')
        print('*** Failure: ', e.ErrorMessageBrief, sep='')


# Define the PubSub connection we will work with. Uses implicit conversion from a string.
# "opc.eth" is the scheme for OPC UA Ethernet. "FF-FF-FF-FF-FF-FF" is the Ethernet broadcast address.
pubSubConnectionDescriptor = UAPubSubConnectionDescriptor.op_Implicit('opc.eth://FF-FF-FF-FF-FF-FF')
# Use packets from the specified Ethernet capture file. The file itself is at the root of the project, and we
# have specified that it has to be copied to the project's output directory.
# Note that .pcap is the default file name extension, and can thus be omitted.
UAPubSubConnectionDescriptorExtension.UseEthernetCaptureFile(pubSubConnectionDescriptor,
                                                             'UADemoPublisher-Ethernet.pcap')

# Alternative setup for Ethernet with VLAN tagging:
#pubSubConnectionDescriptor = UAPubSubConnectionDescriptor.op_Implicit('opc.eth://FF-FF-FF-FF-FF-FF:2')
#UAPubSubConnectionDescriptorExtension.UseEthernetCaptureFile(pubSubConnectionDescriptor,
#                                                             'UADemoPublisher-EthernetVlan.pcap')

# Alternative setup for UDP over IPv4:
#pubSubConnectionDescriptor = UAPubSubConnectionDescriptor.op_Implicit('opc.udp://239.0.0.1')
#UAPubSubConnectionDescriptorExtension.UseEthernetCaptureFile(pubSubConnectionDescriptor,
#                                                             'UADemoPublisher-UDP.pcap')

# Alternative setup for UDP over IPv6:
#pubSubConnectionDescriptor = UAPubSubConnectionDescriptor.op_Implicit('opc.udp://[ff02::1]')
#UAPubSubConnectionDescriptorExtension.UseEthernetCaptureFile(pubSubConnectionDescriptor,
#                                                             'UADemoPublisher-UDP6.pcap')

# Instantiate the subscriber object.
subscriber = EasyUASubscriber()

# Define the arguments for subscribing to the dataset, where the filter is (unsigned 64-bit) publisher Id 31.
subscribeDataSetArguments = UASubscribeDataSetArguments(
    pubSubConnectionDescriptor,
    UASubscribeDataSetFilter(UAPublisherId.CreateUInt64(Decimal(31))))

print('Subscribing...')
IEasyUASubscriberExtension.SubscribeDataSet(subscriber,
                                            subscribeDataSetArguments,
                                            EasyUADataSetMessageEventHandler(dataSetMessage))

print('Processing dataset message events for 20 seconds...')
time.sleep(20)

print('Unsubscribing...')
subscriber.UnsubscribeAllDataSets()

print('Waiting for 1 second...')
# Unsubscribe operation is asynchronous, messages may still come for a short while.
time.sleep(1)

print('Finished.')
' This example shows how to feed the packet capture file into the PubSub subscriber, instead of connecting to the message
' oriented middleware (receiving the messages from the network).
'
' The OpcLabs.Pcap assembly needs to be referenced in your project (Or otherwise made available, together with its
' dependencies) for the capture files to work. Refer to the documentation for more information.

Imports OpcLabs.EasyOpc.UA.PubSub
Imports OpcLabs.EasyOpc.UA.PubSub.Extensions
Imports OpcLabs.EasyOpc.UA.PubSub.OperationModel

Namespace PubSub._EasyUASubscriber
    Partial Friend Class SubscribeDataSet
        Public Shared Sub CaptureFile()

            ' Define the PubSub connection we will work with. Uses implicit conversion from a string.
            Dim pubSubConnectionDescriptor As UAPubSubConnectionDescriptor = "opc.eth://FF-FF-FF-FF-FF-FF"
            ' Use packets from the specified Ethernet capture file. The file itself is at the root of the project, and we
            ' have specified that it has to be copied to the project's output directory.
            ' Note that .pcap is the default file name extension, and can thus be omitted.
            pubSubConnectionDescriptor.UseEthernetCaptureFile("UADemoPublisher-Ethernet.pcap")

            ' Alternative setup for Ethernet with VLAN tagging
            'Dim pubSubConnectionDescriptor As UAPubSubConnectionDescriptor = "opc.eth://FF-FF-FF-FF-FF-FF:2"
            'pubSubConnectionDescriptor.UseEthernetCaptureFile("UADemoPublisher-EthernetVlan.pcap")

            ' Alternative setup for UDP over IPv4
            'Dim pubSubConnectionDescriptor As UAPubSubConnectionDescriptor = "opc.udp://239.0.0.1"
            'pubSubConnectionDescriptor.UseUdpCaptureFile("UADemoPublisher-UDP.pcap")

            ' Alternative setup for UDP over IPv6
            'Dim pubSubConnectionDescriptor As UAPubSubConnectionDescriptor = "opc.udp://[ff02::1]"
            'pubSubConnectionDescriptor.UseUdpCaptureFile("UADemoPublisher-UDP6.pcap")

            ' Instantiate the subscriber object.
            Dim subscriber = New EasyUASubscriber()

            ' Define the arguments for subscribing to the dataset, where the filter Is (unsigned 64-bit) publisher Id 31.
            Dim subscribeDataSetArguments = New UASubscribeDataSetArguments(
                pubSubConnectionDescriptor, UAPublisherId.CreateUInt64(31))

            Console.WriteLine("Subscribing...")
            subscriber.SubscribeDataSet(subscribeDataSetArguments,
                Sub(sender, eventArgs)
                    '  Display the dataset.
                    If eventArgs.Succeeded Then
                        ' An event with null DataSetData just indicates a successful connection.
                        If Not eventArgs.DataSetData Is Nothing Then
                            Console.WriteLine()
                            Console.WriteLine("Dataset data: {0}", eventArgs.DataSetData)
                            For Each pair As KeyValuePair(Of String, UADataSetFieldData) In eventArgs.DataSetData.FieldDataDictionary
                                Console.WriteLine(pair)
                            Next
                        End If
                    Else
                        Console.WriteLine()
                        Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief)
                    End If
                End Sub)

            Console.WriteLine("Processing dataset message events for 20 seconds...")
            Threading.Thread.Sleep(20 * 1000)

            Console.WriteLine("Unsubscribing...")
            subscriber.UnsubscribeAllDataSets()

            Console.WriteLine("Waiting for 1 second...")
            ' Unsubscribe operation is asynchronous, messages may still come for a short while.
            Threading.Thread.Sleep(1 * 1000)

            Console.WriteLine("Finished...")
        End Sub
    End Class

    ' Example output
    '
    'Subscribing...
    'Processing dataset message events for 20 seconds...
    '
    'Dataset data: 2019-10-31T16:04:59.145,266,700,00; Good; Data; publisher=(UInt64)31, writer=1, fields: 4
    '[#0, True {System.Boolean} @2019-10-31T1604:59.145,266,700,00; Good]
    '[#1, 0 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
    '[#2, 767 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
    '[#3, 10/31/2019 4:04:59 PM {System.DateTime} @2019-10-31T16:04:59.145,266,700,00; Good]
    '
    'Dataset data: 2019-10-31T16:04:59.170,047,500,00; Good; Data; publisher=(UInt64)31, writer=3, fields: 100
    '[#0, 0 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#1, 100 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#2, 200 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#3, 300 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#4, 400 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#5, 500 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#6, 600 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#7, 700 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#8, 800 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#9, 900 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '[#10, 1000 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
    '...

End Namespace
// This example shows how to feed the packet capture file into the PubSub subscriber, instead of connecting to the message
// oriented middleware (receiving the messages from the network).
//
// In order to produce network messages for this example, run the UADemoPublisher tool. For documentation, see
// http://kb.opclabs.com/UADemoPublisher_Basics . In some cases, you may have to specify the interface name to be used.
//
// The OpcLabs.Pcap assembly needs to be referenced in your project (or otherwise made available, together with its
// dependencies) for the capture files to work. Refer to the documentation for more information.

type
  TSubscriberEventHandlers72 = class
    procedure OnDataSetMessage(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyUADataSetMessageEventArgs);
  end;

class procedure SubscribeDataSet.CaptureFile;
var
  ConnectionDescriptor: _UAPubSubConnectionDescriptor;
  SubscribeDataSetArguments: _EasyUASubscribeDataSetArguments;
  Subscriber: TEasyUASubscriber;
  SubscriberEventHandlers: TSubscriberEventHandlers72;
begin
  // Define the PubSub connection we will work with.
  SubscribeDataSetArguments := CoEasyUASubscribeDataSetArguments.Create;
  ConnectionDescriptor := SubscribeDataSetArguments.DataSetSubscriptionDescriptor.ConnectionDescriptor;
  ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString := 'opc.eth://FF-FF-FF-FF-FF-FF';
  // Use packets from the specified Ethernet capture file. The file itself is at the root of the project, and we
  // have specified that it has to be copied to the project's output directory.
  // Note that .pcap is the default file name extension, and can thus be omitted.
  ConnectionDescriptor.UseEthernetCaptureFile('UADemoPublisher-Ethernet.pcap');

  // Alternative setup for Ethernet with VLAN tagging:
  //ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = 'opc.eth://FF-FF-FF-FF-FF-FF:2';
  //ConnectionDescriptor.UseEthernetCaptureFile('UADemoPublisher-EthernetVlan.pcap');

  // Alternative setup for UDP over IPv4:
  //ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = 'opc.udp://239.0.0.1';
  //ConnectionDescriptor.UseUdpCaptureFile('UADemoPublisher-UDP.pcap');

  // Alternative setup for UDP over IPv6:
  //ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = 'opc.udp://[ff02::1]';
  //ConnectionDescriptor.UseUdpCaptureFile('UADemoPublisher-UDP6.pcap');

  // Define the arguments for subscribing to the dataset, where the filter is (unsigned 64-bit) publisher Id 31.
  SubscribeDataSetArguments.DataSetSubscriptionDescriptor.Filter.PublisherId.SetIdentifier(UAPublisherIdType_UInt64, 31);

  // Instantiate the subscriber object and hook events.
  Subscriber := TEasyUASubscriber.Create(nil);
  SubscriberEventHandlers := TSubscriberEventHandlers72.Create;
  Subscriber.OnDataSetMessage := SubscriberEventHandlers.OnDataSetMessage;

  WriteLn('Subscribing...');
  Subscriber.SubscribeDataSet(SubscribeDataSetArguments);

  WriteLn('Processing dataset message for 20 seconds...');
  PumpSleep(20*1000);

  WriteLn('Unsubscribing...');
  Subscriber.UnsubscribeAllDataSets;

  WriteLn('Waiting for 1 second...');
  // Unsubscribe operation is asynchronous, messages may still come for a short while.
  PumpSleep(1*1000);

  WriteLn('Finished.');
  FreeAndNil(Subscriber);
  FreeAndNil(SubscriberEventHandlers);
end;

procedure TSubscriberEventHandlers72.OnDataSetMessage(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyUADataSetMessageEventArgs);
var
  Count: Cardinal;
  DictionaryEntry2: _DictionaryEntry2;
  Element: OleVariant;
  FieldDataDictionaryEnumerator: IEnumVariant;
begin
  // Display the dataset.
  if eventArgs.Succeeded then
  begin
    // An event with null DataSetData just indicates a successful connection.
    if eventArgs.DataSetData <> nil then
    begin
      WriteLn;
      WriteLn('Dataset data: ', eventArgs.DataSetData.ToString);
      FieldDataDictionaryEnumerator := eventArgs.DataSetData.FieldDataDictionary.GetEnumerator;
      while (FieldDataDictionaryEnumerator.Next(1, Element, Count) = S_OK) do
      begin
        DictionaryEntry2 := IUnknown(Element) as _DictionaryEntry2;
        WriteLn(DictionaryEntry2.ToString);
      end;
    end;
  end
  else begin
    WriteLn;
      WriteLn('*** Failure: ', eventArgs.ErrorMessageBrief);
  end;
end;

// Example output:
//
//Subscribing...
//Processing dataset message events for 20 seconds...
//
//Dataset data: 2019-10-31T16:04:59.145,266,700,00; Good; Data; publisher=(UInt64)31, writer=1, fields: 4
//[#0, True {System.Boolean} @2019-10-31T16:04:59.145,266,700,00; Good]
//[#1, 0 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
//[#2, 767 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
//[#3, 10/31/2019 4:04:59 PM {System.DateTime} @2019-10-31T16:04:59.145,266,700,00; Good]
//
//Dataset data: 2019-10-31T16:04:59.170,047,500,00; Good; Data; publisher=(UInt64)31, writer=3, fields: 100
//[#0, 0 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#1, 100 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#2, 200 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#3, 300 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#4, 400 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#5, 500 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#6, 600 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#7, 700 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#8, 800 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#9, 900 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//[#10, 1000 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
//...
Rem This example shows how to feed the packet capture file into the PubSub subscriber, instead of connecting to the message
Rem oriented middleware (receiving the messages from the network).
Rem
Rem The OpcLabs.Pcap assembly needs to be referenced in your project (or otherwise made available, together with its
Rem dependencies) for the capture files to work. Refer to the documentation for more information.

Option Explicit

Const UAPublisherIdType_UInt64 = 4

' Define the PubSub connection we will work with.
Dim SubscribeDataSetArguments: Set SubscribeDataSetArguments = CreateObject("OpcLabs.EasyOpc.UA.PubSub.OperationModel.EasyUASubscribeDataSetArguments")
Dim ConnectionDescriptor: Set ConnectionDescriptor = SubscribeDataSetArguments.DataSetSubscriptionDescriptor.ConnectionDescriptor
ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = "opc.eth://FF-FF-FF-FF-FF-FF"
' Use packets from the specified Ethernet capture file. The file itself is at the root of the project, and we
' have specified that it has to be copied to the project's output directory.
' Note that .pcap is the default file name extension, and can thus be omitted.
ConnectionDescriptor.UseEthernetCaptureFile "UADemoPublisher-Ethernet.pcap"

' Alternative setup for Ethernet with VLAN tagging:
'ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = "opc.eth://FF-FF-FF-FF-FF-FF:2"
'ConnectionDescriptor.UseEthernetCaptureFile "UADemoPublisher-EthernetVlan.pcap"

' Alternative setup for UDP over IPv4:
'ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = "opc.udp://239.0.0.1"
'ConnectionDescriptor.UseUdpCaptureFile "UADemoPublisher-UDP.pcap"

' Alternative setup for UDP over IPv6:
'ConnectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = "opc.udp://[ff02::1]"
'ConnectionDescriptor.UseUdpCaptureFile "UADemoPublisher-UDP6.pcap"

' Define the arguments for subscribing to the dataset, where the filter is (unsigned 64-bit) publisher Id 31.
SubscribeDataSetArguments.DataSetSubscriptionDescriptor.Filter.PublisherId.SetIdentifier UAPublisherIdType_UInt64, 31

' Instantiate the subscriber object and hook events.
Dim Subscriber: Set Subscriber = CreateObject("OpcLabs.EasyOpc.UA.PubSub.EasyUASubscriber")
WScript.ConnectObject Subscriber, "Subscriber_"

WScript.Echo "Subscribing..."
Subscriber.SubscribeDataSet SubscribeDataSetArguments

WScript.Echo "Processing dataset message events for 20 seconds..."
WScript.Sleep 20*1000

WScript.Echo "Unsubscribing..."
Subscriber.UnsubscribeAllDataSets

WScript.Echo "Waiting for 1 second..."
' Unsubscribe operation is asynchronous, messages may still come for a short while.
WScript.Sleep 1*1000

WScript.Echo "Finished."



Sub Subscriber_DataSetMessage(Sender, e)
    ' Display the dataset.
    If e.Succeeded Then
        ' An event with null DataSetData just indicates a successful connection.
        If Not (e.DataSetData Is Nothing) Then
            WScript.Echo
            WScript.Echo "Dataset data: " & e.DataSetData
            Dim Pair: For Each Pair in e.DataSetData.FieldDataDictionary
                WScript.Echo Pair
            Next
        End If
    Else
        WScript.Echo
        WScript.Echo "*** Failure: " & e.ErrorMessageBrief
    End If
End Sub



' Example output:
'
'Subscribing...
'Processing dataset message events for 20 seconds...
'
'Dataset data: 2019-10-31T16:04:59.145,266,700,00; Good; Data; publisher=(UInt64)31, writer=1, fields: 4
'[#0, True {System.Boolean} @2019-10-31T16:04:59.145,266,700,00; Good]
'[#1, 0 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
'[#2, 767 {System.Int32} @2019-10-31T16:04:59.145,266,700,00; Good]
'[#3, 10/31/2019 4:04:59 PM {System.DateTime} @2019-10-31T16:04:59.145,266,700,00; Good]
'
'Dataset data: 2019-10-31T16:04:59.170,047,500,00; Good; Data; publisher=(UInt64)31, writer=3, fields: 100
'[#0, 0 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#1, 100 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#2, 200 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#3, 300 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#4, 400 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#5, 500 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#6, 600 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#7, 700 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#8, 800 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#9, 900 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'[#10, 1000 {System.Int64} @2019-10-31T16:04:59.170,047,500,00; Good]
'...
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