Getting Started

Note

Content of this section is applicable for Windows only.

Prerequisites.

  • Install Microsoft Visual Studio 2017. Free Community edition is sufficient.

Note

Component Desktop development with C++ should be selected in Visual Studio Installer, and installed. Please also note that scripts which build dependencies, have path to the vcvarsall.bat file, which prepares environment to build C++ code, set for Community edition of Visual Studio (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build). If you have Enterprise or Professional version, please adjust accordingly path value in the variable vc_bat_name1 in the script file setup-environment.cmd (located in the SDK root folder).

  • Install Python 2.7 and add path to it into the Path environment variable.
  • Install nasm (download from http://www.nasm.us, and add path to it into the Path environment variable)
  • git should be availabe from Windows command line console, together with git shell sh.exe.

Installation of the SDK

  • Open Windows command line console.
  • Navigate to the source codes repository folder (referred as <RepoBaseFolder> further below).
  • Clone the SDK by running command git clone https://github.com/onewayautomation/1WaOpcUaSdk.git

Installation and building of dependencies.

  • Open Windows command line console.
  • Navigate to the folder where OPC UA SDK is cloned (by defualt <RepoBaseFolder>/1WaOpcUaSdk)
  • run the script install-opcuasdk-dependencies.cmd

Note

The script clones dependency libraries Boost and Botan, and builds them, which takes significant time (15-30 minutes depending on hardware).

After completion of steps descibed in this section, the SDK and dependency library files will be organized under the root folder as descibed in the next section. The SDK is ready to use after this point.

Folders structure.

  • <RepoBaseFolder> - root folder where all source code repositories are located.

    • 1WaOpcUaSdk - the folder with OPC UA C++ SDK files.

      • include - C++ header files.

      • docs - documentation folder (where local copy of this Developers Guide is located). Open file index.html to read it offline.

      • lib - folder with OPC UA SDK pre-built C++ libraries.

        • WindowsVC2017 - libraries for Windows, Visual Studio 2017.

          • x64 - 64 bit.
            • Debug - Debug mode
              • OpcUaSdk.lib - static library file.
            • Release - Release mode
              • OpcUaSdk.lib - static library file.
        • Ubuntu 18.04 LTS - this and other targets are planned in coming releases. Please contact us if you need binaries for other targets urgently. The SDK source code is cross-platform, and it can be built for wide range of targets, where C++14 is supported and dependency libraries can be built.

    • boost - Boost library files.

    • botan - Botan library files.

How to use the SDK: the first OPC UA project.

The best way to learn how to use is SDK is looking at code in sample projects, located in folder <RepoBaseFolder>/1WaOpcUaSdk/examples.

For new projects, following below settings are important. All directories are given relatively the folder with sample projects (2 levels deeper than repository root folder).

Include directories.

The following below paths should be included:

  • ../../1WaOpcUaSdk/include
  • ../../botan/install/$(Configuration)/include/botan-2 (here $(Configuration) is either Debug or Release).
  • ../../boost

Library directories.

The following below paths shoudl be included (where $(Configuration) is either Debug or Release):

  • ../../1WaOpcUaSdk/lib/WindowsVC2017/x64/$(Configuration)
  • ../../boost/stage/x64/lib
  • ../../botan/install/$(Configuration)/lib

Sample project HelloWorld.

This sample application connects to the public OPC UA server running at opc.tcp://opcuaserver.com:48010, reads value of the static variable of String type, and writes new value to it: either Hello World! or Hi there!, depending on what is current read value.

You can verify that new value is actually written, by using either OPC UA Web Client, or third party OPC UA client UA Expert.

To build and run the project, you can use Visual Studio 2017 solution examples.sln, which contains this project among with other sample projects.

Full source code of the project (content of file HelloWorld.cpp ) is quoted below.

Internals of the SDK are described in detail in following sections.

#include <opcua/Connection.h>
#include <iostream>

using namespace OWA::OpcUa;
int main (int argc, char** argv)
{
  (void)argc;
  (void)argv;
  const std::string value1 = "Hello World!";
  const std::string value2 = "Hi there!";
  bool succeeded = false;
  {
    auto connection = Connection::create("opc.tcp://opcuaserver.com:48010", true);
    if (connection->connect().get().isGood())
    {
      NodeId nodeId("Demo.Static.Scalar.String", 2);
      ReadRequest::Ptr readRequest(new ReadRequest(nodeId));
      auto readResponse = connection->send(readRequest).get();
      if (readResponse->isGood() && readResponse->results.size() == 1 && Utils::isGood(readResponse->results[0].statusCode))
      {
        // We know that data type of the value is String, therefore convert it to string should succeed:
        std::string currentValue = readResponse->results[0].value;
        std::string newValue = (currentValue == value1) ? value2 : value1;
        WriteRequest::Ptr writeRequest(new WriteRequest(WriteValue(nodeId, DataValue(Variant(newValue)))));
        auto writeResponse = connection->send(writeRequest).get();
        if (writeResponse->isGood() && Utils::isGood(writeResponse->results[0]))
        {
          readResponse = connection->send(readRequest).get();
          if (readResponse->isGood() && readResponse->results.size() == 1 && Utils::isGood(readResponse->results[0].statusCode))
          {
            currentValue = readResponse->results[0].value;
            if (currentValue == newValue)
            {
              std::cout << "Wrote value " << currentValue << " to the server!" << std::endl;
              succeeded = true;
            }
          }
        }
      }
    }
  }
  OWA::OpcUa::Utils::closeSdk();
  if (!succeeded)
  {
    std::cout << "Writing new value to the server failed!" << std::endl;
    return -1;
  }
  else
  {
    return 0;
  }
}