QuickOPC User's Guide and Reference
WriteValue(IEasyUAClient,UAEndpointDescriptor,UANodeDescriptor,Object,TypeCode) Method
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA Namespace > IEasyUAClientExtension Class > WriteValue Method : WriteValue(IEasyUAClient,UAEndpointDescriptor,UANodeDescriptor,Object,TypeCode) Method
The client object that will perform the operation.
Endpoint descriptor. Identifies the OPC-UA server.
Node descriptor. Identifies the node in OPC server's address space.
The value to be written.
Specifies the type code that should be used when writing the value.
Writes a value into an attribute an OPC server. Only the item value is written (status code and timestamps are not written). Writes value into a Value attribute of a node, using an endpoint descriptor, node Id, and a type code.
Syntax
'Declaration
 
<ExtensionAttribute()>
Public Overloads Shared Function WriteValue( _
   ByVal client As IEasyUAClient, _
   ByVal endpointDescriptor As UAEndpointDescriptor, _
   ByVal nodeDescriptor As UANodeDescriptor, _
   ByVal value As Object, _
   ByVal typeCode As TypeCode _
) As ValueTuple(Of Boolean,Boolean)
'Usage
 
Dim client As IEasyUAClient
Dim endpointDescriptor As UAEndpointDescriptor
Dim nodeDescriptor As UANodeDescriptor
Dim value As Object
Dim typeCode As TypeCode
Dim value As ValueTuple(Of Boolean,Boolean)
 
value = IEasyUAClientExtension.WriteValue(client, endpointDescriptor, nodeDescriptor, value, typeCode)

Parameters

client
The client object that will perform the operation.
endpointDescriptor
Endpoint descriptor. Identifies the OPC-UA server.
nodeDescriptor
Node descriptor. Identifies the node in OPC server's address space.
value
The value to be written.
typeCode
Specifies the type code that should be used when writing the value.
Exceptions
ExceptionDescription

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.

The OPC UA operation has failed. This operation exception in uniformly used to allow common handling of various kinds of errors. The System.Exception.InnerException always contains information about the actual error cause.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

The method will attempt to convert the value to the type specified by typeCode before writing. This may be needed because the OPC server will only accept values of proper type.

You can obtain nodeDescriptor e.g. by calling one of the browsing methods on EasyUAClientCore object.

Example

.NET

COM

