call
- Data Types OpcUaVariant It is a container for input and output parameters.
- Find a way to use NodeId browse Pair with
filter=NodeClass.Method.
Signature
def call(self,
object_node_id: str,
method_node_id: str,
inputs: Optional[List[OpcUaVariant]] = None) -> List[OpcUaVariant]: ...
| Parameters | Note |
|---|---|
| object_node_id | The method is located in the Object node NodeId |
| method_node_id | The Method Itself NodeId |
| inputs | Input parameters: List OpcUaVariant (None / [] indicates no parameters) |
Returns List of output List[OpcUaVariant] parameters.
Memory Ownership (Important)
In the returned list, Each OpcUaVariant one holds a native handle (_owns=True), You must call dispose; otherwise, native memory Variant will be leaked.
Recommended wording:
outputs = ua.call(obj_id, mtd_id, args)
for v in outputs:
with v:
print(v.as_int32)
Or explicitly:
outputs = ua.call(obj_id, mtd_id, args)
try:
for v in outputs:
# Using v.as_xxx etc.
...
finally:
for v in outputs:
v.close()
Examples
from darra_opcua import OpcUaVariant
# Adjust Calculator.Add(3, 4)
inputs = [OpcUaVariant().set_int32(3), OpcUaVariant().set_int32(4)]
outputs = ua.call(
object_node_id="ns=2;s=Calculator",
method_node_id="ns=2;s=Calculator.Add",
inputs=inputs)
for v in outputs:
with v:
print(f"sum = {v.as_int32}")
# Dispose of input parameters when they are no longer needed (call they are not consumed internally).
for v in inputs:
v.close()
Simplified Version:
def call_simple(ua, obj, mtd, *vs):
outputs = ua.call(obj, mtd, list(vs))
try:
return [v.value for v in outputs] # value = Pythonic decoding
finally:
for v in outputs: v.close()
for v in vs: v.close()
result = call_simple(ua, "ns=2;s=Calc", "ns=2;s=Calc.Add",
OpcUaVariant().set_int32(3), OpcUaVariant().set_int32(4))
print(result) # [7]
Exception
call Failure exception ( OpcUaException thrown when both NodeId parameters are null ValueError); the difference lies in the code contained ex.status_code within it:
| Trigger Points | status_code |
|---|---|
| object_node_id / method_node_id is empty | ValueError (Not an OpcUaException) |
| NodeId String parsing failed | StatusCode.BadNodeIdInvalid |
| RPC Transmission Failed (Underlying layer returned a non-0 value) | Transport layer status codes, such as BadCommunicationError / BadTimeout |
| The server returns a non-0 method status | Business code provided by the server (e.g., BadUserAccessDenied) |
Business-level failures (when a server-side method returns a value other than "Good") also throw an exception— ex.status_code specifically, the business code; the compatibility alias ex.status points to the same value.
Characters not included in the enumeration StatusCode (such as BadInvalidArgument / BadMethodInvalid) will not be converted to Bad — but will be retained as is ex.status_code int.
Looking for a way to... NodeId
from darra_opcua import NodeClass
# 1. browse Object Child Node Filtering Method
methods = ua.browse("ns=2;s=Calculator", filter=NodeClass.Method)
for m in methods:
print(f" {m.browse_name} -> {m.node_id}")
# 2. Check the input/output parameter signatures (browse the properties of the method node)
props = ua.browse(methods[0].node_id) # Includes InputArguments / OutputArguments
Input Parameter Construction
Constructing method signatures OpcUaVariant:
# Add(Int32, Int32) -> Int32
OpcUaVariant().set_int32(3), OpcUaVariant().set_int32(4)
# SetTemperature(string deviceId, double value)
OpcUaVariant().set_string("Boiler1"), OpcUaVariant().set_double(85.5)
set_* All are stream-based (return self), and can be written consecutively; see the complete list at OpcUaVariant。
Complex structure (ExtensionObject) The current one SDK cannot OpcUaVariant be decoded/constructed and cannot be passed in.
Asynchronous
call It is synchronous and blocking (send once, RPC wait for a response), There is no asynchronous version. To run asyncio it inside, wrap it in a thread:
import asyncio
result = await asyncio.to_thread(ua.call, obj_id, mtd_id, args)