bytehound-opc-da-client
Backend-agnostic OPC DA client library for Rust — async, trait-based, with transparent COM management.
Features
- Async/Await API: Built for modern asynchronous Rust using
tokioandasync-trait. - Trait-Based Abstraction: The
OpcProvidertrait allows for easy mocking and backend swapping. - Transparent COM Management: Handles COM initialization (
CoInitializeEx) and apartment thread affinity automatically in the background. - Read & Write Support: Read tag values and write typed values (
Int,Float,Bool,String) to OPC tags. - Scalable Native Browsing: Open isolated sessions and request bounded, one-level pages through OPC DA 3.0 with an automatic OPC DA 2.x fallback.
- Bounded Namespace Inventory: Stream exact ItemIDs with breadcrumb labels through a cancellable, bounded DA 3.0/2.x traversal.
- Failure-safe Inventory Worker: Converts worker panics and inventory errors into terminal stream errors instead of silently ending the stream.
- Defensive COM Iterators: Rejects native enumerator counts that exceed the fixed cache capacity before indexing the returned buffer.
- Windows COM/DCOM Support: Native OPC DA backend via
windows-rs— no external OPC crates needed. - Robust Error Handling: Leverages
thiserrorfor theOpcErrordomain type andfriendly_com_hint()for human-readable HRESULT explanations. - Test-Friendly: Built-in
MockOpcProvidervia thetest-supportfeature.
Installation
Add this to your Cargo.toml:
[]
= { = "bytehound-opc-da-client", = "0.2.3" }
Prerequisites
- Operating System: Windows (COM/DCOM is a Windows-only technology).
- Rust: 1.88 or newer.
- OPC DA Core Components: Ensure the OPC DA Core Components are installed and registered on your system.
- DCOM Configuration: If connecting to remote servers, appropriate DCOM permissions must be configured.
Usage Examples
Connecting & Listing Servers
Enumerate available OPC DA servers on a local or remote host.
use ;
async
Reading Tags
Connect to a specific server and read current values for a set of tags.
use ;
async
read_tag_values is the machine-facing read API. For VT_BSTR values, TagValue::value
contains the exact COM string contents: no quote characters are added or removed. Consumers
that intentionally want the historical quoted string presentation can call
read_tag_values_for_display; its default trait implementation falls back to
read_tag_values for third-party providers.
Writing a Value
Write a typed value to a single OPC tag.
use ;
async
Browsing the Address Space
Recursively discover available tags on an OPC server.
use ;
use ;
async
For large namespaces, use the bounded native browse API instead of recursive discovery:
use ;
async
Session, node, and continuation tokens are opaque UUIDs. Native browse sessions
own dedicated server connections, expire after five minutes of inactivity, and
never expose COM pointers or OPC DA continuation strings. Transport adapters can
encode tokens with to_string() and restore them with each token type's
parse() method.
The DA 2.x fallback merges a same-named branch and leaf into one
BrowseNodeKind::BranchAndItem node and resolves its exact item ID through
GetItemID.
For large namespaces, start_inventory streams a bounded inventory without
persisting browse-session or continuation tokens:
use ;
async
The returned InventoryStream exposes pause, resume, and cancellation controls.
Each native browse call is bounded by InventoryOptions::batch_size, and
max_entries can cap a deliberately limited inventory.
If a DA2 server rejects a branch name with E_INVALIDARG, the client probes native
navigation before deciding whether it is safe to skip; navigable branches are retained,
and genuinely non-navigable names are reported as a completion warning.
Architecture
The library is split into a core trait layer and concrete implementations:
OpcProvider: The primary async trait defining server discovery, recursive tag browsing, native paged browsing, reads, and writes.OpcDaClient: The default implementation using nativewindows-rsCOM calls. Generic overServerConnectorfor testability; defaults toComConnector.
See architecture.md for in-depth design details and spec.md for behavioral contracts.
COM Threading Model
OPC DA relies on Windows COM, which requires per-thread initialization and strict thread affinity. The opc-da-client dependency alias handles this transparently:
- Dedicated Worker Thread: All COM operations are executed on a dedicated background worker thread initialized in Multi-Threaded Apartment (MTA) mode.
- No Manual Init: You do not need to call
CoInitializeor manage COM lifecycles in your calling application. - Host Thread Initialization: Applications that also perform COM work on their own thread can hold a public
ComGuard::new()guard for that thread's lifetime.
License
This project is licensed under the MIT License.