Skip to main content

Call

Preliminary / Supporting
  • Data Types Variant It is a container for input and output parameters.
  • Find a way to use NodeId Browse Pass NodeClass::Method through the filter.

Signature​

namespace darra::opcua {
class Session {
public:
std::vector<Variant> Call(std::string const& object_node_id,
std::string const& method_node_id,
std::vector<Variant> const& inputs);
};
}
ParametersNote
object_node_idMethod on the Object node (NodeId string)
method_node_idThe Method Itself NodeId (NodeId String)
inputsInput Parameter List Variant (borrowed, not released)

Returns a list of output std::vector<Variant> parameters. Each returned item holds Variant a resource handle, which is automatically released upon destruction (stack objects).

Memory Ownership​

  • Input parameters: SDK Just borrowing Handle(), Do not release. The caller continues to hold the object; it is destroyed Variant when it leaves the scope.
  • Return value std::vector<Variant>: Each holds Variant a resource handle, which is automatically released upon destructor execution. It is recommended to use stacked objects:
auto outputs = s.Call(obj_id, mtd_id, args);
for (auto const& v : outputs) {
int32_t x = 0;
v.TryGetInt32(x);
std::cout << x << "\n";
}
// outputs: Once they are out of scope, all Variant are destroyed.

Variant is move-only (Variant(Variant const&) = delete), so the parameter list must be std::move passed by value, not by copy.

Examples​

#include <darra/opcua/client.hpp>
#include <iostream>

using namespace darra::opcua;

Session s("opc.tcp://localhost:4840");
s.Connect();

// Adjust Calculator.Add(3, 4)
std::vector<Variant> inputs;
{ Variant a; a.SetInt32(3); inputs.push_back(std::move(a)); }
{ Variant b; b.SetInt32(4); inputs.push_back(std::move(b)); }

auto outputs = s.Call(
"ns=2;s=Calculator",
"ns=2;s=Calculator.Add",
inputs);

for (auto const& v : outputs) {
int32_t sum = 0; v.TryGetInt32(sum);
std::cout << "sum = " << sum << "\n";
}

Exception​

Call Exception Handler (darra::opcua::Exception Inheritance std::runtime_error):

Throwing PointInvalid Content
Failed to parse object_node_idException("Parse object NodeId '<id>' failed", Status::BadNodeIdInvalid)
Failed to parse method_node_idException("Parse method NodeId '<id>' failed", Status::BadNodeIdInvalid)
Transmission Layer FailureException("Call '<method_id>' transport failed", <原始状态码>)
The server returned a method code other than "Good"Exception("Call '<method_id>' returned status", <方法返回码>)

Business-level failures (methods that return a value other than "Good") also throw exceptions; use ex.status_code() to retrieve the business code and Stack::StatusName(ex.status_code()) to retrieve the human-readable name. Don't use it to ex.status() judge whether something is good or bad — In C++, Status enumerations are named only for a subset of the source code; values that are not named are not equal to any enumeration constants after a cast.

If you don't want to catch exceptions, just use CallSafe (<darra/opcua/safe_api.hpp>), which suppresses them Result<std::vector<Variant>>.

Looking for a way to... NodeId​

// 1. Browse Object Child Node Filtering Method
auto methods = s.Browse("ns=2;s=Calculator", NodeClass::Method);
for (auto const& m : methods) {
std::cout << " " << m.browse_name << " -> " << m.node_id << "\n";
}

// 2. Check the input/output parameter signatures (Browse the properties of the method node)
auto props = s.Browse(methods[0].node_id); // Includes InputArguments / OutputArguments

Input Parameter Construction​

Constructing method signatures Variant:

// Add(Int32, Int32) -> Int32
Variant a; a.SetInt32(3);
Variant b; b.SetInt32(4);
std::vector<Variant> args;
args.push_back(std::move(a));
args.push_back(std::move(b));

// SetTemperature(string deviceId, double value)
Variant id; id.SetString("Boiler1");
Variant val; val.SetDouble(85.5);

Variant The scalar setter overrides SetBool / SetSByte / SetByte / SetInt16 / SetUInt16 / SetInt32 / SetUInt32 / SetInt64 / SetFloat / SetDouble / SetString / SetDateTime, and returns Variant& chaining support. Note that Variant is move-only, and chaining resultsCannot be copied into a new variable — First set the value using a named object, then std::move place it into std::vector<Variant>.

Currently, C++ does not have Variant array setters or ExtensionObject encoder/decoder entry points. (See the implementation notes struct_decoder.hpp for / method_builder.hpp ), so array and complex structure parameters cannot currently be constructed directly from the C++ side; only scalars can be passed.

Asynchronous​

Call is synchronous and blocking (one request at a time). The SDK has no asynchronous CallAsync entry point like that. — If you need asynchrony, start your own thread (requires #include <future>):

auto fut = std::async(std::launch::async, [&]() {
return s.Call(obj_id, mtd_id, args);
});
auto outputs = fut.get();

Chained calls​

Don't want to jerk off? std::vector<Variant> Just use it MethodBuilder. See you. MethodBuilder.

Next Step​