// This example shows how to register and unregister multiple nodes in an OPC UA server, and use this approach together with
// connection locking.
//
// Node registration (with OPC UA servers that support it) can improve performance with repeatedly accessed nodes.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
using System;
using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
using OpcLabs.EasyOpc.UA.Services;
using OpcLabs.EasyOpc.UA.Services.Extensions;
namespace UADocExamples._EasyUAClientNodeRegistration
{
class RegisterAndUnregisterMultipleNodes
{
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.
using (var client = new EasyUAClient())
{
// Obtain the client node registration service.
IEasyUAClientNodeRegistration clientNodeRegistration = client.GetRequiredService<IEasyUAClientNodeRegistration>();
// Obtain the client connection control service.
IEasyUAClientConnectionControl clientConnectionControl = client.GetRequiredService<IEasyUAClientConnectionControl>();
var nodeDescriptorArray = new UANodeDescriptor[]
{
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[0]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[4]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[8]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[12]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[16]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[20]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[24]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[28]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[32]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[36]"
};
Console.WriteLine("Registering nodes");
int[] registrationHandleArray = clientNodeRegistration.RegisterMultipleNodes(endpointDescriptor, nodeDescriptorArray);
Console.WriteLine("Locking the connection");
// Locking the connection will attempt to open it, and when successful, the nodes will be registered with
// the server at that time. The use of locking is not necessary, but it may bring benefits together with the
// node registration. See the conceptual documentation for more information.
int lockHandle = clientConnectionControl.LockConnection(endpointDescriptor);
Console.WriteLine("Waiting for 10 seconds...");
// The example uses this delay to demonstrate the fact that your code might have other tasks to do, before
// it accesses the previously registered nodes.
System.Threading.Thread.Sleep(10 * 1000);
Console.WriteLine("Reading (1)");
UAAttributeDataResult[] resultArray1 = client.ReadMultiple(endpointDescriptor, nodeDescriptorArray);
foreach (UAAttributeDataResult result in resultArray1)
Console.WriteLine(result);
Console.WriteLine("Reading (2)");
UAAttributeDataResult[] resultArray2 = client.ReadMultiple(endpointDescriptor, nodeDescriptorArray);
foreach (UAAttributeDataResult result in resultArray2)
Console.WriteLine(result);
Console.WriteLine("Unlocking the connection");
clientConnectionControl.UnlockConnection(lockHandle);
Console.WriteLine("Unregistering nodes");
clientNodeRegistration.UnregisterMultipleNodes(registrationHandleArray);
Console.WriteLine("Finished.");
}
}
}
}
# This example shows how to register and unregister multiple nodes in an OPC UA server, and use this approach together
# with connection locking.
#
# Node registration (with OPC UA servers that support it) can improve performance with repeatedly accessed nodes.
#
# 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 Microsoft.Extensions.DependencyInjection import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel import *
from OpcLabs.EasyOpc.UA.Services import *
from OpcLabs.EasyOpc.UA.Services.Extensions import *
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.
client = None
try:
client = EasyUAClient()
# Obtain the client connection monitoring service.
clientNodeRegistration = ServiceProviderServiceExtensions.GetRequiredService[IEasyUAClientNodeRegistration](client)
if clientNodeRegistration is None:
print('The client connection monitoring service is not available.')
exit()
# Obtain the client connection control service.
clientConnectionControl = ServiceProviderServiceExtensions.GetRequiredService[IEasyUAClientConnectionControl](client)
if clientConnectionControl is None:
print('The client connection control service is not available.')
exit()
nodeDescriptorArray = [
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[0]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[4]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[8]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[12]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[16]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[20]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[24]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[28]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[32]'),
UANodeDescriptor('nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[36]'),
]
print('Registering nodes')
registrationHandleArray = IEasyUAClientNodeRegistrationExtension.RegisterMultipleNodes(clientNodeRegistration,
endpointDescriptor,
nodeDescriptorArray)
print('Locking the connection')
# Locking the connection will attempt to open it, and when successful, the nodes will be registered with
# the server at that time. The use of locking is not necessary, but it may bring benefits together with the
# node registration. See the conceptual documentation for more information.
lockHandle = clientConnectionControl.LockConnection(endpointDescriptor)
print('Waiting for 10 seconds...')
# The example uses this delay to demonstrate the fact that your code might have other tasks to do, before
# it accesses the previously registered nodes.
time.sleep(10)
print('Reading (1)')
attributeDataResultArray1 = IEasyUAClientExtension.ReadMultiple(client, endpointDescriptor, nodeDescriptorArray)
for attributeDataResult in attributeDataResultArray1:
print(attributeDataResult)
print('Reading (2)')
attributeDataResultArray2 = IEasyUAClientExtension.ReadMultiple(client, endpointDescriptor, nodeDescriptorArray)
for attributeDataResult in attributeDataResultArray2:
print(attributeDataResult)
print('Unlocking the connection')
clientConnectionControl.UnlockConnection(lockHandle)
print('Unregistering nodes')
clientNodeRegistration.UnregisterMultipleNodes(registrationHandleArray)
print('Finished.')
finally:
client and client.Dispose()
' This example shows how to register and unregister multiple nodes in an OPC UA server, and use this approach together with
' connection locking.
'
' Node registration (with OPC UA servers that support it) can improve performance with repeatedly accessed nodes.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
Imports Microsoft.Extensions.DependencyInjection
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel
Imports OpcLabs.EasyOpc.UA.Services
Imports OpcLabs.EasyOpc.UA.Services.Extensions
Namespace _EasyUAClientNodeRegistration
Friend Class RegisterAndUnregisterMultipleNodes
Public Shared Sub Main1()
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.
Using client = New EasyUAClient()
' Obtain the client node registration service.
Dim clientNodeRegistration As IEasyUAClientNodeRegistration = client.GetRequiredService(Of IEasyUAClientNodeRegistration)
' Obtain the client connection control service.
Dim clientConnectionControl As IEasyUAClientConnectionControl = client.GetRequiredService(Of IEasyUAClientConnectionControl)
Dim nodeDescriptorArray = New UANodeDescriptor() {
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[0]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[4]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[8]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[12]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[16]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[20]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[24]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[28]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[32]",
"nsu=http://samples.org/UA/memorybuffer/Instance ;ns=8;s=UInt32[36]"
}
Console.WriteLine("Registering nodes")
Dim registrationHandleArray() As Integer = clientNodeRegistration.RegisterMultipleNodes(endpointDescriptor, nodeDescriptorArray)
Console.WriteLine("Locking the connection")
' Locking the connection will attempt to open it, and when successful, the nodes will be registered with
' the server at that time. The use of locking is not necessary, but it may bring benefits together with the
' node registration. See the conceptual documentation for more information.
Dim lockHandle As Integer = clientConnectionControl.LockConnection(endpointDescriptor)
Console.WriteLine("Waiting for 10 seconds...")
' The example uses this delay to demonstrate the fact that your code might have other tasks to do, before
' it accesses the previously registered nodes.
System.Threading.Thread.Sleep(10 * 1000)
Console.WriteLine("Reading (1)")
Dim resultArray1() As UAAttributeDataResult = client.ReadMultiple(endpointDescriptor, nodeDescriptorArray)
For Each result As UAAttributeDataResult In resultArray1
Console.WriteLine(result)
Next result
Console.WriteLine("Reading (2)")
Dim resultArray2() As UAAttributeDataResult = client.ReadMultiple(endpointDescriptor, nodeDescriptorArray)
For Each result As UAAttributeDataResult In resultArray2
Console.WriteLine(result)
Next result
Console.WriteLine("Unlocking the connection")
clientConnectionControl.UnlockConnection(lockHandle)
Console.WriteLine("Unregistering nodes")
clientNodeRegistration.UnregisterMultipleNodes(registrationHandleArray)
Console.WriteLine("Finished.")
End Using
End Sub
End Class
End Namespace