UaModeler  1.6.5.472
HowTo (Windows): Compile the Generated Method Project Files

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 the Visual Studio Solution for Lesson 1 of the Getting Started Tutorials for the ANSI C SDK 1.6.0 and newer in six simple steps.

Step 1: Adding a New vc(x)proj to the Visual Studio Solution

The first step is to add a new Visual Studio Project to the Visual Studio Solution for Lesson 1 of the Getting Started Tutorials delivered with the ANSI C SDK. Open the Solution File by selecting Examples → Getting Started Server → Lesson 1 from the start menu. Right click on the solution and select Add → New Project as shown in the screenshot below.

vs_c_1.png

Now select a new Win32 Project and set the location to the output directory selected in HowTo (Windows): Create a New Project With a Method Step 2: Selecting a Template and press the “OK” button.

vs_c_2.png

Continue the wizard to Application Settings, select Static library and uncheck Precompiled header. Confirm with the “Finish” button and return to the solution.

vs_c_3.png

Step 2: Adding the Generated Source Files to the Project

The next step is to add the source and header files (*.c and *.h) of the provider generated in HowTo (Windows): Create a New Project With a Method into the project created in the previous step: Right click on the project and select Add → Existing Item and select all generated source files in the output folder.

vs_c_4.png

Step 3: Adding Include Directories and Preprocessor Definitions

It is necessary to add include directories, preprocessor definitions and a build event to finish your project file. Select “Properties” from the context menu.

First of all, add the following include directories to your project file:

  • [Output Folder of UaModeler]
  • [SDK Installation Folder]\include\baselib
  • [SDK Installation Folder]\include\serverlib
  • [SDK Installation Folder]\include\uastack

In addition to that, you have to add the following preprocessor definitions to your project file:

WIN32;_UA_STACK_USE_DLL;HAVE_ENCODING_BINARY=1;HAVE_ENCODING_XML=0

Set the Output File name in the Librarian section of your project file to

$(OutDir)\exampleprojectproviderd.lib

The “d” depends to your configuration. Use this “d” only in debug mode of your Visual Studio configuration.

The last step in your new project file is to add a Post-Build Event to copy the compiled library into the lib folder. To do this, navigate to Build Events → Post-Build Event and add the following line:

copy /Y "$(TargetDir)" "[SDK Installation Folder]\lib"

This is all you have to do in the new project file.

Step 4: Adding the exampleprojectprovider Project to the Server Application

Next, select the server application’s project file (lesson01) and add the library exampleprojectproviderd.lib to LinkerAdditional Dependencies.

Finally, enter the output folder of UaModeler at Additional Include Directories.

Step 5: Adding Code to the Application and Implementing the Method

First of all you have to add the provider in servermain.c:

Include the header file:

...
#include <uaserver_module.h>
#include <uaserver_utilities.h>
#include <uaprovider_exampleproject.h> // Add this line
...

Add the provider to ServerMain():

OpcUa_StatusCode ServerMain()
{
UaServer uaServer;
UaServer_Configuration *pServerConfig = OpcUa_Null;
UaServer_Provider Provider; // Add this line
...
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Server: Building Provider List...\n");
uStatus = UaServer_ProviderList_Create(&uaServer);
OpcUa_GotoErrorIfBad(uStatus);
// New code begins
memset( &Provider, 0, sizeof( Provider ) );
# ifndef BUILD_SHARED_LIBS
Provider.pfInit = UaProvider_ExampleProject_Initialize;
# else
Provider.sProviderLibrary = "exampleprojectprovider";
# endif
UaServer_ProviderList_AddProvider( &uaServer, &Provider );
// New code ends
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Server: Loading Provider Modules...\n");
uStatus = UaServer_Providers_Initialize(&uaServer);
OpcUa_GotoErrorIfBad(uStatus);

In addition to that, the method has to be implemented in the generated file uaprovider_exampleproject_myobjecttype_methods.c as shown below:

...
{
OpcUa_StatusCode ret = OpcUa_Good;
OpcUa_ReferenceParameter(a_pCallContext);
OpcUa_ReferenceParameter(pObjectId);
OpcUa_UInt32_Initialize(out1);
*out1 = in1 + in2; // Add this line
return ret;
}

Step 6: Compile, Run and Connect to the Server

Now compile the whole solution and start up the application. You can see that the generated provider (ExampleProject Provider) was loaded and initialized, which means that this provider can be called and browsed in within any client.

vs_c_14.png

Now 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.

expert_linc_1.png
expert2.png