UA Server SDK C++ Bundle  1.3.2.200
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
Lesson 1: Setting up a basic OPC UA Client console application

Content:

Utilities Used in this Lesson

The lesson uses callback.h from examples.

Step 1: Create New Project

Set up a console application.

Windows:
Create a new project. Use the following settings:

  • Win32 Console Application
  • No precompiled headers
  • Empty project.

Step 2: Add Files to the Project

Add [SDK Installation Directory]\examples\client_cpp_sdk\callback.h to your application.

Step 3: Add Include Directories

Add the following include paths to your application:

[SDK Installation Directory]\examples\client_cpp_sdk
[SDK Installation Directory]\include\uastack
[SDK Installation Directory]\include\uabase
[SDK Installation Directory]\include\uaclient
[SDK Installation Directory]\include\uapki
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\inc32

Linux:
[SDK Installation Directory]/examples/utilities/linux

For more information see LibraryOverview.

Step 4: Add Linker Settings

Windows:
For Additional Library Directories enter the following values:
[SDK Installation Directory]\lib
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\out32dll.dbg (Debug)
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\out32dll (Release)

For Additional Dependencies (Debug) enter:
uastackd.lib
uabased.lib
uapkid.lib
uaclientd.lib
crypt32.lib
libeay32d.lib
ws2_32.lib
rpcrt4.lib

For Additional Dependencies (Release) enter:
uastack.lib
uabase.lib
uapki.lib
uaclient.lib
crypt32.lib
libeay32.lib
ws2_32.lib
rpcrt4.lib

Linux:
For Additional Library Directories enter the following values:
-L[SDK Installation Directory]/lib

For Additional Dependencies (Debug) enter:
-luastackd -luaclientd -luabased -luapkid -lpthread -lrt -lssl

For Additional Dependencies (Release) enter:
-luastack -luaclient -luabase -luapki -lpthread -lrt -lssl

Step 5: Add Preprocessor Defines

Add

OPCUA_SUPPORT_SECURITYPOLICY_BASIC128RSA15=1
OPCUA_SUPPORT_SECURITYPOLICY_BASIC256=1
OPCUA_SUPPORT_SECURITYPOLICY_NONE=1
OPCUA_SUPPORT_PKI=1
_UA_STACK_USE_DLL

Additional defines for Windows:

UNICODE
_UNICODE
_CRT_SECURE_NO_WARNINGS
_CRT_SECURE_NO_DEPRECATE

Step 6: Set Output Path

Set output path to bin where the config file resides.

Windows:

Enter these values:

Output Directory
[SDK Installation Directory]\bin
Intermediate Directory
obj\$(ConfigurationName)

Step 7: Introducing class UaSession (part 1)

First of all we add the file client_cpp_sdk_tutorial.cpp and write down the following code snippet:

/* system */
#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#include <crtdbg.h>
#else
#include <signal.h>
volatile sig_atomic_t signal_received = 0;
/* Signal handler for CTRL-C. */
void SigInt(int)
{
signal_received = 1;
return;
}
#endif
/* C++ UA Client API */
#include "uaclientsdk.h"
using namespace UaClientSdk;
#include "uasession.h"
#include "callback.h"
#include "uaplatformlayer.h"
/* low level and stack */
#include "opcua_core.h"
/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;

The code shown above will be described in Lesson 2: Connecting to Server, so just use this code and learn more about it in the next lesson.

Step 8: Creating function WaitForKeypress

In the file client_cpp_sdk_tutorial.cpp we have to implement a function to shut down the client with a simple press on the key “x”. It is a simple function to handle the keypress event:

bool WaitForKeypress(int& action)
{
action = -1;
if(!kbhit())
{
}
else
{
switch ( getch() )
{
case 'x':
return true;
}
}
return false;
}

You do not have to use the key 'x', it is just a suggestion. Use can use every key you want.

Step 9: Implementing the main function

/*============================================================================
main
*===========================================================================*/
int main(int /*argc*/, char* /*argv*/[])
{
#ifndef WIN32
init_keyboard();
#endif
printf("\n*******************************************************\n");
printf("*******************************************************\n");
printf("** Press x to stop *******\n");
printf("*******************************************************\n");
printf("*******************************************************\n");
int action=-1;
/******************************************************************************/
/* Wait for user command to terminate the client thread. */
while(!WaitForKeypress(action))
{
Sleep(100);
}
#ifndef WIN32
close_keyboard();
#endif
return 0;
}

Main only calls the WaitForKeypress function if any key was pressed. That is all for now. Compile your first lesson and get familiar with the code. The complete client_cpp_sdk_tutorial.cpp;

/* system */
#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#include <crtdbg.h>
#else
#include <signal.h>
volatile sig_atomic_t signal_received = 0;
/* Signal handler for CTRL-C. */
void SigInt(int)
{
signal_received = 1;
return;
}
#endif
/* C++ UA Client API */
#include "uaclientsdk.h"
using namespace UaClientSdk;
#include "uasession.h"
#include "callback.h"
#include "uaplatformlayer.h"
/* low level and stack */
#include "opcua_core.h"
#ifdef WIN32
#define kbhit _kbhit
#define getch _getch
#else
#include <unistd.h>
void Sleep(int ms)
{
usleep(1000 * ms);
}
#include "kbhit.c"
#define getch readch
#endif
UaSession* g_pUaSession;
Callback* g_pCallback;
bool WaitForKeypress(int& action)
{
action = -1;
if(!kbhit())
{
}
else
{
switch ( getch() )
{
case 'x':
return true;
}
}
return false;
}
/*============================================================================
main
*===========================================================================*/
int main(int /*argc*/, char* /*argv*/[])
{
#ifndef WIN32
init_keyboard();
#endif
printf("\n*******************************************************\n");
printf("*******************************************************\n");
printf("** Press x to stop *******\n");
printf("*******************************************************\n");
printf("*******************************************************\n");
int action=-1;
/******************************************************************************/
/* Wait for user command to terminate the client thread. */
while(!WaitForKeypress(action))
{
Sleep(100);
}
#ifndef WIN32
close_keyboard();
#endif
return 0;
}