Skip to content

Commit

Permalink
opcua模块服务器支持修改名称,并完善例程
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut committed Mar 19, 2024
1 parent 9a63859 commit 6d4b7b4
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ include(cmake/RMVLVersion.cmake)
# ----------------------------------------------------------------------------
# RMVL parameters and miscellaneous generation
# ----------------------------------------------------------------------------
set(para_template_path "${CMAKE_SOURCE_DIR}/cmake/templates" CACHE STRING "GenPara template path" FORCE)
set(para_template_path "${CMAKE_SOURCE_DIR}/cmake/templates" CACHE INTERNAL "GenPara template path" FORCE)
include(cmake/RMVLGenPara.cmake)

# ----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions modules/opcua/include/rmvl/opcua/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ class Server
* @brief 创建 OPC UA 服务器
*
* @param[in] port OPC UA 服务器端口号,一般设置为 `4840U`
* @param[in] name OPC UA 服务器名称,为空则采用默认值 `open62541-based OPC UA Application`
* @param[in] users 用户列表 @see UserConfig
*/
explicit Server(uint16_t port, const std::vector<UserConfig> &users = {});
Server(uint16_t port, std::string_view name = {}, const std::vector<UserConfig> &users = {});

/**
* @brief 从服务器配置函数指针创建 OPC UA 服务器
Expand All @@ -70,9 +71,10 @@ class Server
*
* @param[in] on_config 服务器配置函数指针
* @param[in] port OPC UA 服务器端口号,一般设置为 `4840U`
* @param[in] name OPC UA 服务器名称,为空则采用默认值 `open62541-based OPC UA Application`
* @param[in] users 用户列表 @see UserConfig
*/
Server(ServerUserConfig on_config, uint16_t port, const std::vector<UserConfig> &users = {});
Server(ServerUserConfig on_config, uint16_t port, std::string_view name = {}, const std::vector<UserConfig> &users = {});

Server(const Server &) = delete;
Server(Server &&) = delete;
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/param/opcua.para
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uint32_t SPIN_TIMEOUT = 500 # 服务器超时响应的时间,单位 (ms)
uint32_t SPIN_TIMEOUT = 10 # 服务器超时响应的时间,单位 (ms)

double SAMPLING_INTERVAL = 2 # 服务器监视变量的采样速度,单位 (ms),不得小于 2ms
double PUBLISHING_INTERVAL = 2 # 服务器尝试发布数据变更的期望时间间隔,若数据未变更则不会发布,单位 (ms),不得小于 2ms
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace rm
/************************************** 发布者 **************************************/

Publisher<TransportID::UDP_UADP>::Publisher(const std::string &pub_name, const std::string &address, uint16_t port,
const std::vector<UserConfig> &users) : Server(port, users), _name(pub_name)
const std::vector<UserConfig> &users) : Server(port, pub_name, users), _name(pub_name)
{
//////////////////// 添加连接配置 ////////////////////
UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP());
Expand Down
17 changes: 15 additions & 2 deletions modules/opcua/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,25 @@ namespace rm

// ============================= 基本配置 =============================

Server::Server(uint16_t port, const std::vector<UserConfig> &users)
Server::Server(uint16_t port, std::string_view name, const std::vector<UserConfig> &users)
{
_server = UA_Server_new();

UA_ServerConfig *config = UA_Server_getConfig(_server);
UA_ServerConfig_setMinimal(config, port, nullptr);
// 修改名字
if (!name.empty())
{
UA_LocalizedText_clear(&config->applicationDescription.applicationName);
config->applicationDescription.applicationName = UA_LOCALIZEDTEXT_ALLOC("en-US", name.data());
for (size_t i = 0; i < config->endpointsSize; ++i)
{
UA_LocalizedText *ptr = &config->endpoints[i].server.applicationName;
UA_LocalizedText_clear(ptr);
*ptr = UA_LOCALIZEDTEXT_ALLOC("en-US", name.data());
}
}
// 修改采样间隔和发布间隔
config->samplingIntervalLimits.min = 2.0;
config->publishingIntervalLimits.min = 2.0;
if (!users.empty())
Expand All @@ -56,7 +69,7 @@ Server::Server(uint16_t port, const std::vector<UserConfig> &users)
}
}

Server::Server(ServerUserConfig on_config, uint16_t port, const std::vector<UserConfig> &users) : Server(port, users)
Server::Server(ServerUserConfig on_config, uint16_t port, std::string_view name, const std::vector<UserConfig> &users) : Server(port, name, users)
{
if (on_config != nullptr)
on_config(_server);
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace rm
{

Subscriber<TransportID::UDP_UADP>::Subscriber(const std::string &sub_name, const std::string &address, uint16_t port,
const std::vector<UserConfig> &users) : Server(port, users), _name(sub_name)
const std::vector<UserConfig> &users) : Server(port, sub_name, users), _name(sub_name)
{
//////////////////// 添加连接配置 ////////////////////
UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP());
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/test/test_opcua_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST(OPC_UA_Server, value_config)
// 服务器添加变量节点
TEST(OPC_UA_Server, add_node)
{
rm::Server svr(4840);
rm::Server svr(4840, "TestServer");
rm::Variable variable = 3.1415;
variable.browse_name = "test_double";
variable.description = "this is test double";
Expand Down
4 changes: 4 additions & 0 deletions modules/rmath/include/rmvl/rmath/uty_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ using Matx11f = Matx<float, 1, 1>;
using Matx11d = Matx<double, 1, 1>;
using Matx51f = Matx<float, 5, 1>;
using Matx15f = Matx<float, 1, 5>;
using Matx51d = Matx<double, 5, 1>;
using Matx15d = Matx<double, 1, 5>;
using Matx55f = Matx<float, 5, 5>;
using Matx55d = Matx<double, 5, 5>;

} // namespace cv

Expand Down
6 changes: 3 additions & 3 deletions samples/opcua/opcua_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
int main()
{
rm::Client client("opc.tcp://localhost:4840");
auto var_id = rm::nodeObjectsFolder | client.find("var_demo");
auto var = client.read(var_id);
printf("\033[32mValue: %d\033[0m\n", var.cast<int>());
auto position_id = rm::nodeObjectsFolder | client.find("position");
auto position = client.read(position_id);
printf("\033[32mValue: %d\033[0m\n", position.cast<int>());
return 0;
}
25 changes: 15 additions & 10 deletions samples/opcua/opcua_server.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#include <csignal>

#include "rmvl/opcua/server.hpp"

rm::Server *p_server{nullptr};

inline void onHandle(int) { p_server->stop(); }

int main()
{
rm::Server server(4840U);
rm::Variable var = 42;
var.display_name = "VarDemo";
var.browse_name = "var_demo";
server.addVariableNode(var);
server.start();
signal(SIGINT, onHandle);

rm::Server server(4840);
p_server = &server;

printf("\033[32mServer started for 20 seconds ...\033[0m\n");
std::this_thread::sleep_for(std::chrono::seconds(20));
printf("\033[32mServer stopped ...\033[0m\n");
server.stop();
rm::Variable position = 5500;
position.display_name = "Position";
position.browse_name = "position";
server.addVariableNode(position);
server.start();
server.join();
return 0;
}

0 comments on commit 6d4b7b4

Please sign in to comment.