Skip to main content

Read / Write

Prerequisite Reading
  • Data Types Variant and DataValue It is the foundation of all reading and writing.
  • For frequent access to the same node, please use register_nodes Accelerate.
  • For real-time change notifications Subscription Rather than a loop that reads.

read​

pub fn read(&self, node_id: &str) -> Result<DataValue, OpcUaError>;

Read the properties Value of a single node.

ParametersTypeNote
node_id&strNodeId String (i=2258 / ns=2; s=...)

Returns DataValue (Implementation Drop: native is automatically released when it goes out of scope.)

Error: OpcUaError Carry BAD_NODE_ID_UNKNOWN / BAD_NOT_READABLE / BAD_COMMUNICATION_ERROR / ...

let dv = s.read("ns=2;s=Temperature")?;
println!("Temperature = {} °C", dv.variant().try_get_f64().unwrap_or(0.0));

read_attr​

pub fn read_attr(&self, node_id: &str, attr: AttributeId)
-> Result<DataValue, OpcUaError>;

Specify a single node to read from AttributeId.

use darra_opcua::AttributeId;

// Read DisplayName
let dn = s.read_attr("ns=2;s=Temperature", AttributeId::DisplayName)?;
println!("name = {}", dn.variant().try_get_string().unwrap_or_default());

read_many​

pub fn read_many(&self, node_ids: &[&str], attr: AttributeId)
-> Result<Vec<Option<DataValue>>, OpcUaError>;

Reads the same attribute from multiple nodes in a single RPC batch. Returns Vec<Option<DataValue>> — a length equal to node_ids.len(), with failed entries being None.

let nodes = ["ns=2;s=T1", "ns=2;s=T2", "ns=2;s=T3"];
let values = s.read_many(&nodes, AttributeId::Value)?;
for (nid, dv) in nodes.iter().zip(values.iter()) {
match dv {
Some(d) => println!("{} = {:?}", nid, d.variant()),
None => println!("{} FAILED", nid),
}
}

Performance: Approximately 1 request for every 100 nodes ( RPC saving 99 RPC round trips); actual time taken varies depending on the network and server—please test in your own environment.

write​

pub fn write(&self, node_id: &str, value: &Variant)
-> Result<StatusCode, OpcUaError>;

Order Entry Node Value Properties.

use darra_opcua::Variant;

let mut v = Variant::new();
v.set_f64(42.5);
let st = s.write("ns=2;s=Setpoint", &v)?;
if !st.is_good() {
println!("Write failed: {}", st);
}

Returns StatusCode: GOOD / BAD_NOT_WRITABLE / BAD_TYPE_MISMATCH / BAD_USER_ACCESS_DENIED / ...

Business-level failures do not return Err.

write does not return a value ( Err unless a transport-level error occurs); business-level failures are determined by the return value Ok(StatusCode). You must check st.is_good(); don't just assume it's okay..

write_attr​

pub fn write_attr(&self, node_id: &str, attr: AttributeId, value: &Variant)
-> Result<StatusCode, OpcUaError>;

Specify an attribute (usually just use the default Value, called write via ).

Variant Quick Reference for Structures​

use darra_opcua::Variant;

let mut v = Variant::new();
v.set_bool(true);
v.set_byte(255);
v.set_i16(42);
v.set_i32(42);
v.set_u32(42);
v.set_i64(42);
v.set_f32(3.14);
v.set_f64(3.14);
v.set_string("hello");
v.set_datetime(std::time::SystemTime::now());

For more information, see Variant Data Types.

DataValue Field Quick Reference​

MethodsReturn TypeNote
variant()VariantActual Value (Borrowed, Not Released)
status()StatusCodeGood / Bad* / Uncertain*
is_good()bool== status().is_good()
source_timestamp()Option<SystemTime>Data Source Timestamp
server_timestamp()Option<SystemTime>Timestamps set by the server
let dv = s.read("ns=2;s=T1")?;
if !dv.is_good() {
println!("Bad value: {}", dv.status());
return Ok(());
}
let value = dv.variant().try_get_f64().unwrap_or(0.0);
println!("{} @ {:?}", value, dv.source_timestamp());

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 read_many; do not loop read (save N-1 iterations RPC)
  • Drop (Automatic Release): DataValue When the scope is exited, native resources are automatically released; no manual disposal is required.
  • write Check the return value: Don't assume it's necessarily "Good"; use st.is_good() / st.is_bad()
  • try_get_xxx: Variant Use try_get_xxx() -> Option<T> to prevent a panic due to a type mismatch
  • Use subscriptions for real-time changes: Don't do 1-second polling with read; instead, use Subscription (Server-side push)

Next Step​