// 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 .
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 Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# 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.')
' 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 .
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