RMVL  2.1.0-dev
Robotic Manipulation and Vision Library
载入中...
搜索中...
未找到
samples/opcua/opcua_client.cpp

OPC UA 客户端例程

OPC UA 客户端例程

#include <iostream>
using namespace rm;
template <typename Tp>
void read(ClientView client, const rm::NodeId &node)
{
auto val = client.read(node).cast<Tp>();
std::cout << "\033[32mValue: "<< val << "\033[0m\n";
}
template <typename Tp>
void write(ClientView client, const rm::NodeId &node)
{
std::cout << "请输入要写入的值: ";
Tp val{};
std::cin >> val;
client.write(node, val);
std::cout << "\033[32m写入成功\033[0m\n";
}
template <typename Tp>
void value(ClientView client, const rm::NodeId &node)
{
std::cout << "- 0: 读取\n- 1: 写入\n";
std::cout << "请输入要操作的编号: \n";
std::string num{};
std::cin >> num;
if (num == "0")
read<Tp>(client, node);
else if (num == "1")
write<Tp>(client, node);
else
printf("\033[31m无效的编号\033[0m\n");
}
void call(Client &client)
{
int num1{}, num2{};
std::cout << "计算两数之和 num1 + num2:\n";
std::cout << "请输入 num1: ";
std::cin >> num1;
std::cout << "请输入 num2: ";
std::cin >> num2;
std::vector<Variable> oargs;
bool result = client.call("add", {num1, num2}, oargs);
if (result)
std::cout << "\033[32m计算结果: " << oargs[0].cast<int>() << "\033[0m\n";
else
std::cout << "\033[31m调用失败\033[0m\n";
}
int main()
{
Client client("opc.tcp://127.0.0.1:4840");
if (!client.ok())
return -1;
while (true)
{
std::cout << "- 0: value_1\n- 1: value_2\n- 2: add\n- q: 退出程序\n";
std::cout << "请输入要操作的编号: \n";
std::string num{};
std::cin >> num;
if (num == "0")
value<int>(client, nodeObjectsFolder | client.find("value_1"));
else if (num == "1")
value<double>(client, nodeObjectsFolder | client.find("value_2"));
else if (num == "2")
call(client);
else if (num == "q")
break;
else
printf("\033[31m无效的编号\033[0m\n");
}
return 0;
}
OPC UA 客户端视图
定义 client.hpp:31
bool write(const NodeId &node, const Variable &val) const
给指定的变量节点写数据
Variable read(const NodeId &node) const
从指定的变量节点读数据
OPC UA 客户端
定义 client.hpp:101
bool call(const NodeId &obj_node, const std::string &name, const std::vector< Variable > &inputs, std::vector< Variable > &outputs) const
在客户端调用指定对象节点中的方法
FindNodeInClient find(std::string_view browse_name, uint16_t ns=1U) const
获取路径搜索必要信息
定义 client.hpp:136
bool ok() const
是否成功创建客户端并成功连接到服务器
定义 client.hpp:141
OPC UA 节点 ID
定义 utilities.hpp:36
static Tp cast(const rm::Variable &val)
将变量节点转化为指定类型的数据
定义 variable.hpp:170
OPC UA 客户端
定义 datastruct.hpp:20