// This example shows how to unsubscribe from changes of just some monitored
// items.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.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.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.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in PowerShell on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PowerShell .
# Missing some example? Ask us for it on our Online Forums, https://www.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.
#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.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.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.
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel
Namespace _EasyUAClient
Partial Friend Class UnsubscribeMultipleMonitoredItems
Public Shared Sub Some()
' 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_DataChangeNotification2
Console.WriteLine()
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)
}
)
For i As Integer = 0 To handleArray.Length - 1
Console.WriteLine($"handleArray[{i}]: {handleArray(i)}")
Next 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 Integer() {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.")
End Sub
Private Shared Sub client_DataChangeNotification2(ByVal sender As Object, ByVal e As EasyUADataChangeNotificationEventArgs)
' Display value
If e.Succeeded Then
Console.WriteLine($"{e.Arguments.NodeDescriptor}: {e.AttributeData.Value}")
Else
Console.WriteLine($"{e.Arguments.NodeDescriptor} *** Failure: {e.ErrorMessageBrief}")
End If
End Sub
End Class
End Namespace
REM This example shows how to unsubscribe from changes of just some monitored
REM items.
REM
REM Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
REM OPC client and subscriber examples in Visual Basic on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VB .
REM Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
REM a commercial license in order to use Online Forums, and we reply to every post.
' 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
# This example shows how to unsubscribe from changes of just some monitored
# items.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.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.
# 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.')