Development Models > Imperative Programming Model > Imperative Programming Model for OPC Data (Classic and UA) > Subscribing to Information (OPC Data) > Unsubscribing (OPC Data) |
If you no longer want to receive item change notifications, you need to unsubscribe from them. In QuickOPC.NET (OPC Classic), to unsubscribe from a single OPC item, call the UnsubscribeItem method, passing it the subscription handle. In QuickOPC-UA, to unsubscribe from a single monitored item, call the UnsubscribeMonitoredItem method, passing it the subscription handle as well.
Examples for OPC Classic:
// This example shows how subscribe to changes of multiple items, and unsubscribe from one of them. using System; using System.Threading; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { class UnsubscribeItem { public static void Main1() { // Instantiate the client object. using (var client = new EasyDAClient()) { client.ItemChanged += client_ItemChanged; int[] handleArray = client.SubscribeMultipleItems( new[] { new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, null) }); Console.WriteLine("Processing item changed events for 30 seconds..."); Thread.Sleep(30 * 1000); Console.WriteLine("Unsubscribing from the first item..."); client.UnsubscribeItem(handleArray[0]); Console.WriteLine(); Console.WriteLine("Processing item changed events for 30 seconds..."); Thread.Sleep(30 * 1000); } } // Item changed event handler static void client_ItemChanged(object sender, EasyDAItemChangedEventArgs e) { if (e.Succeeded) Console.WriteLine("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq); else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.ItemDescriptor.ItemId, e.ErrorMessageBrief); } } }
# This example shows how subscribe to changes of multiple items, and unsubscribe from one of them. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * # Item changed event handler. def itemChanged(sender, e): if e.Succeeded: print(e.Arguments.ItemDescriptor.ItemId, ': ', e.Vtq, sep='') else: print(e.Arguments.ItemDescriptor.ItemId, ' *** Failure: ', e.ErrorMessageBrief, sep='') # Instantiate the client object. client = EasyDAClient() client.ItemChanged += itemChanged print('Subscribing item changes...') handleArray = client.SubscribeMultipleItems([ EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Ramp (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Sine (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 1000, None), ]) for i in range(len(handleArray)): print('handleArray[', i, ']: ', handleArray[i], sep='') print('Processing item change notifications for 30 seconds...') time.sleep(30) print('Unsubscribing from the first item...') IEasyDAClientExtension.UnsubscribeItem(client, handleArray[0]) print('Processing item change notifications for 30 seconds...') time.sleep(30) client.ItemChanged -= itemChanged print('Finished.')
' This example shows how subscribe to changes of multiple items, and unsubscribe from one of them. Imports System.Threading Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.DataAccess.OperationModel Namespace DataAccess._EasyDAClient Partial Friend Class UnsubscribeItem Shared Sub Main1() Using client = New EasyDAClient() AddHandler client.ItemChanged, AddressOf client_ItemChanged Dim handleArray = client.SubscribeMultipleItems( New DAItemGroupArguments() _ { _ New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, Nothing), New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, Nothing), New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, Nothing), New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, Nothing) }) Console.WriteLine("Processing item changed events for 30 seconds...") Thread.Sleep(30 * 1000) Console.WriteLine("Unsubscribing from the first item...") client.UnsubscribeItem(handleArray(0)) Console.WriteLine() Console.WriteLine("Processing item changed events for 30 seconds...") Thread.Sleep(30 * 1000) End Using End Sub ' Item changed event handler Private Shared Sub client_ItemChanged(sender As Object, e As EasyDAItemChangedEventArgs) If e.Succeeded Then Console.WriteLine("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq) Else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.ItemDescriptor.ItemId, e.ErrorMessageBrief) End If End Sub End Class End Namespace
Rem This example shows how unsubscribe from changes of a single item. Option Explicit Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") WScript.ConnectObject Client, "Client_" WScript.Echo "Subscribing..." Dim handle: handle = Client.SubscribeItem("", "OPCLabs.KitServer.2", "Simulation.Random", 1000) WScript.Echo "Waiting for 10 seconds..." WScript.Sleep 10*1000 WScript.Echo "Unsubscribing..." Client.UnsubscribeItem handle WScript.Echo "Waiting for 10 seconds..." WScript.Sleep 10*1000 Sub Client_ItemChanged(Sender, e) If Not (e.Succeeded) Then WScript.Echo "*** Failure: " & e.ErrorMessageBrief Exit Sub End If WScript.Echo e.Vtq End Sub
Examples for OPC UA:
// This example shows how unsubscribe from changes of a single monitored item. using System; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.OperationModel; namespace UADocExamples._EasyUAClient { class UnsubscribeMonitoredItem { public static void Main1() { 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/" // Instantiate the client object and hook events var client = new EasyUAClient(); client.DataChangeNotification += client_DataChangeNotification; Console.WriteLine("Subscribing..."); int handle = client.SubscribeDataChange( endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000); Console.WriteLine("Processing monitored item changed events for 10 seconds..."); System.Threading.Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing..."); client.UnsubscribeMonitoredItem(handle); Console.WriteLine("Waiting for 5 seconds..."); System.Threading.Thread.Sleep(5 * 1000); } static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e) { // Display value if (e.Succeeded) Console.WriteLine("{0}", e.AttributeData.Value); else Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief); } } }
# This example shows how unsubscribe from changes of a single monitored item. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * def dataChangeNotification(sender, e): # Display value. if e.Succeeded: print('Value: ', e.AttributeData.Value, sep='') else: print('*** Failure: ', e.ErrorMessageBrief, sep='') endpointDescriptor = UAEndpointDescriptor('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/' # Instantiate the client object and hook events. client = EasyUAClient() client.DataChangeNotification += dataChangeNotification print('Subscribing...') handle = IEasyUAClientExtension.SubscribeDataChange(client, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'), 1000) print('Processing data change events for 10 seconds...') time.sleep(10) print('Unsubscribing...') IEasyUAClientExtension.UnsubscribeMonitoredItem(client, handle) print('Waiting for 5 seconds...') time.sleep(5) print('Finished.')
' This example shows how unsubscribe from changes of a single monitored item. Imports OpcLabs.EasyOpc.UA Imports OpcLabs.EasyOpc.UA.OperationModel Namespace _EasyUAClient Friend Class UnsubscribeMonitoredItem Public Shared Sub Main1() ' Define which server we will work with. Dim endpointDescriptor As UAEndpointDescriptor = "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/" ' Instantiate the client object and hook events Dim client = New EasyUAClient() AddHandler client.DataChangeNotification, AddressOf client_DataChangeNotification Console.WriteLine("Subscribing...") Dim handle As Integer = client.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000) Console.WriteLine("Processing monitored item changed events for 10 seconds...") Threading.Thread.Sleep(10 * 1000) Console.WriteLine("Unsubscribing...") client.UnsubscribeMonitoredItem(handle) Console.WriteLine("Waiting for 5 seconds...") Threading.Thread.Sleep(5 * 1000) End Sub Private Shared Sub client_DataChangeNotification(ByVal sender As Object, ByVal e As EasyUADataChangeNotificationEventArgs) ' Display value If e.Succeeded Then Console.WriteLine("Value: {0}", e.AttributeData.Value) Else Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief) End If End Sub End Class End Namespace
Rem This example shows how unsubscribe from changes of a single monitored item. 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..." Dim handle: handle = 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 10 seconds..." WScript.Sleep 10*1000 WScript.Echo "Unsubscribing..." Client.UnsubscribeMonitoredItem handle WScript.Echo "Waiting for 5 seconds..." WScript.Sleep 5 * 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
In QuickOPC.NET (OPC Classic), to unsubscribe from multiple OPC items in an efficient manner, call the UnsubscribeMultipleItems method (instead of calling UnsubscribeItem in a loop), passing it an array of subscription handles. In QuickOPC-UA, to unsubscribe from multiple monitored items in an efficient manner, call the UnsubscribeMultipleMonitoredItems method (instead of calling UnsubscribeMonitoredItem in a loop), passing it an array of subscription handles.
Examples for OPC Classic:
// This example shows how to unsubscribe from changes of multiple items. using System; using System.Threading; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { class UnsubscribeMultipleItems { public static void Main1() { // Instantiate the client object. using (var client = new EasyDAClient()) { client.ItemChanged += client_ItemChanged; Console.WriteLine("Subscribing..."); int[] handleArray = client.SubscribeMultipleItems( new[] { new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, null) }); for (int i = 0; i < handleArray.Length; i++) Console.WriteLine($"handleArray({i}): {handleArray[i]}"); Console.WriteLine("Processing item changed events for 10 seconds..."); Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing from two items..."); client.UnsubscribeMultipleItems(new [] {handleArray[1], handleArray[2]}); Console.WriteLine("Processing item changed events for 10 seconds..."); Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing from all remaining items..."); client.UnsubscribeAllItems(); Console.WriteLine("Waiting for 5 seconds..."); Thread.Sleep(5 * 1000); } } // Item changed event handler static void client_ItemChanged(object sender, EasyDAItemChangedEventArgs e) { if (e.Succeeded) Console.WriteLine("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq); else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.ItemDescriptor.ItemId, e.ErrorMessageBrief); } // Example output: // //Subscribing... //handleArray(0): 12586002 //handleArray(1) : 12586004 //handleArray(2) : 12586005 //handleArray(3) : 12586006 //Processing item changed events for 10 seconds... //Simulation.Register_I4: 0 {System.Int32} @1601-01-01T00:00:00.000; GoodNonspecific(192) //Trends.Ramp(1 min): 0.585198730230331 {System.Double} @2020-04-10T15:50:35.111; GoodNonspecific(192) //Simulation.Random: 0.00125125888851588 {System.Double} @2020-04-10T15:50:35.111; GoodNonspecific(192) //Trends.Sine(1 min): -0.51011579179648 {System.Double} @2020-04-10T15:50:35.111; GoodNonspecific(192) //Trends.Ramp(1 min): 0.601875007152557 {System.Double} @2020-04-10T15:50:36.112; GoodNonspecific(192) //Simulation.Random: 0.56358531449324 {System.Double} @2020-04-10T15:50:36.112; GoodNonspecific(192) //Trends.Sine(1 min): -0.597275285519755 {System.Double} @2020-04-10T15:50:36.112; GoodNonspecific(192) //Trends.Ramp(1 min): 0.618548572063446 {System.Double} @2020-04-10T15:50:37.112; GoodNonspecific(192) //Simulation.Random: 0.193304239020966 {System.Double} @2020-04-10T15:50:37.112; GoodNonspecific(192) //Trends.Sine(1 min): -0.677870836926452 {System.Double} @2020-04-10T15:50:37.112; GoodNonspecific(192) //Trends.Ramp(1 min): 0.635226935148239 {System.Double} @2020-04-10T15:50:38.113; GoodNonspecific(192) //Simulation.Random: 0.808740501113926 {System.Double} @2020-04-10T15:50:38.113; GoodNonspecific(192) //Trends.Sine(1 min): -0.751053255223256 {System.Double} @2020-04-10T15:50:38.113; GoodNonspecific(192) //Trends.Ramp(1 min): 0.65190464258194 {System.Double} @2020-04-10T15:50:39.114; GoodNonspecific(192) //Simulation.Random: 0.58500930814539 {System.Double} @2020-04-10T15:50:39.114; GoodNonspecific(192) //Trends.Sine(1 min): -0.815993052494079 {System.Double} @2020-04-10T15:50:39.114; GoodNonspecific(192) //Trends.Ramp(1 min): 0.668579548597336 {System.Double} @2020-04-10T15:50:40.114; GoodNonspecific(192) //Simulation.Random: 0.47987304300058 {System.Double} @2020-04-10T15:50:40.114; GoodNonspecific(192) //Trends.Sine(1 min): -0.87197220432209 {System.Double} @2020-04-10T15:50:40.114; GoodNonspecific(192) //Trends.Ramp(1 min): 0.68526017665863 {System.Double} @2020-04-10T15:50:41.115; GoodNonspecific(192) //Simulation.Random: 0.350291451765496 {System.Double} @2020-04-10T15:50:41.115; GoodNonspecific(192) //Trends.Sine(1 min): -0.918402631917152 {System.Double} @2020-04-10T15:50:41.115; GoodNonspecific(192) //Trends.Ramp(1 min): 0.701931446790695 {System.Double} @2020-04-10T15:50:42.115; GoodNonspecific(192) //Simulation.Random: 0.895962401196326 {System.Double} @2020-04-10T15:50:42.115; GoodNonspecific(192) //Trends.Sine(1 min): -0.954736510704127 {System.Double} @2020-04-10T15:50:42.115; GoodNonspecific(192) //Trends.Ramp(1 min): 0.718607157468796 {System.Double} @2020-04-10T15:50:43.116; GoodNonspecific(192) //Simulation.Random: 0.822840052491836 {System.Double} @2020-04-10T15:50:43.116; GoodNonspecific(192) //Trends.Sine(1 min): -0.98060979065431 {System.Double} @2020-04-10T15:50:43.116; GoodNonspecific(192) //Trends.Ramp(1 min): 0.735284030437469 {System.Double} @2020-04-10T15:50:44.117; GoodNonspecific(192) //Simulation.Random: 0.746604815820795 {System.Double} @2020-04-10T15:50:44.117; GoodNonspecific(192) //Trends.Sine(1 min): -0.995728326344284 {System.Double} @2020-04-10T15:50:44.117; GoodNonspecific(192) //Unsubscribing from two items... //Processing item changed events for 10 seconds... //Simulation.Random: 0.174108096560564 { System.Double} @2020-04-10T15:50:45.117; GoodNonspecific(192) //Simulation.Random: 0.858943449201941 {System.Double} @2020-04-10T15:50:46.118; GoodNonspecific(192) //Simulation.Random: 0.710501419110691 {System.Double} @2020-04-10T15:50:47.118; GoodNonspecific(192) //Simulation.Random: 0.513534958952605 {System.Double} @2020-04-10T15:50:48.120; GoodNonspecific(192) //Simulation.Random: 0.303994872890408 {System.Double} @2020-04-10T15:50:49.120; GoodNonspecific(192) //Simulation.Random: 0.0149845881527146 {System.Double} @2020-04-10T15:50:50.121; GoodNonspecific(192) //Simulation.Random: 0.0914029358806116 {System.Double} @2020-04-10T15:50:51.122; GoodNonspecific(192) //Simulation.Random: 0.364452040162358 {System.Double} @2020-04-10T15:50:52.124; GoodNonspecific(192) //Simulation.Random: 0.147312845240638 {System.Double} @2020-04-10T15:50:53.125; GoodNonspecific(192) //Unsubscribing from all remaining items... //Waiting for 5 seconds... } }
# This example shows how to unsubscribe from changes of multiple items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from System.Collections.Generic import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * # Item changed event handler. def itemChanged(sender, e): if e.Succeeded: print(e.Arguments.ItemDescriptor.ItemId, ': ', e.Vtq, sep='') else: print(e.Arguments.ItemDescriptor.ItemId, ' *** Failure: ', e.ErrorMessageBrief, sep='') # Instantiate the client object. client = EasyDAClient() client.ItemChanged += itemChanged print('Subscribing item changes...') handleArray = client.SubscribeMultipleItems([ EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Ramp (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Sine (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 1000, None), ]) for i in range(len(handleArray)): print('handleArray[', i, ']: ', handleArray[i], sep='') print('Processing item change notifications for 10 seconds...') time.sleep(10) print('Unsubscribing from two items...') handleList = List[int]() handleList.Add(handleArray[1]) handleList.Add(handleArray[2]) client.UnsubscribeMultipleItems(handleList) print('Processing item change notifications for 10 seconds...') time.sleep(10) print('Unsubscribing from all remaining items...') client.UnsubscribeAllItems() print('Waiting for 5 seconds...') time.sleep(5) client.ItemChanged -= itemChanged print('Finished.')
Rem This example shows how to unsubscribe from changes of multiple items. Option Explicit Dim ItemSubscriptionArguments1: Set ItemSubscriptionArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments1.ItemDescriptor.ItemID = "Simulation.Random" ItemSubscriptionArguments1.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments2: Set ItemSubscriptionArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" ItemSubscriptionArguments2.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments3: Set ItemSubscriptionArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" ItemSubscriptionArguments3.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments4: Set ItemSubscriptionArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" ItemSubscriptionArguments4.GroupParameters.RequestedUpdateRate = 1000 Dim arguments(3) Set arguments(0) = ItemSubscriptionArguments1 Set arguments(1) = ItemSubscriptionArguments2 Set arguments(2) = ItemSubscriptionArguments3 Set arguments(3) = ItemSubscriptionArguments4 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") WScript.ConnectObject Client, "Client_" WScript.Echo "Subscribing..." Dim handleArray: handleArray = Client.SubscribeMultipleItems(arguments) Dim i: For i = LBound(handleArray) To UBound(handleArray) WScript.Echo "handleArray(" & i & "): " & handleArray(i) Next WScript.Echo "Processing item changed events for 10 seconds..." WScript.Sleep 10 * 1000 WScript.Echo "Unsubscribing from two items..." Dim handles1(1) handles1(0) = handleArray(1) handles1(1) = handleArray(2) Client.UnsubscribeMultipleItems handles1 WScript.Echo "Processing item changed events for 10 seconds..." WScript.Sleep 10 * 1000 WScript.Echo "Unsubscribing from all remaining items..." Client.UnsubscribeAllItems WScript.Echo "Waiting for 5 seconds..." WScript.Sleep 5 * 1000 Sub Client_ItemChanged(Sender, e) If Not (e.Succeeded) Then WScript.Echo "*** Failure: " & e.ErrorMessageBrief Exit Sub End If WScript.Echo e.Arguments.ItemDescriptor.ItemId & ": " & e.Vtq End Sub
// This example shows how to unsubscribe from changes of multiple items. class DEasyDAClientEvents { function ItemChanged($varSender, $varE) { if ($varE->Succeeded) { printf("s: s\n", $varE->Arguments->ItemDescriptor->ItemId, $varE->Vtq->ToString()); } else { printf("*** Failure: %s\n", $varE->ErrorMessageBrief); } } } $ItemSubscriptionArguments1 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments"); $ItemSubscriptionArguments1->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ItemSubscriptionArguments1->ItemDescriptor->ItemID = "Simulation.Random"; $ItemSubscriptionArguments1->GroupParameters->RequestedUpdateRate = 1000; $ItemSubscriptionArguments2 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments"); $ItemSubscriptionArguments2->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ItemSubscriptionArguments2->ItemDescriptor->ItemID = "Trends.Ramp (1 min)"; $ItemSubscriptionArguments2->GroupParameters->RequestedUpdateRate = 1000; $ItemSubscriptionArguments3 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments"); $ItemSubscriptionArguments3->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ItemSubscriptionArguments3->ItemDescriptor->ItemID = "Trends.Sine (1 min)"; $ItemSubscriptionArguments3->GroupParameters->RequestedUpdateRate = 1000; $ItemSubscriptionArguments4 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments"); $ItemSubscriptionArguments4->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ItemSubscriptionArguments4->ItemDescriptor->ItemID = "Simulation.Register_I4"; $ItemSubscriptionArguments4->GroupParameters->RequestedUpdateRate = 1000; $arguments[0] = $ItemSubscriptionArguments1; $arguments[1] = $ItemSubscriptionArguments2; $arguments[2] = $ItemSubscriptionArguments3; $arguments[3] = $ItemSubscriptionArguments4; $Client = new COM("OpcLabs.EasyOpc.DataAccess.EasyDAClient"); $Events = new DEasyDAClientEvents(); com_event_sink($Client, $Events, "DEasyDAClientEvents"); print "Subscribing...\n"; $handleArray = $Client->SubscribeMultipleItems($arguments); for ($i = 0; $i < count($handleArray); $i++) { printf("handleArray[d]s\n", $i, $handleArray[$i]); } print "Processing item changed events for 10 seconds...\n"; $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 10); print "Unsubscribing from two items...\n"; $handles1[0] = $handleArray[1]; $handles1[1] = $handleArray[2]; $Client->UnsubscribeMultipleItems($handles1); print "Processing item changed events for 10 seconds...\n"; $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 10); print "Unsubscribing from all remaining items...\n"; $Client->UnsubscribeAllItems; print "Waiting for 5 seconds...\n"; $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 5);
Examples for OPC UA:
// This example shows how to unsubscribe from changes of multiple items. using System; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.OperationModel; namespace UADocExamples._EasyUAClient { partial class UnsubscribeMultipleMonitoredItems { public static void Main1() { 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/" // Instantiate the client object and hook events. var client = new EasyUAClient(); client.DataChangeNotification += client_DataChangeNotification; Console.WriteLine("Subscribing..."); int[] handleArray = client.SubscribeMultipleMonitoredItems(new[] { new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", 1000) }); Console.WriteLine("Processing monitored item changed events for 10 seconds..."); System.Threading.Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing..."); client.UnsubscribeMultipleMonitoredItems(handleArray); Console.WriteLine("Waiting for 5 seconds..."); System.Threading.Thread.Sleep(5 * 1000); Console.WriteLine("Finished."); } static void client_DataChangeNotification(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}"); } } }
# This example shows how to unsubscribe from changes of multiple items. #requires -Version 5.1 using namespace OpcLabs.EasyOpc.UA using namespace OpcLabs.EasyOpc.UA.AddressSpace using namespace OpcLabs.EasyOpc.UA.OperationModel # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll" $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/" # Instantiate the client object. $client = New-Object EasyUAClient # Data change notification handler Register-ObjectEvent -InputObject $client -EventName DataChangeNotification -Action { if ($EventArgs.Succeeded) { Write-Host "$($EventArgs.Arguments.NodeDescriptor): $($EventArgs.AttributeData.Value)" } else { Write-Host "$($EventArgs.Arguments.NodeDescriptor) *** Failure: $($EventArgs.ErrorMessageBrief)" } } Write-Host "Subscribing..." $handleArray = $client.SubscribeMultipleMonitoredItems(@( (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10845")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10853")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10855")), 1000)) )) Write-Host "Processing data change events for 10 seconds..." $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() while ($stopwatch.Elapsed.TotalSeconds -lt 10) { Start-Sleep -Seconds 1 } Write-Host "Unsubscribing..." $client.UnsubscribeMultipleMonitoredItems($handleArray) Write-Host "Waiting for 5 seconds..." Start-Sleep -Seconds 5 Write-Host "Finished."
# This example shows how to unsubscribe from changes of multiple items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * def dataChangeNotification(sender, e): # Display value. if e.Succeeded: print(e.Arguments.NodeDescriptor, ': ', e.AttributeData.Value, sep='') else: print(e.Arguments.NodeDescriptor, ' *** Failure: ', e.ErrorMessageBrief, sep='') endpointDescriptor = UAEndpointDescriptor('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/' # Instantiate the client object and hook events. client = EasyUAClient() client.DataChangeNotification += dataChangeNotification print('Subscribing...') handleArray = client.SubscribeMultipleMonitoredItems([ EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10845'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10855'), UAMonitoringParameters(1000)), ]) print('Processing data change events for 10 seconds...') time.sleep(10) print('Unsubscribing...') client.UnsubscribeMultipleMonitoredItems(handleArray) print('Waiting for 5 seconds...') time.sleep(5) print('Finished.')
' This example shows how to unsubscribe from changes of multiple items. Imports OpcLabs.EasyOpc.UA Imports OpcLabs.EasyOpc.UA.OperationModel Namespace _EasyUAClient Friend Class UnsubscribeMultipleMonitoredItems Public Shared Sub Main1() ' Define which server we will work with. Dim endpointDescriptor As UAEndpointDescriptor = "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/" ' Instantiate the client object and hook events Dim client = New EasyUAClient() AddHandler client.DataChangeNotification, AddressOf client_DataChangeNotification Console.WriteLine("Subscribing...") Dim handleArray() As Integer = client.SubscribeMultipleMonitoredItems(New EasyUAMonitoredItemArguments() _ { _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", 1000), _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000), _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", 1000) _ } _ ) Console.WriteLine("Processing monitored item changed events for 10 seconds...") Threading.Thread.Sleep(10 * 1000) Console.WriteLine("Unsubscribing...") client.UnsubscribeMultipleMonitoredItems(handleArray) Console.WriteLine("Waiting for 5 seconds...") Threading.Thread.Sleep(5 * 1000) End Sub Private Shared Sub client_DataChangeNotification(ByVal sender As Object, ByVal e As EasyUADataChangeNotificationEventArgs) ' Display value If e.Succeeded Then Console.WriteLine("{0}: {1}", e.Arguments.NodeDescriptor, e.AttributeData.Value) Else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.NodeDescriptor, e.ErrorMessageBrief) End If End Sub End Class End Namespace
// This example shows how to unsubscribe from changes of multiple items. type TClientEventHandlers127 = class procedure OnDataChangeNotification( ASender: TObject; sender: OleVariant; const eventArgs: _EasyUADataChangeNotificationEventArgs); end; procedure TClientEventHandlers127.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 UnsubscribeMultipleMonitoredItems.Main; var Arguments: OleVariant; Client: TEasyUAClient; ClientEventHandlers: TClientEventHandlers127; Handle: Cardinal; HandleArray: OleVariant; HandleSafeArray: PSafeArray; I: Cardinal; MonitoredItemArguments1, MonitoredItemArguments2, MonitoredItemArguments3: _EasyUAMonitoredItemArguments; MonitoringParameters: _UAMonitoringParameters; begin // Instantiate the client object and hook events Client := TEasyUAClient.Create(nil); ClientEventHandlers := TClientEventHandlers127.Create; Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification; WriteLn('Subscribing...'); MonitoringParameters := CoUAMonitoringParameters.Create; MonitoringParameters.SamplingInterval := 1000; MonitoredItemArguments1 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments1.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments1.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10845'; MonitoredItemArguments1.MonitoringParameters := MonitoringParameters; MonitoredItemArguments2 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments2.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments2.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10853'; MonitoredItemArguments2.MonitoringParameters := MonitoringParameters; MonitoredItemArguments3 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments3.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments3.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10855'; MonitoredItemArguments3.MonitoringParameters := MonitoringParameters; Arguments := VarArrayCreate([0, 2], varVariant); Arguments[0] := MonitoredItemArguments1; Arguments[1] := MonitoredItemArguments2; Arguments[2] := MonitoredItemArguments3; HandleSafeArray := Client.SubscribeMultipleMonitoredItems(Arguments); TVarData(HandleArray).VType := varArray or varVariant; TVarData(HandleArray).VArray := PVarArray(HandleSafeArray); for I := VarArrayLowBound(HandleArray, 1) to VarArrayHighBound(HandleArray, 1) do begin Handle := Cardinal(HandleArray[I]); WriteLn('HandleArray[', I, ']: ', Handle); end; WriteLn('Processing monitored item changed events for 10 seconds...'); PumpSleep(10*1000); WriteLn('Unsubscribing...'); Client.UnsubscribeMultipleMonitoredItems(HandleArray); WriteLn('Waiting for 5 seconds...'); Sleep(5*1000); WriteLn('Finished.'); VarClear(HandleArray); VarClear(Arguments); FreeAndNil(Client); FreeAndNil(ClientEventHandlers); end;
Rem This example shows how to unsubscribe from changes of multiple items. 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..." Dim MonitoringParameters: Set MonitoringParameters = CreateObject("OpcLabs.EasyOpc.UA.UAMonitoringParameters") MonitoringParameters.SamplingInterval = 1000 Dim MonitoredItemArguments1: Set MonitoredItemArguments1 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments1.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments1.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10845" MonitoredItemArguments1.MonitoringParameters = MonitoringParameters Dim MonitoredItemArguments2: Set MonitoredItemArguments2 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments2.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments2.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10853" MonitoredItemArguments2.MonitoringParameters = MonitoringParameters Dim MonitoredItemArguments3: Set MonitoredItemArguments3 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments3.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments3.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10855" MonitoredItemArguments3.MonitoringParameters = MonitoringParameters Dim arguments(2) Set arguments(0) = MonitoredItemArguments1 Set arguments(1) = MonitoredItemArguments2 Set arguments(2) = MonitoredItemArguments3 Dim handleArray: handleArray = Client.SubscribeMultipleMonitoredItems(arguments) Dim i: For i = LBound(handleArray) To UBound(handleArray) WScript.Echo "handleArray(" & i & "): " & handleArray(i) Next WScript.Echo "Processing monitored item changed events for 10 seconds..." WScript.Sleep 10 * 1000 WScript.Echo "Unsubscribing from two monitored items..." Dim handles1(1) handles1(0) = handleArray(1) handles1(1) = handleArray(2) Client.UnsubscribeMultipleMonitoredItems handles1 WScript.Echo "Processing monitored item changed events for 10 seconds..." WScript.Sleep 10 * 1000 WScript.Echo "Unsubscribing from all remaining items..." Client.UnsubscribeAllMonitoredItems WScript.Echo "Waiting for 5 seconds..." WScript.Sleep 5 * 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 e.Arguments.NodeDescriptor & ":" & display End Sub
Obviously, when you are subscribing from multiple items, you can either choose to subscribe from the same set of items you used when subscribing (in which case you can simply pass the array of handles received from the SubscribeMultipleXXXX method to the UnsubscribeMultipleXXXX method), or, you can choose which items you unsubscribe from, picking just the handles you want.
Examples for OPC UA:
// This example shows how to unsubscribe from changes of just some monitored // items. using System; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.OperationModel; namespace UADocExamples._EasyUAClient { partial class UnsubscribeMultipleMonitoredItems { public static void Some() { 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/" // Instantiate the client object and hook events. var client = new EasyUAClient(); client.DataChangeNotification += client_DataChangeNotification2; Console.WriteLine(); Console.WriteLine("Subscribing..."); int[] handleArray = client.SubscribeMultipleMonitoredItems(new[] { new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", 1000) }); for (int i = 0; i < handleArray.Length; i++) Console.WriteLine($"handleArray[{i}]: {handleArray[i]}"); Console.WriteLine(); Console.WriteLine("Processing monitored item changed events for 10 seconds..."); System.Threading.Thread.Sleep(10 * 1000); Console.WriteLine(); Console.WriteLine("Unsubscribing from 2 monitored items..."); // We will unsubscribe from the first and third monitored item we have // previously subscribed to. client.UnsubscribeMultipleMonitoredItems(new [] {handleArray[0], handleArray[2]}); Console.WriteLine(); Console.WriteLine("Processing monitored item changed events for 10 seconds..."); System.Threading.Thread.Sleep(10 * 1000); Console.WriteLine(); Console.WriteLine("Unsubscribing from all remaining monitored items..."); client.UnsubscribeAllMonitoredItems(); Console.WriteLine("Waiting for 5 seconds..."); System.Threading.Thread.Sleep(5 * 1000); Console.WriteLine("Finished."); } static void client_DataChangeNotification2(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}"); } } }
# This example shows how to unsubscribe from changes of multiple items. #requires -Version 5.1 using namespace OpcLabs.EasyOpc.UA using namespace OpcLabs.EasyOpc.UA.AddressSpace using namespace OpcLabs.EasyOpc.UA.OperationModel # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll" $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/" # Instantiate the client object. $client = New-Object EasyUAClient # Data change notification handler Register-ObjectEvent -InputObject $client -EventName DataChangeNotification -Action { if ($EventArgs.Succeeded) { Write-Host "$($EventArgs.Arguments.NodeDescriptor): $($EventArgs.AttributeData.Value)" } else { Write-Host "$($EventArgs.Arguments.NodeDescriptor) *** Failure: $($EventArgs.ErrorMessageBrief)" } } Write-Host "Subscribing..." $handleArray = $client.SubscribeMultipleMonitoredItems(@( (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10845")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10853")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10855")), 1000)) )) for ($i = 0; $i -lt $handleArray.Length; $i++) { Write-Host "handleArray[$($i)]: $($handleArray[$i])" } Write-Host Write-Host "Processing data change events for 10 seconds..." $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() while ($stopwatch.Elapsed.TotalSeconds -lt 10) { Start-Sleep -Seconds 1 } Write-Host Write-Host "Unsubscribing from 2 monitored items..." # We will unsubscribe from the first and third monitored item we have # previously subscribed to. $client.UnsubscribeMultipleMonitoredItems([int[]]@($handleArray[0], $handleArray[2])) Write-Host Write-Host "Processing data change events for 10 seconds..." $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() while ($stopwatch.Elapsed.TotalSeconds -lt 10) { Start-Sleep -Seconds 1 } Write-Host Write-Host "Unsubscribing from all remaining monitored items..." $client.UnsubscribeAllMonitoredItems() Write-Host "Waiting for 5 seconds..." Start-Sleep -Seconds 5 Write-Host "Finished."
# This example shows how to unsubscribe from changes of just some monitored # items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from System.Collections.Generic import * from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * def dataChangeNotification(sender, e): # Display value. if e.Succeeded: print(e.Arguments.NodeDescriptor, ': ', e.AttributeData.Value, sep='') else: print(e.Arguments.NodeDescriptor, ' *** Failure: ', e.ErrorMessageBrief, sep='') endpointDescriptor = UAEndpointDescriptor('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/' # Instantiate the client object and hook events. client = EasyUAClient() client.DataChangeNotification += dataChangeNotification print('Subscribing...') handleArray = client.SubscribeMultipleMonitoredItems([ EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10845'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10855'), UAMonitoringParameters(1000)), ]) for i, handle in enumerate(handleArray): print('handleArray[', i, ']: ', handle, sep='') print() print('Processing data change events for 10 seconds...') time.sleep(10) print() print('Unsubscribing from 2 monitored items...') handleList = List[int]() handleList.Add(handleArray[0]) handleList.Add(handleArray[2]) client.UnsubscribeMultipleMonitoredItems(handleList) print() print('Processing data change events for 10 seconds...') time.sleep(10) print() print('Unsubscribing from all remaining monitored items...') client.UnsubscribeAllMonitoredItems() print('Waiting for 5 seconds...') time.sleep(5) print('Finished.')
// This example shows how to unsubscribe from changes of just some monitored // items. type TClientEventHandlers128 = class procedure OnDataChangeNotification( ASender: TObject; sender: OleVariant; const eventArgs: _EasyUADataChangeNotificationEventArgs); end; procedure TClientEventHandlers128.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 UnsubscribeMultipleMonitoredItems.Some; var Arguments: OleVariant; Client: TEasyUAClient; ClientEventHandlers: TClientEventHandlers128; Handle: Cardinal; HandleArray: OleVariant; HandleSafeArray: PSafeArray; I: Cardinal; MonitoredItemArguments1, MonitoredItemArguments2, MonitoredItemArguments3: _EasyUAMonitoredItemArguments; MonitoringParameters: _UAMonitoringParameters; SelectedHandles: OleVariant; begin // Instantiate the client object and hook events Client := TEasyUAClient.Create(nil); ClientEventHandlers := TClientEventHandlers128.Create; Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification; WriteLn; WriteLn('Subscribing...'); MonitoringParameters := CoUAMonitoringParameters.Create; MonitoringParameters.SamplingInterval := 1000; MonitoredItemArguments1 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments1.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments1.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10845'; MonitoredItemArguments1.MonitoringParameters := MonitoringParameters; MonitoredItemArguments2 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments2.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments2.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10853'; MonitoredItemArguments2.MonitoringParameters := MonitoringParameters; MonitoredItemArguments3 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments3.EndpointDescriptor.UrlString := //'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'; MonitoredItemArguments3.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10855'; MonitoredItemArguments3.MonitoringParameters := MonitoringParameters; Arguments := VarArrayCreate([0, 2], varVariant); Arguments[0] := MonitoredItemArguments1; Arguments[1] := MonitoredItemArguments2; Arguments[2] := MonitoredItemArguments3; HandleSafeArray := Client.SubscribeMultipleMonitoredItems(Arguments); TVarData(HandleArray).VType := varArray or varVariant; TVarData(HandleArray).VArray := PVarArray(HandleSafeArray); for I := VarArrayLowBound(HandleArray, 1) to VarArrayHighBound(HandleArray, 1) do begin Handle := Cardinal(HandleArray[I]); WriteLn('HandleArray[', I, ']: ', Handle); end; WriteLn; WriteLn('Processing monitored item changed events for 10 seconds...'); PumpSleep(10*1000); WriteLn; WriteLn('Unsubscribing from 2 monitored items...'); SelectedHandles := VarArrayCreate([0, 1], varVariant); // We will unsubscribe from the first and third monitored item we have // previously subscribed to. SelectedHandles[0] := HandleArray[0]; SelectedHandles[1] := HandleArray[2]; Client.UnsubscribeMultipleMonitoredItems(SelectedHandles); WriteLn; WriteLn('Processing monitored item changed events for 10 seconds...'); PumpSleep(10*1000); WriteLn; WriteLn('Unsubscribing from all remaining monitored items...'); Client.UnsubscribeAllMonitoredItems; WriteLn; WriteLn('Waiting for 5 seconds...'); Sleep(5*1000); WriteLn; WriteLn('Finished.'); VarClear(HandleArray); VarClear(Arguments); FreeAndNil(Client); FreeAndNil(ClientEventHandlers); end;
// This example shows how to unsubscribe from changes of just some monitored // items. class ClientEvents { function DataChangeNotification($Sender, $E) { // Display the data if ($E->Succeeded) printf("s: s\n", $E->Arguments->NodeDescriptor, $E->AttributeData); else printf("s *** Failures\n", $E->Arguments->NodeDescriptor, $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("\n"); printf("Subscribing...\n"); $MonitoringParameters = new COM("OpcLabs.EasyOpc.UA.UAMonitoringParameters"); $MonitoringParameters->SamplingInterval = 1000; $MonitoredItemArguments1 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments1->EndpointDescriptor->UrlString = //"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"; $MonitoredItemArguments1->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10845"; $MonitoredItemArguments1->MonitoringParameters = $MonitoringParameters; $MonitoredItemArguments2 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments2->EndpointDescriptor->UrlString = //"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"; $MonitoredItemArguments2->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10853"; $MonitoredItemArguments2->MonitoringParameters = $MonitoringParameters; $MonitoredItemArguments3 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments3->EndpointDescriptor->UrlString = //"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"; $MonitoredItemArguments3->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10855"; $MonitoredItemArguments3->MonitoringParameters = $MonitoringParameters; $arguments[0] = $MonitoredItemArguments1; $arguments[1] = $MonitoredItemArguments2; $arguments[2] = $MonitoredItemArguments3; $handleArray = $Client->SubscribeMultipleMonitoredItems($arguments); for ($i = 0; $i < count($handleArray); $i++) { printf("handleArray[d]d\n", $i, $handleArray[$i]); } printf("\n"); printf("Processing monitored item changed events for 10 seconds...\n"); $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 10); printf("\n"); printf("Unsubscribing from 2 monitored items...\n"); $SelectedHandles[0] = $handleArray[0]; $SelectedHandles[1] = $handleArray[2]; $Client->UnsubscribeMultipleMonitoredItems($SelectedHandles); printf("\n"); printf("Processing monitored item changed events for 10 seconds...\n"); $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 10); printf("\n"); printf("Unsubscribing from all remaining monitored items...\n"); $Client->UnsubscribeAllMonitoredItems; printf("\n"); printf("Waiting for 5 seconds...\n"); $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 5);
Rem This example shows how to unsubscribe from changes of just some monitored Rem items. ' The client object, with events 'Public WithEvents Client8 As EasyUAClient Public Sub UnsubscribeMultipleMonitoredItems_Some_Command_Click() OutputText = "" Set Client8 = New EasyUAClient OutputText = OutputText & "Subscribing..." & vbCrLf Dim MonitoringParameters As New UAMonitoringParameters MonitoringParameters.SamplingInterval = 1000 Dim MonitoredItemArguments1 As New EasyUAMonitoredItemArguments MonitoredItemArguments1.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments1.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10845" Set MonitoredItemArguments1.MonitoringParameters = MonitoringParameters MonitoredItemArguments1.SetState ("Item1") Dim MonitoredItemArguments2 As New EasyUAMonitoredItemArguments MonitoredItemArguments2.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments2.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10853" Set MonitoredItemArguments2.MonitoringParameters = MonitoringParameters MonitoredItemArguments2.SetState ("Item2") Dim MonitoredItemArguments3 As New EasyUAMonitoredItemArguments MonitoredItemArguments3.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments3.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10855" Set MonitoredItemArguments3.MonitoringParameters = MonitoringParameters MonitoredItemArguments3.SetState ("Item3") Dim arguments(2) As Variant Set arguments(0) = MonitoredItemArguments1 Set arguments(1) = MonitoredItemArguments2 Set arguments(2) = MonitoredItemArguments3 Dim handleArray As Variant handleArray = Client8.SubscribeMultipleMonitoredItems(arguments) Dim i As Long: For i = LBound(handleArray) To UBound(handleArray) OutputText = OutputText & "handleArray(" & i & "): " & handleArray(i) & vbCrLf Next OutputText = OutputText & vbCrLf OutputText = OutputText & "Processing monitored item changed events for 10 seconds..." & vbCrLf Pause 10000 OutputText = OutputText & vbCrLf OutputText = OutputText & "Unsubscribing from 2 monitored items..." & vbCrLf Dim SelectedHandles(1) As Variant ' We will unsubscribe from the first and third monitored item we have ' previously subscribed to. SelectedHandles(0) = handleArray(0) SelectedHandles(1) = handleArray(2) Client8.UnsubscribeMultipleMonitoredItems (SelectedHandles) OutputText = OutputText & vbCrLf OutputText = OutputText & "Processing monitored item changed events for 10 seconds..." & vbCrLf Pause 10000 OutputText = OutputText & vbCrLf OutputText = OutputText & "Unsubscribing from all remaining monitored items..." & vbCrLf Call Client8.UnsubscribeAllMonitoredItems OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf Pause 5000 Set Client8 = Nothing OutputText = OutputText & "Finished." & vbCrLf End Sub Public Sub Client8_DataChangeNotification(ByVal sender As Variant, ByVal eventArgs As EasyUADataChangeNotificationEventArgs) ' Display the data If eventArgs.Succeeded Then OutputText = OutputText & eventArgs.arguments.nodeDescriptor & ": " & eventArgs.AttributeData & vbCrLf Else OutputText = OutputText & eventArgs.arguments.nodeDescriptor & ": " & eventArgs.ErrorMessageBrief & vbCrLf End If End Sub
In QuickOPC Classic, you can also unsubscribe from all items you have previously subscribed to (on the same instance of EasyDAClient object) by calling the UnsubscribeAllItems method. In QuickOPC-UA, you can unsubscribe from all monitored items you have previously subscribed to (on the same instance of EasyUAClient object) by calling the UnsubscribeAllMonitoredItems method.
Examples for OPC Classic:
// This example shows how to unsubscribe from changes of all items. using System; using System.Threading; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { class UnsubscribeAllItems { public static void Main1() { // Instantiate the client object. using (var client = new EasyDAClient()) { client.ItemChanged += client_ItemChanged; Console.WriteLine("Subscribing..."); /*int[] handleArray = */client.SubscribeMultipleItems( new[] { new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000, null), new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000, null) }); Console.WriteLine("Processing item changed events for 10 seconds..."); Thread.Sleep(10 * 1000); Console.WriteLine("Unsubscribing from all items..."); client.UnsubscribeAllItems(); Console.WriteLine("Waiting for 10 seconds..."); Thread.Sleep(10 * 1000); } } // Item changed event handler static void client_ItemChanged(object sender, EasyDAItemChangedEventArgs e) { if (e.Succeeded) Console.WriteLine("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq); else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.ItemDescriptor.ItemId, e.ErrorMessageBrief); } } }
# This example shows how to unsubscribe from changes of all items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * # Item changed event handler. def itemChanged(sender, e): if e.Succeeded: print(e.Arguments.ItemDescriptor.ItemId, ': ', e.Vtq, sep='') else: print(e.Arguments.ItemDescriptor.ItemId, ' *** Failure: ', e.ErrorMessageBrief, sep='') # Instantiate the client object. client = EasyDAClient() client.ItemChanged += itemChanged print('Subscribing item changes...') client.SubscribeMultipleItems([ EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Ramp (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Trends.Sine (1 min)', 1000, None), EasyDAItemSubscriptionArguments('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 1000, None), ]) print('Processing item change notifications for 10 seconds...') time.sleep(10) print('Unsubscribing from all items...') client.UnsubscribeAllItems() print('Waiting for 10 seconds...') time.sleep(10) client.ItemChanged -= itemChanged print('Finished.')
Rem This example shows how to unsubscribe from changes of all items. Option Explicit Dim ItemSubscriptionArguments1: Set ItemSubscriptionArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments1.ItemDescriptor.ItemID = "Simulation.Random" ItemSubscriptionArguments1.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments2: Set ItemSubscriptionArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" ItemSubscriptionArguments2.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments3: Set ItemSubscriptionArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" ItemSubscriptionArguments3.GroupParameters.RequestedUpdateRate = 1000 Dim ItemSubscriptionArguments4: Set ItemSubscriptionArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments") ItemSubscriptionArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ItemSubscriptionArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" ItemSubscriptionArguments4.GroupParameters.RequestedUpdateRate = 1000 Dim arguments(3) Set arguments(0) = ItemSubscriptionArguments1 Set arguments(1) = ItemSubscriptionArguments2 Set arguments(2) = ItemSubscriptionArguments3 Set arguments(3) = ItemSubscriptionArguments4 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") WScript.ConnectObject Client, "Client_" WScript.Echo "Subscribing..." Dim handleArray: handleArray = Client.SubscribeMultipleItems(arguments) WScript.Echo "Processing item changed events for 10 seconds..." WScript.Sleep 10*1000 WScript.Echo ""Unsubscribing from all items..." Client.UnsubscribeAllItems WScript.Echo "Waiting for 10 seconds..." WScript.Sleep 10*1000 Sub Client_ItemChanged(Sender, e) If Not (e.Succeeded) Then WScript.Echo "*** Failure: " & e.ErrorMessageBrief Exit Sub End If WScript.Echo e.Arguments.ItemDescriptor.ItemId & ": " & e.Vtq End Sub
Examples for OPC UA:
// This example shows how to unsubscribe from changes of all items. using System; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.OperationModel; namespace UADocExamples._EasyUAClient { class UnsubscribeAllMonitoredItems { public static void Main1() { 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/" // Instantiate the client object and hook events. var client = new EasyUAClient(); client.DataChangeNotification += client_DataChangeNotification; Console.WriteLine("Subscribing..."); client.SubscribeMultipleMonitoredItems(new[] { new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000), new EasyUAMonitoredItemArguments(null, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", 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(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}"); } } }
# This example shows how to unsubscribe from changes of all items. #requires -Version 5.1 using namespace OpcLabs.EasyOpc.UA using namespace OpcLabs.EasyOpc.UA.OperationModel # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll" [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/" # Instantiate the client object. $client = New-Object EasyUAClient # Data change notification handler Register-ObjectEvent -InputObject $client -EventName DataChangeNotification -Action { # Display value. if ($EventArgs.Succeeded) { Write-Host "$($EventArgs.Arguments.NodeDescriptor): $($EventArgs.AttributeData.Value)" } else { Write-Host "$($EventArgs.Arguments.NodeDescriptor) *** Failure: $($EventArgs.ErrorMessageBrief)" } } Write-Host "Subscribing..." $handleArray = $client.SubscribeMultipleMonitoredItems(@( (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10845")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10853")), 1000)), (New-Object UAMonitoredItemArguments( (New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10855")), 1000)) )) Write-Host Write-Host "Processing monitored item changed events for 10 seconds..." $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() while ($stopwatch.Elapsed.TotalSeconds -lt 10) { Start-Sleep -Seconds 1 } Write-Host "Unsubscribing..." $client.UnsubscribeAllMonitoredItems() Write-Host "Waiting for 5 seconds..." Start-Sleep -Seconds 5 Write-Host "Finished."
# This example shows how to unsubscribe from changes of all items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * def dataChangeNotification(sender, e): # Display value. if e.Succeeded: print(e.Arguments.NodeDescriptor, ': ', e.AttributeData.Value, sep='') else: print(e.Arguments.NodeDescriptor, ' *** Failure: ', e.ErrorMessageBrief, sep='') endpointDescriptor = UAEndpointDescriptor('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/' # Instantiate the client object and hook events. client = EasyUAClient() client.DataChangeNotification += dataChangeNotification print('Subscribing...') client.SubscribeMultipleMonitoredItems([ EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10845'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'), UAMonitoringParameters(1000)), EasyUAMonitoredItemArguments( None, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10855'), UAMonitoringParameters(1000)), ]) print('Processing data change events for 10 seconds...') time.sleep(10) print('Unsubscribing all monitored items...') client.UnsubscribeAllMonitoredItems() print('Waiting for 5 seconds...') time.sleep(5) print('Finished.')
' This example shows how to unsubscribe from changes of all items. Imports OpcLabs.EasyOpc.UA Imports OpcLabs.EasyOpc.UA.OperationModel Namespace _EasyUAClient Friend Class UnsubscribeAllMonitoredItems Public Shared Sub Main1() ' Define which server we will work with. Dim endpointDescriptor As UAEndpointDescriptor = "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/" ' Instantiate the client object and hook events Dim client = New EasyUAClient() AddHandler client.DataChangeNotification, AddressOf client_DataChangeNotification Console.WriteLine("Subscribing...") client.SubscribeMultipleMonitoredItems(New EasyUAMonitoredItemArguments() _ { _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", 1000), _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000), _ New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", 1000) _ } _ ) Console.WriteLine("Processing monitored item changed events for 10 seconds...") Threading.Thread.Sleep(10 * 1000) Console.WriteLine("Unsubscribing...") client.UnsubscribeAllMonitoredItems() Console.WriteLine("Waiting for 5 seconds...") Threading.Thread.Sleep(5 * 1000) End Sub Private Shared Sub client_DataChangeNotification(ByVal sender As Object, ByVal e As EasyUADataChangeNotificationEventArgs) ' Display value If e.Succeeded Then Console.WriteLine("{0}: {1}", e.Arguments.NodeDescriptor, e.AttributeData.Value) Else Console.WriteLine("{0} *** Failure: {1}", e.Arguments.NodeDescriptor, e.ErrorMessageBrief) End If End Sub End Class End Namespace
// This example shows how to unsubscribe from changes of all items. type TClientEventHandlers126 = class procedure OnDataChangeNotification( ASender: TObject; sender: OleVariant; const eventArgs: _EasyUADataChangeNotificationEventArgs); end; procedure TClientEventHandlers126.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 UnsubscribeAllMonitoredItems.Main; var Arguments: OleVariant; Client: TEasyUAClient; ClientEventHandlers: TClientEventHandlers126; EndpointDescriptor: string; MonitoredItemArguments1, MonitoredItemArguments2, MonitoredItemArguments3: _EasyUAMonitoredItemArguments; MonitoringParameters: _UAMonitoringParameters; begin 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'; // Instantiate the client object and hook events Client := TEasyUAClient.Create(nil); ClientEventHandlers := TClientEventHandlers126.Create; Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification; WriteLn('Subscribing...'); MonitoringParameters := CoUAMonitoringParameters.Create; MonitoringParameters.SamplingInterval := 1000; MonitoredItemArguments1 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments1.EndpointDescriptor.UrlString := EndpointDescriptor; MonitoredItemArguments1.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10845'; MonitoredItemArguments1.MonitoringParameters := MonitoringParameters; MonitoredItemArguments2 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments2.EndpointDescriptor.UrlString := EndpointDescriptor; MonitoredItemArguments2.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10853'; MonitoredItemArguments2.MonitoringParameters := MonitoringParameters; MonitoredItemArguments3 := CoEasyUAMonitoredItemArguments.Create; MonitoredItemArguments3.EndpointDescriptor.UrlString := EndpointDescriptor; MonitoredItemArguments3.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10855'; MonitoredItemArguments3.MonitoringParameters := MonitoringParameters; Arguments := VarArrayCreate([0, 2], varVariant); Arguments[0] := MonitoredItemArguments1; Arguments[1] := MonitoredItemArguments2; Arguments[2] := MonitoredItemArguments3; Client.SubscribeMultipleMonitoredItems(Arguments); WriteLn('Processing monitored item changed events for 10 seconds...'); PumpSleep(10*1000); WriteLn('Unsubscribing...'); Client.UnsubscribeAllMonitoredItems; WriteLn('Waiting for 5 seconds...'); Sleep(5*1000); WriteLn('Finished.'); VarClear(Arguments); FreeAndNil(Client); FreeAndNil(ClientEventHandlers); end;
// This example shows how to unsubscribe from changes of all items. class ClientEvents { function DataChangeNotification($Sender, $E) { // Display the data if ($E->Succeeded) printf("s: s\n", $E->Arguments->NodeDescriptor, $E->AttributeData); else printf("s *** Failures\n", $E->Arguments->NodeDescriptor, $E->ErrorMessageBrief); } } $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"; // 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"); $MonitoringParameters = new COM("OpcLabs.EasyOpc.UA.UAMonitoringParameters"); $MonitoringParameters->SamplingInterval = 1000; $MonitoredItemArguments1 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments1->EndpointDescriptor->UrlString = $EndpointDescriptor; $MonitoredItemArguments1->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10845"; $MonitoredItemArguments1->MonitoringParameters = $MonitoringParameters; $MonitoredItemArguments2 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments2->EndpointDescriptor->UrlString = $EndpointDescriptor; $MonitoredItemArguments2->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10853"; $MonitoredItemArguments2->MonitoringParameters = $MonitoringParameters; $MonitoredItemArguments3 = new COM("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments"); $MonitoredItemArguments3->EndpointDescriptor->UrlString = $EndpointDescriptor; $MonitoredItemArguments3->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10855"; $MonitoredItemArguments3->MonitoringParameters = $MonitoringParameters; $arguments[0] = $MonitoredItemArguments1; $arguments[1] = $MonitoredItemArguments2; $arguments[2] = $MonitoredItemArguments3; $handleArray = $Client->SubscribeMultipleMonitoredItems($arguments); for ($i = 0; $i < count($handleArray); $i++) { printf("handleArray[d]d\n", $i, $handleArray[$i]); } printf("Processing monitored item changed events for 10 seconds...\n"); $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 10); printf("Unsubscribing...\n"); $Client->UnsubscribeAllMonitoredItems; printf("Waiting for 5 seconds...\n"); $startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 5);
Rem This example shows how to unsubscribe from changes of all items. ' The client object, with events 'Public WithEvents Client7 As EasyUAClient Public Sub UnsubscribeAllMonitoredItems_Main_Command_Click() OutputText = "" Set Client7 = New EasyUAClient OutputText = OutputText & "Subscribing..." & vbCrLf Dim MonitoringParameters As New UAMonitoringParameters MonitoringParameters.SamplingInterval = 1000 Dim MonitoredItemArguments1 As New EasyUAMonitoredItemArguments MonitoredItemArguments1.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments1.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10845" Set MonitoredItemArguments1.MonitoringParameters = MonitoringParameters MonitoredItemArguments1.SetState ("Item1") Dim MonitoredItemArguments2 As New EasyUAMonitoredItemArguments MonitoredItemArguments2.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments2.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10853" Set MonitoredItemArguments2.MonitoringParameters = MonitoringParameters MonitoredItemArguments2.SetState ("Item2") Dim MonitoredItemArguments3 As New EasyUAMonitoredItemArguments MonitoredItemArguments3.endpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments3.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10855" Set MonitoredItemArguments3.MonitoringParameters = MonitoringParameters MonitoredItemArguments3.SetState ("Item3") Dim arguments(2) As Variant Set arguments(0) = MonitoredItemArguments1 Set arguments(1) = MonitoredItemArguments2 Set arguments(2) = MonitoredItemArguments3 Dim handleArray As Variant handleArray = Client7.SubscribeMultipleMonitoredItems(arguments) OutputText = OutputText & "Processing monitored item changed events for 10 seconds..." & vbCrLf Pause 10000 OutputText = OutputText & "Unsubscribing..." & vbCrLf Call Client7.UnsubscribeAllMonitoredItems OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf Pause 5000 Set Client7 = Nothing OutputText = OutputText & "Finished." & vbCrLf End Sub Public Sub Client7_DataChangeNotification(ByVal sender As Variant, ByVal eventArgs As EasyUADataChangeNotificationEventArgs) ' Display the data If eventArgs.Succeeded Then OutputText = OutputText & eventArgs.arguments.nodeDescriptor & ": " & eventArgs.AttributeData & vbCrLf Else OutputText = OutputText & eventArgs.arguments.nodeDescriptor & ": " & eventArgs.ErrorMessageBrief & vbCrLf End If End Sub
Rem This example shows how to unsubscribe from changes of all items. 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..." Dim MonitoringParameters: Set MonitoringParameters = CreateObject("OpcLabs.EasyOpc.UA.UAMonitoringParameters") MonitoringParameters.SamplingInterval = 1000 Dim MonitoredItemArguments1: Set MonitoredItemArguments1 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments1.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments1.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10845" MonitoredItemArguments1.MonitoringParameters = MonitoringParameters Dim MonitoredItemArguments2: Set MonitoredItemArguments2 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments2.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments2.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10853" MonitoredItemArguments2.MonitoringParameters = MonitoringParameters Dim MonitoredItemArguments3: Set MonitoredItemArguments3 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.EasyUAMonitoredItemArguments") MonitoredItemArguments3.EndpointDescriptor.UrlString = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" MonitoredItemArguments3.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10855" MonitoredItemArguments3.MonitoringParameters = MonitoringParameters Dim arguments(2) Set arguments(0) = MonitoredItemArguments1 Set arguments(1) = MonitoredItemArguments2 Set arguments(2) = MonitoredItemArguments3 Dim handleArray: handleArray = Client.SubscribeMultipleMonitoredItems(arguments) Dim i: For i = LBound(handleArray) To UBound(handleArray) WScript.Echo "handleArray(" & i & "): " & handleArray(i) Next WScript.Echo "Processing monitored item changed events for 10 seconds..." WScript.Sleep 10 * 1000 WScript.Echo "Unsubscribing..." Client.UnsubscribeAllMonitoredItems WScript.Echo "Waiting for 5 seconds..." WScript.Sleep 5 * 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 e.Arguments.NodeDescriptor & ":" & display End Sub
If you are no longer using the parent EasyDAClient or EasyUAClient object, you do not necessarily have to unsubscribe from the items, but it is highly recommended that you do so; if you don’t, be aware of the possible consequences described below.
In QuickOPC.NET and QuickOPC-UA, the subscriptions will otherwise be internally alive until the .NET CLR (garbage collector) decides to finalize and destroy the parent EasyDAClient or EasyUAClient object (if ever); you cannot, however, determine that moment. You can alternatively call the Dispose method of the EasyDAClient (or EasyUAClient) object’s IDisposable interface, which will unsubscribe from all items for you.
In QuickOPC-COM, the subscriptions will otherwise be internally alive. You can alternatively release all references to the EasyDAClient object, which will unsubscribe from all items for you.
Copyright © 2004-2023 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Send Documentation Feedback. Resources: Knowledge Base. Technical support: Online Forums, FAQ.