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.
- 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
| Item | Requirements |
|---|---|
| Rust | Edition 2021 |
| Operating System | Windows 10/11 x64 |
| Platform | x64 |
| Agreement | OPC UA TCP (opc.tcp://) |
Introduction
use darra_opcua::{
Session, Subscription, Variant, DataValue,
AttributeId, NodeClass, MessageSecurityMode, StatusCode,
well_known_nodes,
};
Main Types:
| Type | Uses |
|---|---|
| Session | Client Superclass (a session OPC UA ) |
| Subscription | Data Subscription Container |
| EventSubscription | Event Subscriptions (Alarms & Conditions) |
| NodeCollection / Node | Lazy Load Node Access |
| Variant / DataValue | Data Encapsulation |
| Discovery (Static) | get_endpoints / find_servers |
| well_known_nodes (mod) | OPC Foundation Standard Node Constants |
| OpcUaError | Errors related to StatusCode carrying |
| StatusCode | newtype + 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
| Feature | Entry API | Description |
|---|---|---|
| Connection / Encryption | Session::new(...) + connect() | None / Sign / SignAndEncrypt + Username / Anonymous / Certificate |
| Single-Point Read/Write | s.read / write | Single Node, Single Attribute |
| Batch Read | s.read_many(nodes, attr) | Once RPC, N nodes |
| Browse Tree | s.browse / browse_many / browse_with_paging / browse_next | Full pagination support |
| Path Resolution | s.resolve / translate_browse_paths | Browse Path → NodeId |
| Registration Node | s.register_nodes / unregister_nodes | Reduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server testing) |
| Data Subscriptions | s.create_subscription + sub.add_node / add_many | publishing / sampling, two-layer |
| Event Subscription | s.subscribe_events | Alarms & Conditions |
| Adjustment Methods | s.call(obj_id, mtd_id, &[...]) | OPC UA Method Node |
| History Reading | s.read_history / read_history_* | Raw / Modified / AtTime / Processed / Events |
| History Writing | s.update_history / delete_history_range | Insert / Replace / Update / Delete |
| Heartbeat | s.set_keep_alive_interval_ms(ms) | Background Periodic Monitoring |
| Automatic Reconnection | (Built-in) | Check for Disconnections → Republish Subscribe |
| Unified Event | s.events.lock().unwrap().on_* | Session / Subscription / Data / Communication Errors |
| Service Discovery | Discovery::get_endpoints / find_servers | No 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.