The generated code itself is identical for Windows and Linux; hence code that was generated under Windows can be used in a Linux SDK and vice versa. The integration of the generated code into your application strongly depends on the build environment you are using, e.g. Visual Studio, Eclipse, CMake, etc. This example will describe how to integrate the generated code into Server Getting Started Lesson 1 of the Unified Automation ANSI C SDK 1.6 or newer using the CMake files delivered with the SDK in six simple steps.
Step 1: Adding the Generated Provider to the Server Application’s CMakeLists.txt
First of all, the generated provider is added to the server application’s CMakeLists.txt ([SDK Installation Directory]/examples/server_gettingstarted/lesson01/CMakeLists.txt). Add the following code:
...
set(PROVIDER_LIST ${PROVIDER_LIST} ${UASERVERPROVIDER_LIBRARY})
### New code begins ###########################################
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/exampleproject)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/exampleproject)
set(PROVIDER_LIST ${PROVIDER_LIST} exampleprojectprovider)
### New code ends #############################################
...
Step 2: Adding Code to the Server Application’s Source File
Then locate the source file of the application ([SDK Installation Directory]/examples/server_gettingstarted/lesson01/servermain.c) and include the generated provider.
...
#include <uaserver_module.h>
#include <uaserver_utilities.h>
#include <uaprovider_exampleproject.h>
...
For enabling the server to start up the provider, it has to be added to the server’s provider list.
OpcUa_StatusCode ServerMain()
{
UaServer uaServer;
UaServer_Configuration *pServerConfig = OpcUa_Null;
UaServer_Provider Provider;
...
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Server: Building Provider List...\n");
uStatus = UaServer_ProviderList_Create(&uaServer);
OpcUa_GotoErrorIfBad(uStatus);
memset( &Provider, 0, sizeof( Provider ) );
# ifndef BUILD_SHARED_LIBS
Provider.pfInit = UaProvider_ExampleProject_Initialize;
# else
Provider.sProviderLibrary = "exampleprojectprovider";
# endif
UaServer_ProviderList_AddProvider( &uaServer, &Provider )
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Server: Loading Provider Modules...\n");
uStatus = UaServer_Providers_Initialize(&uaServer);
OpcUa_GotoErrorIfBad(uStatus);
...
Step 3: Implementing the Example Method of the Generated Provider
Now, we implement the method in the generated file [SDK Installation Directory]/examples/server_gettingstarted/lesson01/exampleproject/ uaprovider_exampleproject_myobjecttype_methods.c as shown below.
OpcUa_StatusCode UaProvider_ExampleProject_MyObjectType_fktSum(
UaServer_ProviderCallContext *a_pCallContext ,
OpcUa_NodeId *pObjectId ,
OpcUa_UInt32 in1,
OpcUa_UInt32 in2,
OpcUa_UInt32 *out1)
{
OpcUa_StatusCode ret = OpcUa_Good;
OpcUa_ReferenceParameter(a_pCallContext);
OpcUa_ReferenceParameter(pObjectId);
OpcUa_UInt32_Initialize(out1);
*out1 = in1 + in2;
return ret;
}
Step 4: Compiling and Running the Server Application
After completing the previous steps, you can compile the example projects with the delivered script [SDK Installation Directory]/buildSdk.sh.
If everything went well, you can find the applications server_lesson01 and server_lesson01d in the folder [SDK Installation Directory]/bin.
Step 6: Connect to the Server Application via UaExpert
The last step is to check wheter the generated provider and its method are correctly implemented. Just connect to the server with Unified Automation’s UaExpert, browse to your generated object type (Root → Types → ObjectTypes → BaseObjectType → MyObjectType) and call the method.