Read / Write
- Data Types OpcUaVariant and OpcUaDataValue It is the foundation of all reading and writing.
- For frequent access to the same node, please use registerNodes Accelerate.
- For real-time change notifications Subscription Rather than a loop that reads.
read
public OpcUaDataValue read(String nodeId);
public OpcUaDataValue read(String nodeId, Enums.AttributeId attribute);
Read the specified Attribute from a single node.
| Parameters | Type | Default | Note |
|---|---|---|---|
| nodeId | String | — | NodeId String (i=2258 / ns=2; s=...) |
| attribute | Enums.AttributeId | Value (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.
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 / ...).
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
| Methods | Return | Note |
|---|---|---|
| variant() | OpcUaVariant | Actual value (not held; do not close) |
| getStatusEnum() | Enums.StatusCode | Good / Bad* / Uncertain* |
| isGood() | boolean | Equivalent to getStatusEnum().isGood() |
| sourceTimestamp() | long | Data source FileTime, 0 = null |
| serverTimestamp() | long | Server 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
| Id | Name | Note |
|---|---|---|
| 1 | NodeId | Node ID |
| 2 | NodeClass | Node Categories |
| 3 | BrowseName | Browse Name |
| 4 | DisplayName | Display Name |
| 5 | Description | Description |
| 13 | Value | Variable Value (Default) |
| 14 | DataType | Data Types NodeId |
| 17 | AccessLevel | Access permission bits |
| 20 | Historizing | Is history being recorded? |
| 21 | Executable | Is the Method Executable? |
Best Practices
- Lot Priority: Monitor 100+ tags
readMany; do not loopread(save N-1 iterations RPC) - Must close:
OpcUaDataValueImplement 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
readwith 1-second polling; instead, useSubscriptiononDataChanged (Server-side push) - OpcUaVariant You have to close it, too: Self-constructed
new OpcUaVariant(), wrappedtry-with-resourcesin