Code Monkey home page Code Monkey logo

open62541pp's Introduction

open62541++

License: MPL 2.0 CI Compatibility Package Documentation Coverage

C++ wrapper of the amazing open62541 OPC UA library.

Documentation โ€ข Examples

๐ŸŽฏ Features and goals

โœ Examples

Explore all examples in the examples/ directory.

Server

#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();
}

Client

#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") << std::endl;
}

โ‡† Type conversion

Type conversion from and to native UA_* types are handled by the opcua::TypeConverter struct. Have a look at the typeconversion example.

Compile-time checks are used where possible:

opcua::Variant var;

// will compile
int number = 5;
var.setScalar(number);
var.setArrayCopy<double>({1.1, 2.2, 3.3});

// won't compile, because the std::string can't be assigned without copy (conversion needed)
std::string str{"test"};
var.setScalar(str);

// will compile
var.setScalarCopy(str);

You can add template specializations to add conversions for arbitrary types:

namespace opcua {

template <>
struct TypeConverter<std::string> {
    using ValueType = std::string;
    using NativeType = UA_String;
    using ValidTypes = TypeIndexList<UA_TYPES_STRING, UA_TYPES_BYTESTRING, UA_TYPES_XMLELEMENT>;

    static void fromNative(const NativeType& src, ValueType& dst) { /* ... */ }
    static void toNative(const ValueType& src, NativeType& dst) { /* ... */ }
};

}  // namespace opcua

Type map of built-in types

Type Enum opcua::Type Type Typedef Wrapper Conversions
Boolean UA_Boolean bool
SByte UA_SByte int8_t
Byte UA_Byte uint8_t
Int16 UA_Int16 int16_t
UInt16 UA_UInt16 uint16_t
Int32 UA_Int32 int32_t
UInt32 UA_UInt32 uint32_t
Int64 UA_Int64 int64_t
UInt64 UA_UInt64 uint64_t
Float UA_Float float
Double UA_Double double
String UA_String opcua::String std::string, std::string_view, const char*, char[N]
DateTime UA_DateTime int64_t opcua::DateTime std::chrono::time_point
Guid UA_Guid opcua::Guid
ByteString UA_ByteString UA_String opcua::ByteString
XmlElement UA_XmlElement UA_String opcua::XmlElement
NodeId UA_NodeId opcua::NodeId
ExpandedNodeId UA_ExpandedNodeId opcua::ExpandedNodeId
StatusCode UA_StatusCode uint32_t opcua::StatusCode
QualifiedName UA_QualifiedName opcua::QualifiedName
LocalizedText UA_LocalizedText opcua::LocalizedText
ExtensionObject UA_ExtensionObject opcua::ExtensionObject
DataValue UA_DataValue opcua::DataValue
Variant UA_Variant opcua::Variant
DiagnosticInfo UA_DiagnosticInfo opcua::DiagnosticInfo

๐Ÿš€ Getting started

The library can be built, integrated and installed using CMake.

Please check out the open62541 build options here: https://www.open62541.org/doc/1.3/building.html#build-options

open62541++ provides additional build options:

  • UAPP_INTERNAL_OPEN62541: Use internal open62541 library if ON or search for installed open62541 library if OFF
  • UAPP_BUILD_DOCUMENTATION: Build documentation
  • UAPP_BUILD_EXAMPLES: Build examples for examples directory
  • UAPP_BUILD_TESTS: Build unit tests
  • UAPP_BUILD_TESTS_AUTORUN: Run unit tests after build
  • UAPP_ENABLE_CLANG_TIDY: Enable static code analysis with Clang-Tidy
  • UAPP_ENABLE_INCLUDE_WHAT_YOU_USE: Enable static code analysis with Include What You Use
  • UAPP_ENABLE_COVERAGE: Enable coverage analysis
  • UAPP_ENABLE_PCH: Use precompiled headers to speed up compilation
  • UAPP_ENABLE_SANITIZER_ADDRESS/LEAK/MEMORY/THREAD/UNDEFINED_BEHAVIOUR: Enable sanitizers

Integrate as an embedded (in-source) dependency

Add it to your project as a Git submodule (git submodule add https://github.com/open62541pp/open62541pp.git) and link it with CMake:

add_subdirectory(extern/open62541pp)  # the submodule directory
target_link_libraries(myexecutable PRIVATE open62541pp::open62541pp)

Integrate as a pre-compiled library

If you build and install this package to your system, a open62541ppConfig.cmake file will be generated and installed to your system. The installed library can be found and linked within CMake:

find_package(open62541pp::open62541pp CONFIG REQUIRED)
target_link_libraries(myexecutable PRIVATE open62541pp::open62541pp)

Build and install

# clone repository
git clone --recursive https://github.com/open62541pp/open62541pp.git
cd open62541pp

# build
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DUAPP_BUILD_EXAMPLES=ON -DUAPP_BUILD_TESTS=ON ..
cmake --build .

# run tests
ctest --output-on-failure

# generate coverage reports (text or html) with UAPP_ENABLE_COVERAGE option enabled
cmake --build . --target open62541pp_coverage_report
cmake --build . --target open62541pp_coverage_report_html

# install to system
cmake --install .

Dependencies

  • open62541 as integrated submodule or external dependency
  • doctest for tests as integrated submodule

๐Ÿค Contribute

Contributions and feature requests are very welcome. Please have a look at the contribution guidelines.

open62541pp's People

Contributors

lukasberbuer avatar johanneskopf avatar duca avatar eriktempelaarvo avatar ussaka avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.