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 Hello World Server example delivered with the Unified Automation C++ SDK in six simple steps.

Step 1: Adding Files to Visual Studio Solution

The Hello World Server example delivered with the C++ SDK used in this HowTo can be accessed via Start Menu (Examples → UA Hello World Server Project).

First, the source and header files generated in HowTo (Windows): Create a New Project With a Method have to be added to the Visual Studio project. Right click on the project and select Add → Existing Item. Then add all files starting with the prefix “newns_”.

Optional: Create NodeManagers for Additional Models

If additional models have been chosen in Step 3: Selecting the Base Models, it is necessary to create the corresponding NodeManagers. Please refer to the documentation of the C++ SDK for more information.

Step 2: Include Header File

The generated header files must be included in the file servermain.cpp. Add the marked lines:

...
#include "opcua_basedatavariabletype.h"
// New code begins
#include "newns_nodemanagernewnamespace.h"
#include "newns_myobjecttype.h
// New code ends
int OpcServerMain(const char* szAppPath)
{
int ret = 0;
...

Step 3: Creating NodeManager Instance

Now, we create an instance of the NewNs::NodeManagerNewNamespace in the server startup and add it to the Server’s Nodemanager. Add the marked code to servermain.cpp:

...
//- Start up OPC server ---------------------
// This code can be integrated into a startup
// sequence of the application where the
// OPC server should be integrated
//-------------------------------------------
// Create and initialize server object
OpcServer* pServer = new OpcServer;
pServer->setServerConfig(sConfigFileName, szAppPath);
// New code begins
NewNs::NodeManagerNewNamespace* pNM = new NewNs::NodeManagerNewNamespace(false);
pServer->addNodeManager(pNM);
// New code ends
// Start server object
...

Step 4: Creating an Object Instance

In the start method of the server the object must be instantiated and the node is added to the objects folder. After this step, the new object will be browseable and accessible in the server’s address space.

Add the following code to servermain.cpp:

...
// Start server object
ret = pServer->start();
if ( ret != 0 )
{
delete pServer;
return ret;
}
//-------------------------------------------
// New code begins
// Add an object instance to the objects folder
NewNs::MyObjectType* pObject = new NewNs::MyObjectType(
UaNodeId("MyObject", pNM->getNameSpaceIndex()), // NodeId
"MyObject", //Name
pNM->getNameSpaceIndex(), // Name space index
pNM); // Interface for adding nodes
pNM->addNodeAndReference(OpcUaId_ObjectsFolder, pObject,
OpcUaId_Organizes);
// New code ends
...

Step 5: Method Implementation (fktSum)

At last, the method has to be implemented in the file newns_myobjecttype.cpp.

/* MyObjectType method fktSum
*/
UaStatus MyObjectType::fktSum(const ServiceContext& serviceContext,
/*IN*/ OpcUa_UInt32 in1,
/*IN*/ OpcUa_UInt32 in2,
/*OUT*/ OpcUa_UInt32& out1)
{
OpcUa_ReferenceParameter(serviceContext);
out1 = in1 + in2; // Add this line
UaStatus ret;
return ret;
}

Step 6: Compile and Run

Now compile the project. It is also possible to compile it in 64Bit mode if all third-party files (like OpenSSL and LibXml2) are in 64Bit as well.

If the compilation succeeded, you should find compiled application server_hello_world in release version or server_hello_worldd in debug version in the folder <Installation Directory>\UaSdkCppxyz\bin. Just start the application and connect to the server with Unified Automation’s UaExpert.

expert1.png

To check if the generated code works, browse to the node MyObject and call the method fktSum.

expert2.png