Connectivity Software User's Guide and Reference
Examples - OPC UA Specialized - Kepware KEPServerEX - Subscribe multiple monitored items

.NET

// This KEPServerEX/UA example shows how to subscribe to changes of multiple monitored items and display the value of the
// monitored item with each change.
//
// Find all latest examples here: https://www.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://forum.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.

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

namespace UADocExamples.Specialized
{
    partial class Kepware_KEPServerEX_UA
    {
        public static void SubscribeMultipleMonitoredItems()
        {
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:49320";  // default KEPServerEX/UA endpoint 

            // Instantiate the client object and hook events.
            var client = new EasyUAClient();
            client.DataChangeNotification += client_DataChangeNotification_Main1;

            Console.WriteLine("Subscribing...");
            client.SubscribeMultipleMonitoredItems(new[]
                {
                    // The UANodeId.KEPServerEX(...) helper method simplifies creation of node IDs for KEPServerEX/UA nodes.
                    new EasyUAMonitoredItemArguments(null, endpointDescriptor,
                        UANodeId.KEPServerEX("Simulation Examples.Functions.Random1"), 1000),
                    new EasyUAMonitoredItemArguments(null, endpointDescriptor,
                        UANodeId.KEPServerEX("Simulation Examples.Functions.Ramp1"), 1000),
                    new EasyUAMonitoredItemArguments(null, endpointDescriptor,
                        UANodeId.KEPServerEX("Simulation Examples.Functions.Sine1"), 1000),
                    new EasyUAMonitoredItemArguments(null, endpointDescriptor,
                        UANodeId.KEPServerEX("Simulation Examples.Functions.User1"), 1000)
                });

            Console.WriteLine("Processing monitored item changed events for 10 seconds...");
            System.Threading.Thread.Sleep(10 * 1000);

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

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

            Console.WriteLine("Finished.");
        }

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