Expand description
An OPC UA client for PLC data.
Point a Collector at a PLC and it discovers every tag, subscribes to as
many as the server will monitor, polls the rest, reconnects when the link
drops, and streams every change to a sink you choose. TagClient reads
and writes individual values on demand.
use opcua_tag_browser::{Collector, TagChange};
Collector::new("line1", "opc.tcp://192.168.201.2:4840")
.insecure()
.sink(|e: &TagChange<'_>| println!("{} = {}", e.path(), e.value))
.run()§Selecting what to collect
A PLC may expose thousands of nodes, and servers cap how many can be monitored at once. Selecting fewer than the ceiling means everything arrives by subscription and polling never engages.
c().only(["Machine/Axis1/Speed", "Machine/Axis1/Position"]);
c().matching("Machine/Axis*/Speed");
c().select(|tag| tag.path.starts_with("Machine/") && !tag.path.contains("Diag"));§Writing values
use opcua_tag_browser::Collector;
let plc = Collector::new("line1", "opc.tcp://192.168.201.2:4840")
.insecure()
.client()?;
plc.set("Machine/Axis1/Setpoint", 1500.0)?;
plc.set("Machine/Enable", true)?;
println!("{}", plc.get("Machine/Axis1/Speed")?);TagClient is Clone and Send, so it can be moved into another thread
while a collector’s event loop runs.
§Security
ConnectOptions::default signs and encrypts with Basic256Sha256 and
rejects untrusted server certificates. That fails against a server
configured for plaintext, which is deliberate: calling
Collector::insecure records the decision in your source instead of
inheriting it from a default.
use opcua_tag_browser::{ConnectOptions, Security};
use opcua_tag_browser::opcua::client::prelude::SecurityPolicy;
let secure = ConnectOptions::default()
.security(Security::SignAndEncrypt(SecurityPolicy::Basic256Sha256))
.user_name("collector", "hunter2");Sending a username over an unencrypted channel returns
Error::InsecureCredentials rather than transmitting it, and
Credentials redacts passwords in Debug output so they cannot reach a
log by accident.
§Sinks
TagSink is the seam between the crate and your application; the crate
decides nothing about where data goes. A closure implements it, and
sinks::LogSink and sinks::JsonlSink ship ready to use.
use opcua_tag_browser::{TagChange, TagSink};
struct Threshold;
impl TagSink for Threshold {
fn tag_changed(&self, event: &TagChange<'_>) {
if event.as_f64().is_some_and(|v| v > 100.0) {
println!("{} is high: {}", event.path(), event.value);
}
}
}§Shutting down
The crate never calls process::exit. In a binary,
Collector::handle_ctrl_c wires up a handler; inside a larger
application, take a CollectorHandle and stop it yourself.
Closing sessions on exit matters more than it looks. A server does not learn an abandoned session is gone until it times out, and until then its monitored items still count against the budget.
§Lower-level pieces
Collector composes parts that stay public. Reach for them when you want
the address space but not the monitoring:
| Type | Role |
|---|---|
connect | open a session |
PlcSession | session operations, with opcua panics contained |
NodeBrowser | list a node’s children, pagination handled |
NodeFilter | reject server furniture |
TreeScanner | walk the tree into PlcTag values |
TagSet | index tags for lookup by path, name, or glob |
TagRepository | cache the result |
§Feature flags
| Feature | Default | Effect |
|---|---|---|
monitoring | yes | Collector, subscriptions, polling |
write | yes | TagClient, Collector::client, PlcSession::write, OpcUaSession::inner: everything that can change a PLC |
json-cache | yes | JsonFileTagRepository and tag caching |
jsonl-sink | no | sinks::JsonlSink and Collector::jsonl |
ctrl-c | no | Collector::handle_ctrl_c |
full | no | all of the above |
§Version compatibility
This crate re-exports the opcua crate it was built against. Use that
path rather than your own opcua dependency, or a version mismatch produces
type errors that look unrelated to their cause.
opcua-tag-browser | opcua |
|---|---|
0.2 | 0.12 |
0.1 | 0.12 |
§Minimum supported Rust version
1.75. Raising it is a breaking change.
Re-exports§
pub use opcua;
Modules§
Structs§
- Accept
All - Keeps every node.
- Browsed
Node - A child node discovered while browsing.
- Client
Certificate - This client’s own certificate, as the server sees it.
- Collector
- Connects to one PLC, discovers its tags, and streams every change to a sink.
- Collector
Handle - Stops a running
Collectorand closes its sessions. - Connect
Options - Options for opening a client session.
- Connection
Change - A connection state transition.
- Connection
Info - What a session was opened with, for showing next to the server’s own diagnostics.
- Default
Node Filter - Drops nodes that are server furniture rather than process data.
- Json
File TagRepository - A
TagRepositorybacked by a pretty-printed JSON file. - Monitor
Options - Subscription and polling tuning.
- OpcUa
Node Browser - A
NodeBrowserbacked by a live session. - OpcUa
Session - A
PlcSessionbacked by a liveopcuaclient session. - PlcTag
- A single variable node discovered on an OPC UA server.
- Scan
Options - Tuning knobs for a scan.
- Scan
Progress - Where a running scan is, for showing it while it runs.
- Scan
Report - The outcome of a scan, including the subtrees that could not be read.
- TagChange
- A tag whose value or quality changed.
- TagClient
- Reads and writes individual tag values on demand.
- TagSet
- A scanned tag list, indexed for lookup by browse path and display name.
- Tree
Scanner - Recursively walks an address space, collecting variable nodes as tags.
Enums§
- Credentials
- How the client identifies itself to the server.
- Error
- Errors produced while connecting to, browsing, monitoring, or writing to an OPC UA server.
- Security
- Message security applied to a session.
- Source
- Which mechanism produced a value.
Traits§
- Node
Browser - Lists the children of a node.
- Node
Filter - Decides whether a node should be kept during a scan.
- PlcSession
- The OPC UA session operations this crate needs.
- TagRepository
- Stores and retrieves a cached tag list.
- TagSink
- Receives everything a
Collectorobserves.
Functions§
- catch_
panic - Runs a closure, converting any panic into
StatusCode::BadUnexpectedError. - client_
certificate - Reads the client certificate kept in
pki_dir, if one was made already. - connect
- Connects to an endpoint using the supplied options.
- discover
- Scans an endpoint and returns its tags.
- format_
quality - Renders the status code attached to a
DataValueas a short quality label. - format_
variant - Formats an OPC UA
Variantinto a compact display string. - panic_
message - Extracts a readable message from a panic payload.
- variant_
as_ bool - Interprets a
Variantas a boolean, if it holds one. - variant_
as_ f64 - Interprets a
Variantas a float, if it holds a number. - variant_
as_ i64 - Interprets a
Variantas a signed integer, if it holds an integral value.
Type Aliases§
- Result
- Convenience alias for results returned by this crate.