Skip to main content

Read / Write

Prerequisite Reading

read​

public OpcUaDataValue read(String nodeId);
public OpcUaDataValue read(String nodeId, Enums.AttributeId attribute);

Read the specified Attribute from a single node.

ParametersTypeDefaultNote
nodeIdString—NodeId String (i=2258 / ns=2; s=...)
attributeEnums.AttributeIdValue (13)Properties to Read

Returns: OpcUaDataValue (Implementation AutoCloseable, Must close)

Exception: OpcUaException (BadNodeIdUnknown / BadNotReadable / BadCommunicationError / ...)

// Read Value
try (OpcUaDataValue dv = ua.read("ns=2;s=Temperature")) {
System.out.println(dv.variant().asDouble());
}

// Read DisplayName
try (OpcUaDataValue dn = ua.read("ns=2;s=Temperature", Enums.AttributeId.DisplayName)) {
System.out.println(dn.variant().asString());
}

readMany​

Read the same attribute from multiple nodes in a single RPC batch.

public List<OpcUaDataValue> readMany(List<String> nodeIds, Enums.AttributeId attribute);
List<String> nodes = Arrays.asList("ns=2;s=T1", "ns=2;s=T2", "ns=2;s=T3");
List<OpcUaDataValue> values = ua.readMany(nodes, Enums.AttributeId.Value);
try {
for (int i = 0; i < nodes.size(); i++) {
OpcUaDataValue dv = values.get(i);
if (dv != null) {
System.out.println(nodes.get(i) + " = " + dv.variant());
}
}
} finally {
for (OpcUaDataValue dv : values) if (dv != null) dv.close();
}

Performance: Batch RPC reads N nodes at once, saving N-1 RPC round trips compared to reading them one by one in a loop; the absolute time taken varies depending on the network and server, so test it in your own environment.

You must close

readMany each item in the returned list one by one; each item OpcUaDataValue holds a native handle. try-finally: Close each one individually, otherwise there will be a leak.

write​

public Enums.StatusCode write(String nodeId, OpcUaVariant value);
public Enums.StatusCode write(String nodeId, OpcUaVariant value, Enums.AttributeId attribute);

Order-writing node.

try (OpcUaVariant v = new OpcUaVariant().setDouble(42.5)) {
Enums.StatusCode st = ua.write("ns=2;s=Setpoint", v);
if (st != Enums.StatusCode.Good) {
System.err.println("Write failed: " + st);
}
}

Returns Enums.StatusCode (Good / BadNotWritable / BadTypeMismatch / BadUserAccessDenied / ...).

Business-level failures do not throw exceptions

write Do not throw exceptions (Unless it is a transport-level error), business-level failures are determined by the return value. You must check the return value; do not assume it is "Good."

OpcUaVariant Quick Reference for Structures​

new OpcUaVariant().setBoolean(true)          // Boolean
new OpcUaVariant().setSByte((byte)-1) // SByte
new OpcUaVariant().setByte(255) // Byte
new OpcUaVariant().setInt16((short)42) // Int16
new OpcUaVariant().setUInt16(42) // UInt16
new OpcUaVariant().setInt32(42) // Int32
new OpcUaVariant().setUInt32(42) // UInt32
new OpcUaVariant().setInt64(42L) // Int64
new OpcUaVariant().setFloat(3.14f) // Float
new OpcUaVariant().setDouble(3.14) // Double
new OpcUaVariant().setString("hello") // String
new OpcUaVariant().setDateTime(fileTimeLong) // DateTime (Windows FileTime)

For more information, see OpcUaVariant Data Types.

OpcUaDataValue Field Quick Reference​

MethodsReturnNote
variant()OpcUaVariantActual value (not held; do not close)
getStatusEnum()Enums.StatusCodeGood / Bad* / Uncertain*
isGood()booleanEquivalent to getStatusEnum().isGood()
sourceTimestamp()longData source FileTime, 0 = null
serverTimestamp()longServer FileTime, 0 = null
try (OpcUaDataValue dv = ua.read("ns=2;s=T1")) {
if (!dv.isGood()) {
System.err.println("Bad value: " + dv.getStatusEnum());
return;
}
Double v = dv.variant().asDouble();
long ft = dv.sourceTimestamp();
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();
System.out.println(v + " @ " + t);
}

AttributeId Common Values​

IdNameNote
1NodeIdNode ID
2NodeClassNode Categories
3BrowseNameBrowse Name
4DisplayNameDisplay Name
5DescriptionDescription
13ValueVariable Value (Default)
14DataTypeData Types NodeId
17AccessLevelAccess permission bits
20HistorizingIs history being recorded?
21ExecutableIs the Method Executable?

Best Practices​

  • Lot Priority: Monitor 100+ tags readMany; do not loop read (save N-1 iterations RPC)
  • Must close: OpcUaDataValue Implement the AutoCloseable interface; failing to close it will cause native memory leaks.
  • write Check the return value: Don't assume it's necessarily good
  • Use subscriptions for real-time changes: Do not poll read with 1-second polling; instead, use Subscription onDataChanged (Server-side push)
  • OpcUaVariant You have to close it, too: Self-constructed new OpcUaVariant(), wrapped try-with-resources in

Next Step​