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)
#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#include <crtdbg.h>
#else
#include <signal.h>
volatile sig_atomic_t signal_received = 0;
void SigInt(int)
{
signal_received = 1;
return;
}
#endif
#include "uaclientsdk.h"
using namespace UaClientSdk;
#include "uasession.h"
#include "callback.h"
#include "uaplatformlayer.h"
#include "opcua_core.h"
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
...
#include "uaclientsdk.h"
using namespace UaClientSdk;
#include "uasession.h"
#include "uaplatformlayer.h"
#include "opcua_core.h"
#include "callback.h"
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 , char* [])
{
#ifndef WIN32
init_keyboard();
#endif
g_pUaSession = NULL;
g_pCallback = NULL;
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:
Callback* g_pCallback;
void connect(
UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo);
void connect(
UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo)
{
printf("\n\n****************************************************************\n");
printf("** Try to connet to selected server\n");
if ( g_pUaSession )
{
disconnect();
}
g_pCallback = new Callback;
SessionConnectInfo sessionConnectInfo;
sessionConnectInfo.sApplicationName = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sApplicationUri = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sProductUri = "Client_Cpp_SDK";
status = g_pUaSession->connect(
sUrl,
sessionConnectInfo,
sessionSecurityInfo,
g_pCallback);
{
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:
Callback* g_pCallback;
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;
g_pUaSession->disconnect(
serviceSettings,
OpcUa_True);
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;
}
#define SERVER_URL "opc.tcp://localhost:4841"
int main(int , char* [])
{
#ifndef WIN32
init_keyboard();
#endif
g_pUaSession = NULL;
g_pCallback = NULL;
SessionSecurityInfo sessionSecurityInfo;
connect(sUrl, sessionSecurityInfo);
disconnect();
printf("\n*******************************************************\n");
printf("*******************************************************\n");
printf("** Press x to stop *******\n");
printf("*******************************************************\n");
printf("*******************************************************\n");
int action=-1;
while(!WaitForKeypress(action))
{
Sleep(100);
}
#ifndef WIN32
close_keyboard();
#endif
return 0;
}
void connect(
UaString& sUrl, SessionSecurityInfo& sessionSecurityInfo)
{
printf("\n\n****************************************************************\n");
printf("** Try to connet to selected server\n");
if ( g_pUaSession )
{
disconnect();
}
g_pCallback = new Callback;
SessionConnectInfo sessionConnectInfo;
sessionConnectInfo.sApplicationName = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sApplicationUri = "Client_Cpp_SDK@myComputer";
sessionConnectInfo.sProductUri = "Client_Cpp_SDK";
status = g_pUaSession->connect(
sUrl,
sessionConnectInfo,
sessionSecurityInfo,
g_pCallback);
{
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");
}
}
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;
g_pUaSession->disconnect(
serviceSettings,
OpcUa_True);
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
}