Skip to main content

Darra OPC UA Client — Rust SDK

Darra OPC UA Client Rust bindings. Advanced cross-language alignment API—the concepts you learn here can be directly applied to C#, Java, Python, C++, and C.

Chapter Navigation
  • Getting Started → Session (Construct / Connect / Read / Write / KeepAlive)
  • Browse → Nodes (browse / Path Resolution / register_nodes)
  • Real-time → Subscription (create_subscription / MonitoredItem / DataChange)
  • History → History (Raw / Modified / AtTime / Processed / Events / Update / Delete)
  • Call → Method (call)
  • Discover → Discovery (get_endpoints / find_servers)
  • Data → Data Types (Variant / DataValue / well_known_nodes)
  • Security → Security (Certificates / Encryption)

Installation​

cargo add darra-opcua

Or add Cargo.toml:

[dependencies]
darra-opcua = "0.6"

System Requirements​

ItemRequirements
RustEdition 2021
Operating SystemWindows 10/11 x64
Platformx64
AgreementOPC UA TCP (opc.tcp://)

Introduction​

use darra_opcua::{
Session, Subscription, Variant, DataValue,
AttributeId, NodeClass, MessageSecurityMode, StatusCode,
well_known_nodes,
};

Main Types:

TypeUses
SessionClient Superclass (a session OPC UA )
SubscriptionData Subscription Container
EventSubscriptionEvent Subscriptions (Alarms & Conditions)
NodeCollection / NodeLazy Load Node Access
Variant / DataValueData Encapsulation
Discovery (Static)get_endpoints / find_servers
well_known_nodes (mod)OPC Foundation Standard Node Constants
OpcUaErrorErrors related to StatusCode carrying
StatusCodenewtype + associated constants (is_good() / is_bad())

Quick Start in One Minute​

use darra_opcua::{Session, Variant};

fn main() -> Result<(), Box<dyn std::error::Error>> {
darra_opcua::initialize()?;

let mut s = Session::new("opc.tcp://localhost:4840")?;
s.connect()?;

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

// Write
let mut v = Variant::new();
v.set_f64(42.0);
s.write("ns=2;s=Setpoint", &v)?;

// Subscribe
let sub = s.create_subscription(500.0)?;
sub.add_node("ns=2;s=Counter", |args| {
println!("{:?} = {:?}", args.node_id, args.value_string);
})?;

Ok(()) // s / sub Automatically release resources when dropped
}

Advanced API Overview​

FeatureEntry APIDescription
Connection / EncryptionSession::new(...) + connect()None / Sign / SignAndEncrypt + Username / Anonymous / Certificate
Single-Point Read/Writes.read / writeSingle Node, Single Attribute
Batch Reads.read_many(nodes, attr)Once RPC, N nodes
Browse Trees.browse / browse_many / browse_with_paging / browse_nextFull pagination support
Path Resolutions.resolve / translate_browse_pathsBrowse Path → NodeId
Registration Nodes.register_nodes / unregister_nodesReduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server testing)
Data Subscriptionss.create_subscription + sub.add_node / add_manypublishing / sampling, two-layer
Event Subscriptions.subscribe_eventsAlarms & Conditions
Adjustment Methodss.call(obj_id, mtd_id, &[...])OPC UA Method Node
History Readings.read_history / read_history_*Raw / Modified / AtTime / Processed / Events
History Writings.update_history / delete_history_rangeInsert / Replace / Update / Delete
Heartbeats.set_keep_alive_interval_ms(ms)Background Periodic Monitoring
Automatic Reconnection(Built-in)Check for Disconnections → Republish Subscribe
Unified Events.events.lock().unwrap().on_*Session / Subscription / Data / Communication Errors
Service DiscoveryDiscovery::get_endpoints / find_serversNo session required

Result Error Handling​

All returns API Result<T, OpcUaError> that may fail are propagated using the operator ?:

fn quick(s: &Session) -> Result<f64, darra_opcua::OpcUaError> {
let dv = s.read("ns=2;s=Temperature")?; // Return immediately upon failure
Ok(dv.variant().try_get_f64().unwrap_or(0.0)) // None Fallback
}

OpcUaError Field:

pub struct OpcUaError {
pub status: StatusCode, // Business-Level Status Codes
pub message: String, // Short Text Description
}

Drop (Automatic Release)​

Session / Subscription / Variant / DataValue All implemented Drop — Automatically calls the underlying function *_Close / when leaving the scope *_Delete, No manual release is required:

{
let mut s = Session::new("opc.tcp://localhost:4840")?;
s.connect()?;
let dv = s.read("i=2258")?;
// ... working with a DV camera ...
} // <- Here: dv/s Drop, automatic resource release + Disconnect

Cross-Language Alignment​

The Rust SDK follows the snake_case naming convention, but its API semantics match the C# DarraOpcUa (ua.Read ↔ Rust s.read). Choose either C# or Rust to get started; when switching to other languages later, you’ll only need to change the naming—the concepts and semantics remain the same.

Just read through the left sidebar in order to master everything SDK API.