// This example shows how to write a value into a single node, specifying a type code explicitly.
//
// Reasons for specifying the type explicitly might be:
// - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
// - The data type that the reports is incorrect.
// - Writing with an explicitly specified type is more efficient.
//
// TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
// a different overload of the WriteValue method.

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples._EasyUAClient
{
    partial class WriteValue
    {
        public static void TypeCode()
        {
            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.
            var client = new EasyUAClient();

            Console.WriteLine("Modifying value of a node...");
            try
            {
                client.WriteValue(
                    endpointDescriptor,
                    "nsu=http://test.org/UA/Data/ ;i=10221",
                    12345,
                    System.TypeCode.Int32);
            }
            catch (UAException uaException)
            {
                Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished.");
        }
    }
}
# This example shows how to write a value into a single node, specifying a type code explicitly.
#
# Reasons for specifying the type explicitly might be:
# - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
# - The data type that the reports is incorrect.
# - Writing with an explicitly specified type is more efficient.
#
# TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
# a different overload of the WriteValue method.

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

# 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

Write-Host "Modifying value of a node..."
try {
    [IEasyUAClientExtension]::WriteValue($client,
        $endpointDescriptor, 
        "nsu=http://test.org/UA/Data/ ;i=10221", 
        12345,
        [System.TypeCode]::Int32)
}
catch [UAException] {
    Write-Host "*** Failure: $($PSItem.Exception.GetBaseException().Message)"
    return
}

Write-Host "Finished."
# This example shows how to write a value into a single node, specifying a type code explicitly.
#
# Reasons for specifying the type explicitly might be:
# - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
# - The data type that the reports is incorrect.
# - Writing with an explicitly specified type is more efficient.
#
# TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
# a different overload of the WriteValue method.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel 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 = EasyUAClient()

print('Modifying value of a node...')
try:
    IEasyUAClientExtension.WriteValue(client,
        endpointDescriptor,
        UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10221'),
        12345,
        TypeCode.Int32)
except UAException as uaException:
    print('*** Failure: ' + uaException.GetBaseException().Message)
    exit()

print('Finished.')
' This example shows how to write a value into a single node, specifying a type code explicitly.
'
' Reasons for specifying the type explicitly might be:
' - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
' - The data type that the reports is incorrect.
' - Writing with an explicitly specified type is more efficient.
'
' TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
' a different overload of the WriteValue method.

Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace _EasyUAClient
    Partial Friend Class WriteValue
        Public Shared Sub TypeCode()

            ' 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
            Dim client = New EasyUAClient()

            ' Modify value of a node
            Try
                client.WriteValue( _
                    endpointDescriptor, _
                    "nsu=http://test.org/UA/Data/ ;i=10221", _
                    12345, _
                    System.TypeCode.Int32)
            Catch uaException As UAException
                Console.WriteLine("*** Failure: {0}", uaException.GetBaseException.Message)
                Exit Sub
            End Try
        End Sub
    End Class
End Namespace
// This example shows how to write a value into a single node, specifying a type code explicitly.
//
// Reasons for specifying the type explicitly might be:
// - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
// - The data type that the reports is incorrect.
// - Writing with an explicitly specified type is more efficient.
//
// TypeCode is easy to use, but it does not cover all possible types. It is also possible
// to specify the .NET Type, using a different overload of the WriteValue method.


class procedure WriteValue.TypeCode;
var
  Arguments: OleVariant;
  Client: OpcLabs_EasyOpcUA_TLB._EasyUAClient;
  EndpointDescriptorUrlString: string;
  WriteResult: _UAWriteResult;
  WriteValueArguments1: _UAWriteValueArguments;
  Results: OleVariant;
begin
  EndpointDescriptorUrlString :=
    //'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';

  // Prepare the arguments
  WriteValueArguments1 := CoUAWriteValueArguments.Create;
  WriteValueArguments1.EndpointDescriptor.UrlString := EndpointDescriptorUrlString;
  WriteValueArguments1.NodeDescriptor.NodeId.ExpandedText := 'nsu=http://test.org/UA/Data/ ;i=10221';
  WriteValueArguments1.Value := 12345;
  WriteValueArguments1.ValueTypeCode := TypeCode_Int32;    // here is the type explicitly specified

  Arguments := VarArrayCreate([0, 0], varVariant);
  Arguments[0] := WriteValueArguments1;

  // Instantiate the client object
  Client := CoEasyUAClient.Create;

  // Modify value of node
  TVarData(Results).VType := varArray or varVariant;
  TVarData(Results).VArray := PVarArray(Client.WriteMultipleValues(Arguments));

  WriteResult := IUnknown(Results[0]) as _UAWriteResult;
  if not WriteResult.Succeeded then
    WriteLn('*** Failure: ', WriteResult.Exception.GetBaseException.Message);

  VarClear(Results);
  VarClear(Arguments);
end;
// This example shows how to write a value into a single node, specifying a type code explicitly.
//
// Reasons for specifying the type explicitly might be:
// - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
// - The data type that the reports is incorrect.
// - Writing with an explicitly specified type is more efficient.
//
// TypeCode is easy to use, but it does not cover all possible types. It is also possible
// to specify the .NET Type, using a different overload of the WriteValue method.

const TypeCode_Int32 = 9;

$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";

// Prepare the arguments
$WriteValueArguments1 = new COM("OpcLabs.EasyOpc.UA.OperationModel.UAWriteValueArguments");
$WriteValueArguments1->EndpointDescriptor->UrlString = $EndpointDescriptor;
$WriteValueArguments1->NodeDescriptor->NodeId->ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10221";
$WriteValueArguments1->Value = 12345;
$WriteValueArguments1->ValueTypeCode = TypeCode_Int32;    // here is the type explicitly specified

$arguments[0] = $WriteValueArguments1;

// Instantiate the client object
$Client = new COM("OpcLabs.EasyOpc.UA.EasyUAClient");

// Modify value of node
$results = $Client->WriteMultipleValues($arguments);

$WriteResult = $results[0];
if (!$WriteResult->Succeeded)
    printf("*** Failure: %s\n", $WriteResult->Exception->GetBaseException()->Message);
Rem This example shows how to write a value into a single node, specifying a type code explicitly.
Rem 
Rem Reasons for specifying the type explicitly might be:
Rem - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
Rem - The data type that the reports is incorrect.
Rem - Writing with an explicitly specified type is more efficient.
Rem 
Rem TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
Rem a different overload of the WriteValue method.

Option Explicit

Const TypeCode_Int32 = 9

Dim endpointDescriptor: endpointDescriptor = _
    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
    '"http://opcua.demo-this.com:51211/UA/SampleServer"  
    '"https://opcua.demo-this.com:51212/UA/SampleServer/"

' Instantiate the client object
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.UA.EasyUAClient")

' Prepare the arguments
Dim WriteValueArguments1: Set WriteValueArguments1 = CreateObject("OpcLabs.EasyOpc.UA.OperationModel.UAWriteValueArguments")
WriteValueArguments1.EndpointDescriptor.UrlString = endpointDescriptor
WriteValueArguments1.NodeDescriptor.NodeId.ExpandedText = "nsu=http://test.org/UA/Data/ ;i=10221"
WriteValueArguments1.Value = 12345
WriteValueArguments1.ValueTypeCode = TypeCode_Int32

Dim arguments(0)
Set arguments(0) = WriteValueArguments1

' Modify value of a node
Dim results: results = Client.WriteMultipleValues(arguments)
Dim WriteResult: Set WriteResult = results(0)
If Not WriteResult.Succeeded Then
    WScript.Echo "*** Failure: " & WriteResult.Exception.GetBaseException().Message
End If
Rem This example shows how to write a value into a single node, specifying a type code explicitly.
Rem
Rem Reasons for specifying the type explicitly might be:
Rem - The data type in the server has subtypes, and the client therefore needs to pick the subtype to be written.
Rem - The data type that the reports is incorrect.
Rem - Writing with an explicitly specified type is more efficient.
Rem
Rem TypeCode is easy to use, but it does not cover all possible types. It is also possible to specify the .NET Type, using
Rem a different overload of the WriteValue method.

Public Sub WriteValue_TypeCode_Command_Click()
    OutputText = ""
    
    Const TypeCode_Int32 = 9

    Dim endpointDescriptor As String
    'endpointDescriptor = "http://opcua.demo-this.com:51211/UA/SampleServer"
    'endpointDescriptor = "https://opcua.demo-this.com:51212/UA/SampleServer/"
    endpointDescriptor = "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"

    ' Instantiate the client object
    Dim Client As New EasyUAClient

    ' Prepare the arguments
    Dim WriteValueArguments1 As New UAWriteValueArguments
    WriteValueArguments1.endpointDescriptor.UrlString = endpointDescriptor
    WriteValueArguments1.nodeDescriptor.NodeId.expandedText = "nsu=http://test.org/UA/Data/ ;i=10221"
    WriteValueArguments1.SetValue 12345
    WriteValueArguments1.ValueTypeCode = TypeCode_Int32

    Dim arguments(0) As Variant
    Set arguments(0) = WriteValueArguments1

    ' Modify value of node
    Dim results As Variant
    results = Client.WriteMultipleValues(arguments)

    ' Display results
    Dim Result As UAWriteResult: Set Result = results(0)
    If Not Result.Succeeded Then
        OutputText = OutputText & "*** Failure: " & Result.Exception.GetBaseException().Message & vbCrLf
    End If
End Sub
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