Search code examples
eclipseopc-uamilo

How can I write to two OPC-UA array elements in order with Eclipse Milo?


I am running an OPC-UA server which I connect to with an Eclipse Milo client. There are two nodes that I want to be able to write to, both with the DataType Double Array[60].

In order to write over node values I have copied one of the client examples and integrated it into my code:

    public void writeNodeValue(Node node, Object input) {
        Variant v = new Variant(input);
        DataValue dv = new DataValue(v, null, null);
        NodeId nodeId = node.getNodeId();
        
        CompletableFuture<StatusCode> f =
                client.writeValue(nodeId, dv);
        
        StatusCode statusCode = null;
        try {
            statusCode = f.get();
        } catch (Throwable t) {
            logger.error("Error writing value: {}", t.getMessage(), t);
            future.completeExceptionally(t);
        }
    }

Firstly, is there an issue with this implementation, i.e. inputting an Object rather than a specific data type?

Additionally, I have two main questions regarding writing to an array node.

  1. How can I write to a specific element of one of my arrays?
  2. How can I guarantee that the client updates one array before the other?

Thanks for any advice you can provide.


Solution

  • Firstly, is there an issue with this implementation, i.e. inputting an Object rather than a specific data type?

    It's fine to use Object in the type signature, but the value does actually have to be the type the server is expecting.

    How can I write to a specific element of one of my arrays?

    Using the indexRange parameter of WriteValue and calling the OpcUaClient::write method that accepts a List of WriteValue.

    Or, if the server exposes each element as its own Node in the AddressSpace, you can try writing a scalar value directly to that Node.

    In either case it's not a guarantee the server will support or allow it.

    How can I guarantee that the client updates one array before the other?

    Make two separate write calls and wait for the first to finish before you do the second.