-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added standard examples for open62541(pp) #39
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ADD_EXECUTABLE(open62541_client client.cpp) | ||
TARGET_LINK_LIBRARIES(open62541_client PRIVATE open62541::open62541) | ||
|
||
ADD_EXECUTABLE(open62541_server server.cpp) | ||
TARGET_LINK_LIBRARIES(open62541_server PRIVATE open62541::open62541) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <open62541/client.h> | ||
#include <open62541/client_highlevel.h> | ||
#include <open62541/client_config_default.h> | ||
|
||
int main() | ||
{ | ||
/* Create a client and connect */ | ||
UA_Client *client = UA_Client_new(); | ||
UA_ClientConfig_setDefault(UA_Client_getConfig(client)); | ||
UA_StatusCode status = UA_Client_connect(client, "opc.tcp://localhost:4840"); | ||
if (status != UA_STATUSCODE_GOOD) { | ||
UA_Client_delete(client); | ||
return status; | ||
} | ||
|
||
/* Read the value attribute of the node. UA_Client_readValueAttribute is a | ||
* wrapper for the raw read service available as UA_Client_Service_read. */ | ||
UA_Variant value;/* Variants can hold scalar values and arrays of any type */ | ||
UA_Variant_init(&value); | ||
status = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), &value); | ||
if (status == UA_STATUSCODE_GOOD && | ||
UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) { printf("the value is: %i\n", *static_cast<UA_Int32 *>(value.data)); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really want to show the C API with C printf in the C++ example project? at least add a comment with explanation that this is the official C example and we might want to use (or write!) a C++ wrapper if possible. |
||
|
||
/* Clean up */ | ||
UA_Variant_clear(&value); | ||
UA_Client_delete(client);/* Disconnects the client internally */ | ||
return status == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <open62541/server.h> | ||
#include <open62541/server_config_default.h> | ||
|
||
int main() | ||
{ | ||
UA_Server *server = UA_Server_new(); | ||
UA_Server_run_startup(server); | ||
|
||
/* Should the server networklayer block (with a timeout) until a message | ||
arrives or should it return immediately? */ | ||
const UA_Boolean waitInternal = true; | ||
while (true) { UA_Server_run_iterate(server, waitInternal); } | ||
|
||
UA_Server_run_shutdown(server); | ||
UA_Server_delete(server); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
INCLUDE(FetchContent) | ||
|
||
FETCHCONTENT_DECLARE( | ||
open62541pp | ||
GIT_REPOSITORY https://github.com/open62541pp/open62541pp.git | ||
GIT_TAG v0.15.0 | ||
) | ||
FETCHCONTENT_MAKEAVAILABLE(open62541pp) | ||
|
||
INCLUDE_DIRECTORIES(${open62541pp_SOURCE_DIR}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use target_include_directories instead |
||
|
||
ADD_EXECUTABLE(open62541pp_client client.cpp) | ||
TARGET_LINK_LIBRARIES(open62541pp_client PRIVATE open62541pp::open62541pp) | ||
|
||
ADD_EXECUTABLE(open62541pp_server server.cpp) | ||
TARGET_LINK_LIBRARIES(open62541pp_server PRIVATE open62541pp::open62541pp) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
|
||
#include <open62541pp/open62541pp.h> | ||
|
||
int main() { | ||
opcua::Client client; | ||
client.connect("opc.tcp://localhost:4840"); | ||
|
||
opcua::Node node = client.getNode(opcua::VariableId::Server_ServerStatus_CurrentTime); | ||
const auto dt = node.readValueScalar<opcua::DateTime>(); | ||
|
||
std::cout << "Server date (UTC): " << dt.format("%Y-%m-%d %H:%M:%S") << "\n"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <open62541pp/open62541pp.h> | ||
|
||
int main() { | ||
opcua::Server server; | ||
|
||
// Add a variable node to the Objects node | ||
opcua::Node parentNode = server.getObjectsNode(); | ||
opcua::Node myIntegerNode = parentNode.addVariable({1, 1000}, "TheAnswer"); | ||
// Write some node attributes | ||
myIntegerNode.writeDisplayName({"en-US", "The Answer"}) | ||
.writeDataType(opcua::DataTypeId::Int32) | ||
.writeValueScalar(42); | ||
|
||
server.run(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting?