Subscribe to a data change. Specify an endpoint descriptor, node id, and sampling interval.
Syntax
Parameters
- client
- The client object that will perform the operation.
This is typically the EasyUAClient object.
The value of this parameter cannot be null
(Nothing
in Visual Basic).
- endpointDescriptor
- Endpoint descriptor. Identifies the OPC-UA server.
Because the UAEndpointDescriptor has implicit conversions from System.String and System.Uri, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a string (representing the endpoint URL, or a so-called OPC UA endpoint descriptor string), or a System.Uri object, in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can use the UAEndpointDescriptor.FromString or UAEndpointDescriptor.FromUri static method instead.
Also, because the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement and OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement have an implicit conversion to UAEndpointDescriptor, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement (results from OPC UA discovery) in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can convert a (non-null) OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement to UAEndpointDescriptor using the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement.ToUAEndpointDescriptor or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement.ToUAEndpointDescriptor method instead.
If you are using OPC Wizard (for server development), an implicit conversion from EasyUAServerCore can be used in the same way to simply pass the server object in place of this parameter, which will use its OpcLabs.EasyOpc.UA.EasyUAServerCore.EffectiveServerDescriptor property for the connection.
The value of this parameter cannot be null
(Nothing
in Visual Basic).
- nodeDescriptor
- Node descriptor. Identifies the node in OPC server's address space.
Because the UANodeDescriptor has implicit conversions from OpcLabs.EasyOpc.UA.AddressSpace.UANodeId, OpcLabs.EasyOpc.UA.AddressSpace.UANodeElement, OpcLabs.EasyOpc.UA.Navigation.UABrowsePath and System.String, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a OpcLabs.EasyOpc.UA.AddressSpace.UANodeId object (representing the Id of the OPC UA node), a node element object (from OPC UA browsing), OpcLabs.EasyOpc.UA.Navigation.UABrowsePath object (representing OPC UA absolute browse path), or a string (with expanded node Id text) in place of this parameter, and the corresponding node descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can use the UANodeDescriptor.FromString, UANodeDescriptor.FromUABrowsePath, UANodeDescriptor.FromUANodeElement or UANodeDescriptor.FromUANodeId static method instead.
If you are using OPC Wizard (for server development), an implicit conversion from OpcLabs.EasyOpc.UA.NodeSpace.UAServerNode can be used in the same way to simply pass the server node in place of this parameter, which will use its OpcLabs.EasyOpc.UA.NodeSpace.UAServerNode.EffectiveNodeDescriptor property for the operation.
The value of this parameter cannot be null
(Nothing
in Visual Basic).
- samplingInterval
- The sampling interval (in milliseconds) indicates the fastest rate at which the Server should sample its underlying source for data changes.
Valid values of this parameter are in the range from -1
to 2147483647 (Int32.MaxValue)
.
Return Value
The method returns an integer handle that uniquely identifies the monitored item subscription.
Exceptions
Exception | Description |
System.ArgumentNullException |
A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.
This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception. |
System.ArgumentOutOfRangeException |
The value of an argument is outside the allowable range of values as defined by the invoked method.
This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception. |
Example
// This example shows how to subscribe to changes of a single monitored item and display the value of the item with each
// change.
//
// 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 SubscribeDataChange
{
public static void Overload1()
{
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.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000);
Console.WriteLine("Processing data change events for 20 seconds...");
System.Threading.Thread.Sleep(20 * 1000);
Console.WriteLine("Unsubscribing...");
client.UnsubscribeAllMonitoredItems();
Console.WriteLine("Waiting for 5 seconds...");
System.Threading.Thread.Sleep(5 * 1000);
Console.WriteLine("Finished.");
}
static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
{
// Display value.
if (e.Succeeded)
Console.WriteLine($"Value: {e.AttributeData.Value}");
else
Console.WriteLine($"*** Failure: {e.ErrorMessageBrief}");
}
}
}
# This example shows how to subscribe to changes of a single monitored item and display the value of the item with each
# change.
#
# 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
# 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 "Value: $($EventArgs.AttributeData.Value)"
}
else {
Write-Host "*** Failure: $($EventArgs.ErrorMessageBrief)"
}
}
Write-Host "Subscribing..."
$client.SubscribeDataChange($endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000)
Write-Host "Processing data change events for 20 seconds..."
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($stopwatch.Elapsed.TotalSeconds -lt 20) {
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 subscribe to changes of a single monitored item and display the value of the item with each change.
'
' 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
Friend Class SubscribeDataChange
Public Shared Sub Overload1()
' 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.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.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("Value: {0}", e.AttributeData.Value)
Else
Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief)
End If
End Sub
End Class
End Namespace
# This example shows how to subscribe to changes of a single monitored item and display the value of the item with each
# change.
#
# 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 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...')
IEasyUAClientExtension.SubscribeDataChange(client,
endpointDescriptor,
UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'),
1000)
print('Processing data change events for 20 seconds...')
time.sleep(20)
print('Unsubscribing...')
client.UnsubscribeAllMonitoredItems()
print('Waiting for 5 seconds...')
time.sleep(5)
print('Finished.')
// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
//
// 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.ComplexData._EasyUAClient
{
class SubscribeDataChange
{
public static void Main1()
{
// Define which server and node we will work with.
UAEndpointDescriptor endpointDescriptor =
"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
// or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
// or "https://opcua.demo-this.com:51212/UA/SampleServer/"
UANodeDescriptor nodeDescriptor =
"nsu=http://test.org/UA/Data/ ;i=10867"; // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue
// Instantiate the client object and hook the event handler.
var client = new EasyUAClient();
client.DataChangeNotification += client_DataChangeNotification;
// Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
// the data returned is different.
Console.WriteLine("Subscribing...");
client.SubscribeDataChange(endpointDescriptor, nodeDescriptor, samplingInterval:1000);
Console.WriteLine("Processing data change events for 20 seconds...");
System.Threading.Thread.Sleep(20 * 1000);
Console.WriteLine("Unsubscribing...");
client.UnsubscribeAllMonitoredItems();
Console.WriteLine("Waiting for 5 seconds...");
System.Threading.Thread.Sleep(5 * 1000);
// Unhook the event handler.
client.DataChangeNotification -= client_DataChangeNotification;
// Example output:
//
//Subscribing...
//Processing data change events for 20 seconds...
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//Unsubscribing...
//Waiting for 5 seconds...
}
static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
{
// Display value
if (e.Succeeded)
Console.WriteLine("Value: {0}", e.AttributeData.Value);
else
Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);
// For processing the internals of the data, refer to examples for GenericData and DataType classes.
}
}
}
' Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
'
' 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 System
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel
Namespace ComplexData._EasyUAClient
Friend Class SubscribeDataChange
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/"
' Define which node we will work with.
Dim nodeDescriptor As UANodeDescriptor = _
"nsu=http://test.org/UA/Data/ ;i=10867" ' [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue
' Instantiate the client object and hook the event handler.
Dim client = New EasyUAClient
AddHandler client.DataChangeNotification, AddressOf client_DataChangeNotification
' Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
' the data returned is different.
Console.WriteLine("Subscribing...")
client.SubscribeDataChange(endpointDescriptor, nodeDescriptor, samplingInterval:=1000)
Console.WriteLine("Processing data change events for 20 seconds...")
Threading.Thread.Sleep((20 * 1000))
Console.WriteLine("Unsubscribing...")
client.UnsubscribeAllMonitoredItems()
Console.WriteLine("Waiting for 5 seconds...")
Threading.Thread.Sleep((5 * 1000))
' Unhook the event handler.
RemoveHandler client.DataChangeNotification, AddressOf client_DataChangeNotification
' Example output:
'
'Subscribing...
'Processing data change events for 20 seconds...
'(ScalarValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ScalarValueDataType) structured
'(ScalarValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ScalarValueDataType) structured
'(ArrayValueDataType) structured
'(ArrayValueDataType) structured
'(ScalarValueDataType) structured
'(ScalarValueDataType) structured
'(ScalarValueDataType) structured
'Unsubscribing...
'Waiting for 5 seconds...
End Sub
Private Shared Sub client_DataChangeNotification(sender As Object, 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
' For processing the internals of the data, refer to examples for GenericData and DataType classes.
End Sub
End Class
End Namespace
// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// 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.
type
TClientEventHandlers33 = class
procedure OnDataChangeNotification(
ASender: TObject;
sender: OleVariant;
const eventArgs: _EasyUADataChangeNotificationEventArgs);
end;
procedure TClientEventHandlers33.OnDataChangeNotification(
ASender: TObject;
sender: OleVariant;
const eventArgs: _EasyUADataChangeNotificationEventArgs);
begin
// Display value
if eventArgs.Succeeded then
WriteLn('Value: ', eventArgs.AttributeData.Value.ToString)
else
WriteLn('*** Failure: ', eventArgs.ErrorMessageBrief);
// For processing the internals of the data, refer to examples for GenericData and DataType classes.
end;
class procedure SubscribeDataChange.Main;
var
Client: TEasyUAClient;
ClientEventHandlers: TClientEventHandlers33;
EndpointDescriptor: string;
NodeDescriptor: string;
begin
// Define which server and node we will work with.
EndpointDescriptor :=
//'http://opcua.demo-this.com:51211/UA/SampleServer';
//'https://opcua.demo-this.com:51212/UA/SampleServer/';
'opc.tcp://opcua.demo-this.com:51210/UA/SampleServer';
NodeDescriptor := 'nsu=http://test.org/UA/Data/ ;i=10867'; // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue
// Instantiate the client object and hook events
Client := TEasyUAClient.Create(nil);
ClientEventHandlers := TClientEventHandlers33.Create;
Client.OnDataChangeNotification := ClientEventHandlers.OnDataChangeNotification;
// Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
// the data returned is different.
WriteLn('Subscribing...');
Client.SubscribeDataChange(EndpointDescriptor, NodeDescriptor, 1000);
WriteLn('Processing data change events for 20 seconds...');
PumpSleep(20*1000);
WriteLn('Unsubscribing...');
Client.UnsubscribeAllMonitoredItems;
WriteLn('Waiting for 5 seconds...');
PumpSleep(5*1000);
WriteLn('Finished.');
FreeAndNil(Client);
FreeAndNil(ClientEventHandlers);
// Example output:
//
//Subscribing...
//Processing data change events for 20 seconds...
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ArrayValueDataType) structured
//(ArrayValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//(ScalarValueDataType) structured
//Unsubscribing...
//Waiting for 5 seconds...
end;
// Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in PHP on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PHP .
// 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.
class ClientEvents {
function DataChangeNotification($Sender, $E)
{
// Display value
if ($E->Succeeded)
printf("Value: %s\n", $E->AttributeData->Value);
else
printf("*** Failure : %s\n", $E->ErrorMessageBrief);
// For processing the internals of the data, refer to examples for GenericData and DataType classes.
}
}
// Define which server and node we will work with.
$EndpointDescriptor =
//"http://opcua.demo-this.com:51211/UA/SampleServer";
//"https://opcua.demo-this.com:51212/UA/SampleServer/";
"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
$NodeDescriptor = "nsu=http://test.org/UA/Data/ ;i=10867"; // [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue
// Instantiate the client object and hook events
$Client = new COM("OpcLabs.EasyOpc.UA.EasyUAClient");
$ClientEvents = new ClientEvents();
com_event_sink($Client, $ClientEvents, "DEasyUAClientEvents");
// Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
// the data returned is different.
printf("Subscribing...\n");
$Client->SubscribeDataChange($EndpointDescriptor, $NodeDescriptor, 1000);
printf("Processing data change events for 20 seconds...\n");
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 20);
printf("Unsubscribing...\n");
$Client->UnsubscribeAllMonitoredItems;
printf("Waiting for 5 seconds...\n");
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 5);
# Shows how to subscribe to complex data with OPC UA Complex Data plug-in.
#
# 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 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='')
#
# For processing the internals of the data, refer to examples for GenericData and DataType classes.
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/'
# [ObjectsFolder]/Data.Dynamic.Scalar.StructureValue
nodeDescriptor = UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10867')
# Instantiate the client object and hook events.
client = EasyUAClient()
client.DataChangeNotification += dataChangeNotification
# Subscribe to a node which returns complex data. This is done in the same way as regular subscribes - just
# the data returned is different.
print('Subscribing...')
IEasyUAClientExtension.SubscribeDataChange(client,
endpointDescriptor,
nodeDescriptor,
1000) # samplingInterval
print('Processing data change events for 30 seconds...')
time.sleep(30)
print('Unsubscribing...')
client.UnsubscribeAllMonitoredItems()
print('Waiting for 5 seconds...')
time.sleep(5)
# Unhook the event handler.
client.DataChangeNotification -= dataChangeNotification
print('Finished.')
Requirements
Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows
See Also