Functions and function block libraries written in C++
This topic describes the general approach to program functions and function block libraries in C++ for use with iCube Engineer.
This feature is based on Shared Native Libraries and is a combination of C++ implementation and C# wrapper. The iCube Engineer Toolchain supports Shared Native Libraries with a Microsoft® Visual Studio® IDE template, or on the command line via the PLCnext CLI.
Creating a iCube Shared Native Firmware Library
-
Create a new project
- On the Visual Studio select Create a new project
-
Type in search
iCube Engineer
and double-click the
iCube Shared Native Firmware Library.
- Type in your Project Name, select a storage location and click
Create.
-
In the next dialog window, type in a C++ project namespace and select at least one installed SDK for which the C++ part will be build. This setting can be changed later in the project properties.
- Click OK to add the Native Function or Native Function Block. This procedure can take a bit longer when executed for the first time.
-
Add new code sheets
With the Shared Native Library extension, the iCube Native Function and iCube Native Function Block code sheet templates are available. To use a template, proceed as follows:
- In the Solution Explorer, right-click on your project.
-
Select
Add
→
New Item….
-
In the left tree, select
C# Items
→
iCube Engineer
and then one of the templates:
-
Native Function
Template for a function to be used in IEC 61131-3 code.
Select a return value in the popped-up dialog:
Return Type:
For information on the data types in C# programming and their correlations to data types in IEC 61131 or C++ programming, you can pop up the table of supported data types in an overlay window.
Kind of Return Value:
By Reference is recommended for complex data types and is mandatory for strings, arrays and structures.
-
Native Function Block
Template for a function block to be used in IEC 61131-3 code. -
Name your function or function block properly (renaming isn't easy!) and press
Add.
↪ A default code sheet for the selected type of element is created, and the corresponding C++ header and a code sheet are generated automatically.
-
Define the interface
The interface for your functions and function blocks will be defined in C# and then generated into some template files. These templates will be used in the C++ project for implementation and building a iCube Engineer library file.
- In the C# project, open the .cs file you just created.
- Add the inputs and outputs to the public class like in normal iCube Engineer C# projects.
- To set the Visual Studio® build output to normal for getting get necessary information, go to Tools → Options... → Projects and Solutions → Build And Run , and change MSBuild project build output verbosity to Normal . That is, because the toolchain does not automatically add the changes to the C++ project to avoid issues with a potential implementation, and this way you're getting a list of files to check afterwards.
- To make the variables available in C++, right-click on the C# project in the Solution Explorer and select iCube Engineer → Generate .
- Open the new generated <projectname>-template3.h and <projectname>-template64.h header files located in the C# project in the CLI folder and compare them to the <projectname>-cli32.h and <projectname>-cli64.h in the C++ project.
-
Add the differences from the template to the CLI file.
Note: Usually, comments do not matter but leaving them out will result in the above message on each build. So better add the comments, too. Even if you have done all implementation, the last message will still appear, but should never be just ignored on any interface changes.
-
Create the code
- Open the new C++ project code sheet <function or function block name>-cli.cpp that has been added during code generation.
-
Create the code for the function or function block and use the port variables; e.g.:
-
Create the Shared Native Library and iCube Engineer Library by building the C++ project.
↪ A *.pcwlx library is being stored in bin directory of the C++ project.
Proceed with importing the library into iCube Engineer to use your function or function block in an automation project.