Skip to content

Commit e6808ce

Browse files
committed
added standard examples for open62541(pp)
1 parent edf607a commit e6808ce

File tree

10 files changed

+113
-3
lines changed

10 files changed

+113
-3
lines changed

CMakePresets.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"cacheVariables": {
1919
"CPP_STARTER_USE_SML": "ON",
2020
"CPP_STARTER_USE_BOOST_BEAST": "ON",
21-
"CPP_STARTER_USE_CROW": "ON"
21+
"CPP_STARTER_USE_CROW": "ON",
22+
"CPP_STARTER_USE_OPEN62541PP": "ON",
23+
"CPP_STARTER_USE_OPEN62541": "ON"
2224
}
2325
},
2426
{

cmake/Options.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ OPTION(CPP_STARTER_USE_CROW "Enable compilation of crow sample" OFF)
3636
OPTION(CPP_STARTER_USE_CPPZMQ_PROTO "Enable compilation of protobuf and cppzmq sample" OFF)
3737
OPTION(CPP_STARTER_USE_EMBEDDED_TOOLCHAIN "Enable compilation of an example cortex m4 project" OFF)
3838
OPTION(CPP_STARTER_USE_QT "Enable compilation of an example QT project" OFF)
39+
OPTION(CPP_STARTER_USE_OPEN62541PP "Enable compilation of an example open62541pp wrapper project" OFF)
40+
OPTION(CPP_STARTER_USE_OPEN62541 "Enable compilation of an example open62541 project" OFF)

conanfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class HelloConan(ConanFile):
99
settings = 'os', 'compiler', 'build_type', 'arch'
1010
generators = 'CMakeDeps', 'CMakeToolchain'
11-
default_options = {'fmt/*:header_only': True, 'spdlog/*:header_only': True, 'qt/*:with_fontconfig': False}
11+
default_options = {'fmt/*:header_only': True, 'spdlog/*:header_only': True, 'qt/*:with_fontconfig': False, 'open62541/*:cpp_compatible': True}
1212

1313
def configure(self):
1414
cmake = CMakeToolchain(self)
@@ -24,7 +24,7 @@ def configure(self):
2424
requirement = ['catch2/3.7.0', 'gtest/1.15.0', 'docopt.cpp/0.6.3',
2525
'spdlog/1.14.1', 'sml/1.1.11', 'nlohmann_json/3.11.3',
2626
'boost/1.83.0', 'crowcpp-crow/1.2.0', 'cppzmq/4.10.0',
27-
'protobuf/5.27.0']
27+
'protobuf/5.27.0', 'open62541/1.3.9']
2828
self.requires = conans.model.requires.Requirements(requirement)
2929

3030
def build(self):

src/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ IF(CPP_STARTER_USE_QT)
3636
ADD_SUBDIRECTORY(qt)
3737
ENDIF()
3838

39+
# open62541pp example
40+
IF(CPP_STARTER_USE_OPEN62541PP)
41+
MESSAGE("Using open62541pp")
42+
ADD_SUBDIRECTORY(open62541pp)
43+
ENDIF()
44+
45+
# open62541 example
46+
IF(CPP_STARTER_USE_OPEN62541)
47+
MESSAGE("Using open62541")
48+
ADD_SUBDIRECTORY(open62541)
49+
ENDIF()
50+
3951

4052
FIND_PACKAGE(docopt REQUIRED)
4153
FIND_PACKAGE(spdlog REQUIRED)

src/open62541/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ADD_EXECUTABLE(open62541_client client.cpp)
2+
TARGET_LINK_LIBRARIES(open62541_client PRIVATE open62541::open62541)
3+
4+
ADD_EXECUTABLE(open62541_server server.cpp)
5+
TARGET_LINK_LIBRARIES(open62541_server PRIVATE open62541::open62541)

src/open62541/client.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <open62541/client.h>
2+
#include <open62541/client_highlevel.h>
3+
#include <open62541/client_config_default.h>
4+
5+
int main()
6+
{
7+
/* Create a client and connect */
8+
UA_Client *client = UA_Client_new();
9+
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
10+
UA_StatusCode status = UA_Client_connect(client, "opc.tcp://localhost:4840");
11+
if (status != UA_STATUSCODE_GOOD) {
12+
UA_Client_delete(client);
13+
return status;
14+
}
15+
16+
/* Read the value attribute of the node. UA_Client_readValueAttribute is a
17+
* wrapper for the raw read service available as UA_Client_Service_read. */
18+
UA_Variant value;/* Variants can hold scalar values and arrays of any type */
19+
UA_Variant_init(&value);
20+
status = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), &value);
21+
if (status == UA_STATUSCODE_GOOD &&
22+
UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) { printf("the value is: %i\n", *static_cast<UA_Int32 *>(value.data)); }
23+
24+
/* Clean up */
25+
UA_Variant_clear(&value);
26+
UA_Client_delete(client);/* Disconnects the client internally */
27+
return status == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
28+
}

src/open62541/server.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <open62541/server.h>
2+
#include <open62541/server_config_default.h>
3+
4+
int main()
5+
{
6+
UA_Server *server = UA_Server_new();
7+
UA_Server_run_startup(server);
8+
9+
/* Should the server networklayer block (with a timeout) until a message
10+
arrives or should it return immediately? */
11+
const UA_Boolean waitInternal = true;
12+
while (true) { UA_Server_run_iterate(server, waitInternal); }
13+
14+
UA_Server_run_shutdown(server);
15+
UA_Server_delete(server);
16+
return 0;
17+
}

src/open62541pp/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
INCLUDE(FetchContent)
2+
3+
FETCHCONTENT_DECLARE(
4+
open62541pp
5+
GIT_REPOSITORY https://github.com/open62541pp/open62541pp.git
6+
GIT_TAG v0.15.0
7+
)
8+
FETCHCONTENT_MAKEAVAILABLE(open62541pp)
9+
10+
INCLUDE_DIRECTORIES(${open62541pp_SOURCE_DIR})
11+
12+
ADD_EXECUTABLE(open62541pp_client client.cpp)
13+
TARGET_LINK_LIBRARIES(open62541pp_client PRIVATE open62541pp::open62541pp)
14+
15+
ADD_EXECUTABLE(open62541pp_server server.cpp)
16+
TARGET_LINK_LIBRARIES(open62541pp_server PRIVATE open62541pp::open62541pp)

src/open62541pp/client.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
#include <open62541pp/open62541pp.h>
4+
5+
int main() {
6+
opcua::Client client;
7+
client.connect("opc.tcp://localhost:4840");
8+
9+
opcua::Node node = client.getNode(opcua::VariableId::Server_ServerStatus_CurrentTime);
10+
const auto dt = node.readValueScalar<opcua::DateTime>();
11+
12+
std::cout << "Server date (UTC): " << dt.format("%Y-%m-%d %H:%M:%S") << "\n";
13+
}

src/open62541pp/server.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <open62541pp/open62541pp.h>
2+
3+
int main() {
4+
opcua::Server server;
5+
6+
// Add a variable node to the Objects node
7+
opcua::Node parentNode = server.getObjectsNode();
8+
opcua::Node myIntegerNode = parentNode.addVariable({1, 1000}, "TheAnswer");
9+
// Write some node attributes
10+
myIntegerNode.writeDisplayName({"en-US", "The Answer"})
11+
.writeDataType(opcua::DataTypeId::Int32)
12+
.writeValueScalar(42);
13+
14+
server.run();
15+
}

0 commit comments

Comments
 (0)