PicoOPC User's Guide and Reference
Writing a Value

Values can be written to one or more attributes of one or more OPC UA nodes using the Write method of the Client object.

This method takes an input argument that is an array of WriteValue elements. Each WriteValue contains primarily the NodeId of the node you want to write into (see Node Identification for more details) and the data value you want to write (in the DataValue property; see Variants and Data Values for more information). In addition, it has the ID of the attribute you want to write (in the AttributeId property, defaults to the Value attribute), and an index range (in the IndexRange property, for addressing array elements). You can set these parameters differently from the defaults when creating the NodeId, if you need to. Consult the OPC UA specifications for the syntax of semantics of the index range.

The method returns an array of status code, indicating success or failure of the individual write operations. The positions (indexes) of elements in the result array correspond to those in the input array of WriteValue-s. See Operation Model for more information about the status codes.

// This example shows how to write a value into a single attribute of a node.

using System;
using OpcLabs.PicoOpc.UA;

namespace ConsoleApp._Client
{
    partial class Write
    {
        public static void Main1()
        {
            // Instantiate the client object.
            var client = new Client();

            try
            {
                try
                {
                    // Connect to the server.
                    Console.WriteLine();
                    Console.WriteLine("Connecting...");
                    client.Connect(new Uri("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"));

                    // Write a value.
                    Console.WriteLine();
                    Console.WriteLine("Writing...");
                    var nodeId = new NodeId(10221, namespaceIndex:2);
                    UInt32 statusCode = client.Write(new[] {new WriteValue(nodeId,
                        new DataValue(new Variant(BuiltInType.Int32, 123)))})[0];

                    // Display the result.
                    Console.WriteLine($"Status code: 0x{statusCode:X8}");
                }
                finally
                {
                    if (client.IsConnected)
                    {
                        // Disconnect from the server.
                        Console.WriteLine();
                        Console.WriteLine("Disconnecting...");
                        client.Disconnect();
                    }
                }
            }
            catch (AggregateException aggregateException)
            {
                Console.WriteLine("*** Failure: {0}", aggregateException.GetBaseException().Message);
            }
        }
    }
}

If the actual value is an array, writing is no different. You just need to prepare the array with its elements and pass it to the method, as in the following example.  

// This example shows how to write an array into an attribute.

using System;
using OpcLabs.PicoOpc.UA;

namespace ConsoleApp._Client
{
    partial class Write
    {
        public static void Array()
        {
            // Instantiate the client object.
            var client = new Client();

            try
            {
                try
                {
                    // Connect to the server.
                    Console.WriteLine();
                    Console.WriteLine("Connecting...");
                    client.Connect(new Uri("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"));

                    // Write a value.
                    Console.WriteLine();
                    Console.WriteLine("Writing...");
                    var nodeId = new NodeId(10305, namespaceIndex: 2);
                    var value = new object[] {101, 102, 103, 104, 105};
                    UInt32 statusCode = client.Write(new[] {new WriteValue(nodeId,
                        new DataValue(new Variant(BuiltInType.Int32, value, isArray:true)))})[0];

                    // Display the result.
                    Console.WriteLine($"Status code: 0x{statusCode:X8}");
                }
                finally
                {
                    if (client.IsConnected)
                    {
                        // Disconnect from the server.
                        Console.WriteLine();
                        Console.WriteLine("Disconnecting...");
                        client.Disconnect();
                    }
                }
            }
            catch (AggregateException aggregateException)
            {
                Console.WriteLine("*** Failure: {0}", aggregateException.GetBaseException().Message);
            }
        }
    }
}

The above examples were a bit simplified due to the fact that we have only written one value in the Write method call. The following example show the full power of the method, with multiple value being written at once.   

// This example shows how to write into multiple attributes or nodes at once.

using System;
using OpcLabs.PicoOpc.UA;

namespace ConsoleApp._Client
{
    partial class Write
    {
        public static void Multiple()
        {
            // Instantiate the client object.
            var client = new Client();

            try
            {
                try
                {
                    // Connect to the server.
                    Console.WriteLine();
                    Console.WriteLine("Connecting...");
                    client.Connect(new Uri("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"));

                    // Write values.
                    Console.WriteLine();
                    Console.WriteLine("Writing...");
                    UInt32[] statusCodeArray = client.Write(new[]
                    {
                        new WriteValue(new NodeId(10221, namespaceIndex:2), new DataValue(new Variant(BuiltInType.Int32, 23456))),
                        new WriteValue(new NodeId(10226, namespaceIndex:2), new DataValue(new Variant(BuiltInType.Double, 2.34567890))),
                        new WriteValue(new NodeId(10227, namespaceIndex:2), new DataValue(new Variant(BuiltInType.String, "ABC")))
                    });

                    // Display the results.
                    foreach (UInt32 statusCode in statusCodeArray)
                        Console.WriteLine($"Status code: 0x{statusCode:X8}");
                }
                finally
                {
                    if (client.IsConnected)
                    {
                        // Disconnect from the server.
                        Console.WriteLine();
                        Console.WriteLine("Disconnecting...");
                        client.Disconnect();
                    }
                }
            }
            catch (AggregateException aggregateException)
            {
                Console.WriteLine("*** Failure: {0}", aggregateException.GetBaseException().Message);
            }
        }
    }
}
See Also