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 RegisterNodes Accelerate.
  • For real-time change notifications Subscription Rather than a Read loop.

Read​

DataValue Read(std::string const& node_id,
AttributeId attr = AttributeId::Value);

Read the specified Attribute from a single node.

ParametersTypeDefaultNote
node_idstd::string const&—NodeId String (i=2258 / ns=2; s=...)
attrAttributeIdValue (13)Properties to Read

Returns: DataValue (RAII object, the destructor automatically releases the resource handle; move-only, (copy constructor has been deleted)

Exception: Exception (BadNodeIdUnknown / BadNotReadable / BadCommunicationError / ...) — These names are all enum class Status within

#include <darra/opcua/client.hpp>

// Read Value
auto dv = s.Read("ns=2;s=Temperature");
double t = 0; dv.Value().TryGetDouble(t);
std::cout << t << "\n";

// Read DisplayName
auto dn = s.Read("ns=2;s=Temperature", AttributeId::DisplayName);
std::cout << dn.Value().AsString() << "\n";

ReadMany​

std::vector<DataValue> ReadMany(
std::vector<std::string> const& node_ids,
AttributeId attr = AttributeId::Value);

A single batch RPC read of the same attribute from multiple nodes (opcua_session.h: "Batch Read: RPC Read attributes from multiple nodes in a single operation").

std::vector<std::string> nodes = { "ns=2;s=T1", "ns=2;s=T2", "ns=2;s=T3" };
auto values = s.ReadMany(nodes);
for (size_t i = 0; i < nodes.size(); ++i) {
std::cout << nodes[i] << " = " << values[i].Value().AsString() << "\n";
}

Performance: Retrieves RPC everything in a single operation, saving N-1 network round trips compared to a loop with single reads.

The entire batch transfer failed Exception. If only a single node failed to resolve, the overall result is still "Good," and the corresponding slot is Handle() marked as nullptr. Check this before using it:

for (auto& dv : values) {
if (!dv.Handle()) continue; // Failed to resolve this node
// ...
}

Write​

Status Write(std::string const& node_id,
Variant const& value,
AttributeId attr = AttributeId::Value);

Order-writing node.

Variant v; v.SetDouble(42.5);
Status st = s.Write("ns=2;s=Setpoint", v);
if (st != Status::Good) {
std::cerr << "Write failed: 0x" << std::hex << static_cast<uint32_t>(st) << "\n";
}

Returns Status (Good / BadNotWritable / BadUserAccessDenied / BadNodeIdUnknown / Bad / ...). If the type being written does not match, the server returns a 32-bit status code other than "Good"; Stack::StatusName(st) look up the code using.

Write does not throw any exceptions

Write throughout the process; it directly converts the 32-bit status code into a return Status value, Any failure returns a return value; no exceptions are thrown.. You must check the return value; do not assume it is "Good."

Variant Quick Reference for Structures​

Variant Construct a null value by default, then useChained setter Fill in (for each return Variant&).Variant is move-only, Cannot be copied.

#include <darra/opcua/client.hpp>

Variant v;
v.SetBool(true); // Boolean
v.SetSByte(-1); // SByte (According to the Int32 Chronicles)
v.SetByte(255); // Byte
v.SetInt16(42); // Int16
v.SetUInt16(42); // UInt16 (According to the UInt32 Chronicles)
v.SetInt32(42); // Int32
v.SetUInt32(42); // UInt32
v.SetInt64(42LL); // Int64
v.SetDouble(3.14); // Double
v.SetFloat(3.14f); // Float
v.SetString("hello"); // String
v.SetDateTime(file_time); // DateTime (FileTime: 100ns since 1601)

For more information, see Variant Data Types.

DataValue Quick Reference for Accessors​

DataValue Value Retrieval InterfaceThey're all methods, not fields。

AccessorTypeNote
Value()VariantActual value (borrowed resource handle, owns=false)
StatusCode()uint32_tOriginal 32-bit status code
StatusEnum()StatusStrongly Typed Enumerations
Status()uint32_tStatusCodeCompatibility aliases for ()
IsGood()boolEquivalent StatusCode() == 0
SourceTimestamp()int64_tFileTime (100ns since 1601)
ServerTimestamp()int64_tFileTime
Handle()DarraUa_DataValue*Resource handle; nullptr = No data in this slot
auto dv = s.Read("ns=2;s=T1");
if (!dv.IsGood()) {
std::cerr << "Bad value: 0x" << std::hex << dv.StatusCode() << "\n";
return;
}
double t = 0; dv.Value().TryGetDouble(t);
std::cout << t << " @ ft=" << dv.SourceTimestamp() << "\n";

AttributeId Common Values​

enum class AttributeId : int {
NodeId = 1, NodeClass = 2, BrowseName = 3, DisplayName = 4,
Description = 5, WriteMask = 6, UserWriteMask = 7,
IsAbstract = 8, Symmetric = 9, InverseName = 10,
ContainsNoLoops = 11, EventNotifier = 12,
Value = 13, // Default
DataType = 14, ValueRank = 15, ArrayDimensions = 16,
AccessLevel = 17, UserAccessLevel = 18,
MinimumSamplingInterval = 19, Historizing = 20,
Executable = 21, UserExecutable = 22,
};

Best Practices​

  • Lot Priority: For monitoring 100+ tags ReadMany; do not use a loop Read
  • RAII + move-only: auto dv = s.Read(...); Automatically released when it goes out of scope; do not use new; if you need to save it std::move, use, but do not write "copy"
  • Write: Check the return value: Don't assume it's necessarily good
  • Use subscriptions for real-time changes: Do not loop through Read; instead, use Subscription (Server-side push)

Next Step​