#include <iostream>
template <typename Tp>
{
auto val = client.
read(node).
cast<Tp>();
std::cout << "\033[32mValue: "<< val << "\033[0m\n";
}
template <typename Tp>
{
std::cout << "请输入要写入的值: ";
Tp val{};
std::cin >> val;
std::cout << "\033[32m写入成功\033[0m\n";
}
template <typename Tp>
{
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");
}
{
int num1{}, num2{};
std::cout << "计算两数之和 num1 + num2:\n";
std::cout << "请输入 num1: ";
std::cin >> num1;
std::cout << "请输入 num2: ";
std::cin >> num2;
auto [result, oargs] = client.
call(
"add", {num1, num2});
if (result)
std::cout << "\033[32m计算结果: " << oargs[0].cast<int>() << "\033[0m\n";
else
std::cout << "\033[31m调用失败\033[0m\n";
}
int main()
{
Client cli(
"opc.tcp://127.0.0.1:4840");
if (!cli.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>(cli, cli.find("value_1"));
else if (num == "1")
value<double>(cli, cli.find("value_2"));
else if (num == "2")
call(cli);
else if (num == "q")
break;
else
printf("\033[31m无效的编号\033[0m\n");
}
return 0;
}
OPC UA 客户端视图
定义 client.hpp:31
bool write(const NodeId &nd, const Variable &val) const
给指定的变量节点写数据
Variable read(const NodeId &nd) const
从指定的变量节点读数据
OPC UA 客户端
定义 client.hpp:118
std::pair< bool, Variables > call(const NodeId &obj_nd, std::string_view name, const Variables &inputs) const
在客户端调用指定对象节点中的方法
OPC UA 节点 ID
定义 utilities.hpp:40
static Tp cast(const rm::Variable &val)
将变量节点转化为指定类型的数据
定义 variable.hpp:207