UA Server SDK C++ Bundle  1.3.2.200
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
Lesson 2: Connecting to Server

Content:

Step 1: Adding files

First of all we add client_cpp_sdk_tutorial.h from examples/lesson02 to our project in order to seperate code snippets from each other.

Step 2: Introducing class UaSession (part 2)

/* 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;

client_cpp_sdk_tutorial.h declares a pointer to UaSession. This class manages a UA client side application session. It is the main class for connecting to any OPC UA server. It provides all non-Subscription related Services. In addition the class manages the connection to an OPC UA server and the application session established with the server. Now it is not necessary to keep the code described above in your client_cpp_sdk_tutorial.cpp file. Just move it in the new file client_cpp_sdk_tutorial.h.

Step 3: Introducing class UaSessionCallback

. . .
/* C++ UA Client API */
#include "uaclientsdk.h"
using namespace UaClientSdk;
#include "uasession.h"
#include "uaplatformlayer.h"
/* low level and stack */
#include "opcua_core.h"
#include "callback.h"
/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;

g_pCallback is a pointer to Callback. Callback inherits from UaSessionCallback amongst others. UaSessionCallBack again defines the callback interface for UaSession. This interface must be implemented by the user of UaSession to receive connection status change callbacks from the Client SDK.

Step 4: Initializing UA stack

In main() we leave the initialization of the global pointers described above:

int main(int /*argc*/, char* /*argv*/[])
{
#ifndef WIN32
init_keyboard();
#endif
g_pUaSession = NULL;
g_pCallback = NULL;
// ++
UaStatus status;
// ++
// Initialize the UA Stack platform layer
// ++
UaString sUrl(SERVER_URL); // <-- Set the define "SERVER_URL" globally right over the main() function.
// ++
SessionSecurityInfo sessionSecurityInfo;

After this we initialize the platform layer of the UA stack. This has to be done before any stack or SDK function is called.

Step 5: Adding connect() and disconnect()

Add connect()

Add connect() to header and source file:

/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;
// ++
/* Connect */
void connect(UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo);
void connect(UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo)
{
UaStatus status;
printf("\n\n****************************************************************\n");
printf("** Try to connet to selected server\n");
if ( g_pUaSession )
{
disconnect();
}
g_pUaSession = new UaSession();
g_pCallback = new Callback;
SessionConnectInfo sessionConnectInfo;
sessionConnectInfo.sApplicationName = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sApplicationUri = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sProductUri = "Client_Cpp_SDK";
/*********************************************************************
Connect to OPC UA Server
**********************************************************************/
status = g_pUaSession->connect(
sUrl, // URL of the Endpoint - from discovery or config
sessionConnectInfo, // General settings for connection
sessionSecurityInfo, // Security settings
g_pCallback); // Callback interface
/*********************************************************************/
if ( status.isBad() )
{
delete g_pUaSession;
g_pUaSession = NULL;
printf("** Error: UaSession::connect failed [ret=%s] *********\n", status.toString().toUtf8());
printf("****************************************************************\n");
return;
}
else
{
printf("** Connected to Server %s ****\n", sUrl.toUtf8());
printf("****************************************************************\n");
}
}

Note that initialization of g_pUaSession has been moved from main() to connect().

UaSession::connect() in turn takes the server URL, general connection settings, security settings, and a pointer to the callback interface.

Add disconnect()

Now we add disconnect() to header and source file:

/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;
/* Connect / disconnect */
void connect(UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo);
// ++
void disconnect();
// ++
void disconnect()
{
printf("\n\n****************************************************************\n");
printf("** Disconnect from Server\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
ServiceSettings serviceSettings;
/*********************************************************************
Disconnect from OPC UA Server
**********************************************************************/
g_pUaSession->disconnect(
serviceSettings, // Use default settings
OpcUa_True); // Delete subscriptions
/*********************************************************************/
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
}

Step 6: Complete client_cpp_sdk_tutorial.cpp()

Finally we have to call the methods:

#include "client_cpp_sdk_tutorial.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
bool WaitForKeypress(int& action)
{
action = -1;
if(!kbhit())
{
}
else
{
switch ( getch() )
{
case 'x':
return true;
}
}
return false;
}
// ServerURL
#define SERVER_URL "opc.tcp://localhost:4841"
/*============================================================================
main
*===========================================================================*/
int main(int /*argc*/, char* /*argv*/[])
{
#ifndef WIN32
init_keyboard();
#endif
g_pUaSession = NULL;
g_pCallback = NULL;
UaStatus status;
// Initialize the UA Stack platform layer
UaString sUrl(SERVER_URL);
SessionSecurityInfo sessionSecurityInfo;
/*********************************************************************
Connect to OPC UA Server
**********************************************************************/
connect(sUrl, sessionSecurityInfo);
/*********************************************************************/
/*********************************************************************
Disconnect from OPC UA Server
**********************************************************************/
disconnect();
/*********************************************************************/
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;
}
/*============================================================================
connect - Connect to OPC UA Server
*===========================================================================*/
void connect(UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo)
{
UaStatus status;
printf("\n\n****************************************************************\n");
printf("** Try to connet to selected server\n");
if ( g_pUaSession )
{
disconnect();
}
g_pUaSession = new UaSession();
g_pCallback = new Callback;
SessionConnectInfo sessionConnectInfo;
sessionConnectInfo.sApplicationName = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sApplicationUri = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sProductUri = "Client_Cpp_SDK";
/*********************************************************************
Connect to OPC UA Server
**********************************************************************/
status = g_pUaSession->connect(
sUrl, // URL of the Endpoint - from discovery or config
sessionConnectInfo, // General settings for connection
sessionSecurityInfo, // Security settings
g_pCallback); // Callback interface
/*********************************************************************/
if ( status.isBad() )
{
delete g_pUaSession;
g_pUaSession = NULL;
printf("** Error: UaSession::connect failed [ret=%s] *********\n", status.toString().toUtf8());
printf("****************************************************************\n");
return;
}
else
{
printf("** Connected to Server %s ****\n", sUrl.toUtf8());
printf("****************************************************************\n");
}
}
/*============================================================================
disconnect - Disconnect from OPC UA Server
*===========================================================================*/
void disconnect()
{
printf("\n\n****************************************************************\n");
printf("** Disconnect from Server\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
ServiceSettings serviceSettings;
/*********************************************************************
Disconnect from OPC UA Server
**********************************************************************/
g_pUaSession->disconnect(
serviceSettings, // Use default settings
OpcUa_True); // Delete subscriptions
/*********************************************************************/
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
}