Skip to main content

Darra OPC UA Client — Python SDK

Darra OPC UA Client Python bindings. Advanced cross-language alignment API—the concepts you learn here can be directly applied to C#, Java, C++, Rust, 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 / data_changed)
  • History → History (raw / modified / at_time / processed / events / update / delete)
  • Call → Method (call)
  • Discover → Discovery (get_endpoints / find_servers)
  • Data → Data Types (OpcUaVariant / OpcUaDataValue / well_known_nodes)
  • Security → Security (Certificates / Encryption)

Installation​

pip install darra-opcua

The Wheel package contains Windows x64 dynamic libraries; after installation via pip, there is no need to manually configure the loading path.

System Requirements​

ItemRequirements
Python3.8+ (Recommended 3.11)
Operating SystemWindows 10/11 x64
CPUx64
AgreementOPC UA TCP (opc.tcp://)

Importing Modules​

from darra_opcua import (          # Module Name darra_opcua (pip package darra-opcua)
OpcUaSession, # Superclass (= DarraOpcUa Alias)
OpcUaVariant, OpcUaDataValue,
OpcUaSubscription, EventSubscription,
OpcUaNode, NodeCollection, NamespaceCollection, Reference,
OpcUaDiscovery,
MessageSecurityMode, UserTokenType, SessionState,
BuiltinType, NodeClass, AttributeId, MonitoringMode,
StatusCode, OpcUaException,
HistoryAggregate, HistoryUpdateType,
well_known_nodes as wkn,
)

Main Types:

TypeUses
OpcUaSessionClient base class (= DarraOpcUa; the two are equivalent)
OpcUaSubscriptionData Subscription Container (Subscription formerly known as )
EventSubscriptionEvent Subscriptions (Alarms & Conditions)
NodeCollection / OpcUaNodeLazy Load Node Access
OpcUaVariant / OpcUaDataValueData Encapsulation
OpcUaDiscoveryget_endpoints / find_servers (static method)
well_known_nodes (Module)OPC Foundation Standard node constant; short alias: wkn
OpcUaExceptionExceptions containing status_code

Quick Start in One Minute​

from darra_opcua import OpcUaSession, OpcUaVariant

with OpcUaSession("opc.tcp://localhost:4840") as ua:
ua.connect()

# Read
with ua.read("ns=2;s=Temperature") as dv:
print(f"Temperature = {dv.value} °C")

# Write (OpcUaVariant hold the resource handle; close it when finished)
with OpcUaVariant().set_double(42.0) as v:
ua.write("ns=2;s=Setpoint", v)

# Subscribe
with ua.create_subscription(500) as sub:
sub.data_changed.append(
lambda e: print(f"{e.node_id} = {e.value_string}"))
sub.add_node("ns=2;s=Counter")

import time
time.sleep(3)

Advanced API Overview​

FeatureEntry APIDescription
Connection / EncryptionOpcUaSession(...) + connect()NONE / Sign / SignAndEncrypt + Username / Anonymous / Certificate
Single-Point Read/Writeua.read / writeSingle Node, Single Attribute
Batch Readua.read_many(node_ids[, attribute])Once RPC, N nodes
Browse Treeua.browse / browse_many / browse_with_paging / browse_nextFull pagination support
Path Resolutionua.resolve / translate_browse_pathsBrowse Path → NodeId
Registration Nodeua.register_nodes / unregister_nodesReduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server-side testing)
Data Subscriptionsua.create_subscription + sub.add_node / add_manypublishing / sampling, two-layer
Event Subscriptionua.subscribe_eventsAlarms & Conditions
Adjustment Methodsua.call(obj_id, mtd_id, [...])OPC UA Method Node
History Readingua.read_history / ua.read_history_*Raw / Modified / AtTime / Processed / Events
Writing Historyua.update_history / ua.delete_history_rangeInsert / Replace / Update / Delete
Heartbeatua.keep_alive_interval_msBackground Periodic Monitoring
Automatic Reconnection(Built-in)Check for Disconnections → Republish Subscribe
Unified Eventua.events.*Session / Subscription / Data / Communication Errors
Service DiscoveryOpcUaDiscovery.get_endpoints / find_serversNo session required
Standard Nodewkn.SERVER_SERVER_STATUS_CURRENT_TIME, etc.OPC Foundation Standard NodeId Constants

Key Points About Python​

  • Context manager (with): OpcUaSession / OpcUaSubscription / OpcUaDataValue / OpcUaVariant All supported with; resources are automatically released upon exit. Highly Recommended, Do not do this manually close().
  • snake_case method names: ua.read / ua.browse / ua.create_subscription (not Read / Browse).
  • datetime All UTC: History / Always use datetime.datetime (tzinfo=timezone.utc) for timestamps; do not use local time.
  • MessageSecurityMode.NONE Uppercase: Python keyword conflict None; use MessageSecurityMode.NONE (remaining values Sign / SignAndEncrypt case-sensitive like C#).
  • Callback Thread: The callback is triggered by a background SDK thread.DataChangeEventArgs A snapshot of the field has already been taken in the callback thread, making it safe for cross-thread use.

Cross-Language Alignment​

The API naming and behavior of the C# SDK are the reference implementation for bindings in other languages. The Python binding follows the PEP 8 convention (snake_case + property + context manager), but the semantics are consistent (e.g. C# ua.Read ↔ Python ua.read). Start with Python; switching to another language later only requires renaming — the concepts and semantics stay the same.

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