Connectivity Software User's Guide and Reference
Examples - Server OPC UA - Implement variable writing on folder level, with an event

.NET

// This example shows how to handle the write event on a folder, providing a way to implement writing of multiple data
// variables using a single handler.
// You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
// OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-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.BaseLib.NodeSpace;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.NodeSpace;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UAServerDocExamples._UAServerNode
{
    class Write
    {
        public static void Main1()
        {
            // Instantiate the server object.
            // By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            var server = new EasyUAServer();

            // Create a folder node.
            UAFolder myFolder = UAFolder.CreateIn(server.Objects, "MyFolder");

            // Create data variables in the folder. Distinguish them by their state.
            myFolder.Add(new UADataVariable("MyDataVariable1").ValueType<int>().SetState(1));
            myFolder.Add(new UADataVariable("MyDataVariable2").ValueType<int>().SetState(2));
            myFolder.Add(new UADataVariable("MyDataVariable3").ValueType<int>().SetState(3));
            myFolder.Add(new UADataVariable("MyDataVariable4").ValueType<int>().SetState(4));
            myFolder.Add(new UADataVariable("MyDataVariable5").ValueType<int>().SetState(5));

            // Handle the write event for the folder.
            myFolder.Write += MyFolderOnWrite;

            // Start the server.
            Console.WriteLine("The server is starting...");
            server.Start();

            Console.WriteLine("The server is started.");
            Console.WriteLine();

            // Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...");
            Console.ReadLine();

            // Stop the server.
            Console.WriteLine("The server is stopping...");
            server.Stop();

            Console.WriteLine("The server is stopped.");
        }

        /// <summary>
        /// Event handler for the write event on the folder. 
        /// </summary>
        /// <param name="sender">The folder object that sends the event.</param>
        /// <param name="e">Data for the variable write event.</param>
        static private void MyFolderOnWrite(object sender, UADataVariableWriteEventArgs e)
        {
            // Obtain the state associated with the data variable that is being written, and display it on the console
            // together with the new value.
            Console.WriteLine($"Data variable {e.DataVariable.State}, value written: {e.AttributeData.Value}");
        }
    }
}
# This example shows how to handle the write event on a folder, providing a way to implement writing of multiple data
# variables using a single handler.
# You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-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.

#requires -Version 5.1
using namespace OpcLabs.BaseLib.NodeSpace
using namespace OpcLabs.EasyOpc.UA
using namespace OpcLabs.EasyOpc.UA.NodeSpace
using namespace OpcLabs.EasyOpc.UA.OperationModel
using namespace OpcLabs.PowerShellManagement

# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.ServerOpcUA.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.ServerOpcUAComponents.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.PowerShellManagement.dll"

# Event handler for the write event on the folder.
[UADataVariableWriteEventHandler]$MyFolderOnWrite = {
    param ($sender, $e)
    # Obtain the state associated with the data variable that is being written, and display it on the console
    # together with the new value.
    Write-Host "Data variable $($e.DataVariable.State), value written: $($e.AttributeData.Value)"
}

# Instantiate the server object.
# By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
$server = New-Object EasyUAServer

# Create a folder node.
$myFolder = [UAFolder]::CreateIn($server.Objects, "MyFolder")

# Create data variables in the folder. Distinguish them by their state.
$myFolder.Add([NodeExtension]::SetState([UADataVariableExtension]::ValueType((New-Object UADataVariable("MyDataVariable1")), [int]), 1))
$myFolder.Add([NodeExtension]::SetState([UADataVariableExtension]::ValueType((New-Object UADataVariable("MyDataVariable2")), [int]), 2))
$myFolder.Add([NodeExtension]::SetState([UADataVariableExtension]::ValueType((New-Object UADataVariable("MyDataVariable3")), [int]), 3))
$myFolder.Add([NodeExtension]::SetState([UADataVariableExtension]::ValueType((New-Object UADataVariable("MyDataVariable4")), [int]), 4))
$myFolder.Add([NodeExtension]::SetState([UADataVariableExtension]::ValueType((New-Object UADataVariable("MyDataVariable5")), [int]), 5))

# Handle the write event for the folder.
$myFolder.add_Write([RunspacedDelegateFactory]::NewRunspacedDelegate($MyFolderOnWrite))

# Start the server.
Write-Host "The server is starting..."
$server.Start()

Write-Host "The server is started."
Write-Host

# Let the user decide when to stop.
Write-Host "Press Enter to stop the server..."
Read-Host

# Stop the server.
Write-Host "The server is stopping..."
$server.Stop()

Write-Host "The server is stopped."
' This example shows how to handle the write event on a folder, providing a way to implement writing of multiple data
' variables using a single handler.
' You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-ConnectivityStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-ConnectivityStudio-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.BaseLib.NodeSpace
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.NodeSpace
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace Global.UAServerDocExamples._UAServerNode
    Partial Friend Class Write
        Shared Sub Main1()
            ' Instantiate the server object.
            ' By default, the server will run on endpoint URL "opc.tcp://localhost:48040/".
            Dim server = New EasyUAServer()

            ' Create a folder node.
            Dim myFolder As UAFolder = UAFolder.CreateIn(server.Objects, "MyFolder")

            ' Create data variables in the folder. Distinguish them by their state.
            myFolder.Add(New UADataVariable("MyDataVariable1").ValueType(Of Integer)().SetState(1))
            myFolder.Add(New UADataVariable("MyDataVariable2").ValueType(Of Integer)().SetState(2))
            myFolder.Add(New UADataVariable("MyDataVariable3").ValueType(Of Integer)().SetState(3))
            myFolder.Add(New UADataVariable("MyDataVariable4").ValueType(Of Integer)().SetState(4))
            myFolder.Add(New UADataVariable("MyDataVariable5").ValueType(Of Integer)().SetState(5))

            ' Handle the read event for the folder.
            AddHandler myFolder.Write, AddressOf MyFolderOnWrite

            ' Start the server.
            Console.WriteLine("The server is starting...")
            server.Start()

            Console.WriteLine("The server is started.")
            Console.WriteLine()

            ' Let the user decide when to stop.
            Console.WriteLine("Press Enter to stop the server...")
            Console.ReadLine()

            ' Stop the server.
            Console.WriteLine("The server is stopping...")
            server.Stop()

            Console.WriteLine("The server is stopped.")
        End Sub

        ''' <summary>
        ''' Event handler for the write event on the folder.  
        ''' </summary>
        ''' <param name="sender">The data variable object that sends the event.</param>
        ''' <param name="e">Data for the variable write event.</param>
        Private Shared Sub MyFolderOnWrite(ByVal sender As Object, ByVal e As UADataVariableWriteEventArgs)
            ' Obtain the state associated with the data variable that is being written, and display it on the console
            ' together with the new value.
            Console.WriteLine($"Data variable {e.DataVariable.State}, value written: {e.AttributeData.Value}")
        End Sub

        Private Shared ReadOnly Random As Random = New Random()
    End Class
End Namespace
See Also

OPC Wizard

Fundamentals