4Creating programs with C++

With PLCnext\ Technology, you can also use programs created with C++ in the real-time context of a PLC along with classic IEC-61131-3 programs.

To use programs and program parts created in C++ within the scope of the PLCnext\ Technology, you need a software development kit (SDK).

Recommended: Use the freely available Eclipse® software with Phoenix Contact add-in for Eclipse® as a development environment for C++ programming. The add-in enables the easy connection to the PLCnext\ Technology platform.

To use C++ programs in the same way as IEC 61131-3 applications in PC Worx Engineer, the PLCnext\ Technology LibraryBuilder is required.

If you quickly create a program with Eclipse® and want to make it available by using the Eclipse® add-in for PC Worx Engineer, read this section.

4.1Installing PLCnext\ Technology SDK and add-in for Eclipse®

4.1.1Requirements

inset_6.png 

 

An Internet connection is required for installing the components.

inset_22.png 

 

A description of the PLCnext\ Technology SDK and add-in for Eclipse® under Linux is available in the PLCnext-Community under plcnext-community.net.

Before starting the installation, ensure that the system requirements are met and down­load the necessary software.

Operating system

Microsoft® Windows® 7 or

Microsoft® Windows® 10

Phoenix Contact software

PLCnext\ Technology SDK:

The SDK contains all of the important tool chains and libraries required for creating a program.

Phoenix Contact add-in for Eclipse®:

The add-in for Eclipse® supports you during the creation of metafiles as well as IN and OUT ports.

EngineeringLibraryBuilder:

The LibraryBuilder generates a library from the C++ program which can be read by PC Worx Engineer via metadata and shared object. The C++ program can be used in the same way as an IEC 61131-3 program in PC Worx Engineer.

Software packages required for programming in C++ with Eclipse are to be found in the download area of the AXC F 2152 controller at phoenixcontact.net/products (order no.: 2404267)in the following categories:

“Configuration software”: You will find the Phoenix Contact add-in for the Eclipse® de­velopment environment here.

“Software”: You will find the Phoenix Contact LibraryBuilder for creating a library from Eclipse® for use in PC Worx Engineer as well as the Phoenix Contact SDK (Software Development Kit) here.

Some sample applications for the AXC F 2152, programmed in C++, are available in the “Sample applications” category for downloading.

C++ development environment

Eclipse® Neon 3 / Eclipse IDE for C/C++ Developers

Download the Eclipse Neon 3 software with the package “Eclipse IDE for C/C++ Devel­opers” from https://www.eclipse.org.

Java Runtime Environment

Oracle® Java™ Runtime Environment

Download the Java Runtime Environment from https://java.com/de.

.NET Framework

Microsoft® .NET Framework 4.6.1

Download the .NET Framework from https://www.microsoft.com.

inset_24.png 

 

If you use PC Worx Engineer 7.x, the .Net Framework is already installed.

4.1.2Installation

Installing the Java™ Run­time Environment

The Java™ Runtime Environment is required for the Eclipse® development environment.

Install the Java™ Runtime Environment.

Follow the instructions of the installation wizard.

Installing the
.NET Framework

The .Net Framework 4.6.1 is required for the PLCnext\ Technology LibraryBuilder. If you use PC Worx Engineer, .Net Framework is already installed on your system.

Install the .Net Framework.

Follow the instructions of the installation wizard.

Installing and configuring
Eclipse®

A development environment is required for programming in C++. Phoenix Contact recom­mends Eclipse® Neon 3. The Phoenix Contact add-in available for Eclipse® enables the easy use of C++ programs in the PLCnext\ Technology context.

Execute the installation file for the Eclipse® Neon 3 software.

In the dialog that opens, select the integrated development environment for C/C++ de­velopers (Eclipse IDE for C/C++ Developers).

PLCnext_Eclipse_Installation_Cppplugin.png

Figure 4-1 Selecting IDE for C/C++ Developers

Then install the development environment.

Follow the instructions of the installation wizard.

Installing the
Phoenix Contact SDK

Unzip the downloaded SDK file into a directory of your choice. Please note that the di­rectory name may not contain spaces.

After unzipping, ensure that the directory contains all files displayed in Figure 4-2.

PLCnext_SDK_extracted.png

Figure 4-2 Unzipped Phoenix Contact SDK directory

The SDK is accessed during the creation of a new C++ project (see 4.2 “Creating a new project”).

Updating the SDK

Recommended:

Always use the latest version of the SDK. Uninstall earlier SDK versions. This is the only way to ensure that the various versions are not mixed.

Installing the Phoenix Contact add-in for Eclipse®

Unzip the add-in.

Open the Eclipse® software.

In Eclipse®, open the menu “Help, Install New Software”.

Select the path to the directory of the add-in. To do so, click on “Add” in the dialog that opens.

In the dialog “Add Repository”, click on “Local” and select the directory of the add-in.

PLCnext_Eclipse_Plugin_plugin_auswählen.png

Figure 4-3 Selecting the path to the add-in directory

In the next dialog, select the components displayed in Figure 4-4.

PLCnext_Eclipse_Plugin_plugin_items_auswählen.png

Figure 4-4 Selecting Phoenix Contact add-in components

Click on the “Next” button.

Read and accept the licence agreements.

Click “Finish” to complete the installation.

Restart Eclipse®.

Performing the add-in up­date for Eclipse®

To perform any necessary updates, proceed as follows:

Update the Phoenix Contact add-in for Eclipse® by proceeding as for the initial instal­lation.

You can also copy the update file to the existing directory. Start the update as follows:

In Eclipse®, open the menu “Help, Check for Updates” or “Help, Install New Software”.

Installing the
LibraryBuilder

Unzip the downloaded LibraryBuilder file into a directory of your choice. Please note that the directory name may not contain spaces.

Now integrate the LibraryBuilder into Eclipse®. This is necessary to import C++ programs. as libraries in C++ into PC Worx Engineer.

In Eclipse®, open the menu “Window, Preferences”.

Then select “PLCnext”.

Click on “PCWLX Export”.

Click on the “Browse” button.

Select the path to the LibraryBuilder.

Click on the “Apply” button to apply the path.

PLCnext_Eclipse_Installation_Library_Builder_auswählen.png

Figure 4-5 Selecting the LibraryBuilder

Then click on “OK”.

4.2Creating a new project

This section describes how to create a new C++ project in Eclipse®.

Open the Eclipse® software.

Open the menu “File, New, C++ Project”.

PLCnext_Eclipse_neuesCpp_Projekt_erstellen.png

Figure 4-6 Creating a new C++ project

Enter a project name in the “Project name:” input field.

In the “Project type” field, select a project type (In the example in Figure 4-7 “ Entering a project name and selecting a project type”).

PLCnext_Eclipse_neuesCppProjekt_Einstellungen.png

 

Figure 4-7 Entering a project name and selecting a project type

Click on the “Next” button.

If necessary, adapt the basic settings of the C++ project.

PLCnext_Eclipse_neuesCppProjekt_ProgramComponentLibrary.png

Figure 4-8 Basic settings of the C++ project

Click on the “Next” button.

In the “Configurations” area, activate the “Debug” and “Release” check boxes:

PLCnext_Eclipse_neuesCppProjekt_Konfig_auswählen.png

Figure 4-9 Activating the “Debug” and “Release” check boxes

Click on the “Browse” button.

Select the path to the directory “sysroots” of the unzipped Phoenix Contact SDK.

Confirm your selection with “OK”.

Click the “Finish” button to finish creating the project.

PLCnext_Eclipse_neuesCppProjekt_sysroots.png

Figure 4-10 Selecting the path to the “sysroots” directory

To start creating the C++ program, now switch to the “Workbench” view.

PLCnext_Eclipse_neuesCppProjekt_Workbench.png

Figure 4-11 “Workbench” view in Eclipse®

4.3Creating a program

To create a C++ program using Eclipse® which can be imported into PC Worx Engineer, you must first prepare a new project in accordance with the description in section Creating a new project.

Ensure that the correct path to the “sysroots” directory of the unzipped Phoenix Contact SDK is set.

The created project has a defined structure in which the C++ program is created. Figure 4-12 shows an example.

PLCnext_Project_Explorer_CPP_Counter.png

Figure 4-12 Sample structure of a C++ project

Creating variables

The header file of your program (e.g. CPP_Counter_p.hpp) must contain the variable defi­nition.

Define the variables required for the C++ program as well as their initial values here. Some of the variables are used as IN or OUT ports. You will find further information on the IN and OUT ports and GDS in section GDS (Global Data Space).

Create the variables necessary for your project.

PLCnext_Headerdatei_Variablen.png

Figure 4-13 Sample variable declaration wit initial values in the header file

Save the changes to the header file.

Adding IN and OUT ports

Now register the IN and OUT ports for the GDS of the PLCnext\ Technology firmware.
To do so, proceed as follows:

Switch from the header file (*.hpp) to the source file (*.cpp).

Right-click in the line behind the comment “//TODO AddPortInfo”.

PLCnext_cpp_AddPortInfo.png

Figure 4-14 Inserting IN and OUT ports in the source file (*.cpp) (1)

In the context menu that opens, select the entry “PLCnext, Insert Program Ports at po­sition”.

PLCnext_Insert_Program_Ports_at_position.png

Figure 4-15 Inserting IN and OUT ports in the source file (*.cpp) (2)

The add-in now generates a list of all available variables from the header file. You can define all variables of the list as IN or OUT ports.

PLCnext_Portliste_generieren.png

Figure 4-16 List of all available variables is being created

From the list, select the variables that you wan to use as IN or OUT ports.

PLCnext_Ports_auswählen_boolean.png

Figure 4-17 Example: List of all available variables

Confirm your selection with “OK”.

The IN and OUT ports are created at the respective location in the source file with the re­quired syntax. You can furnish these with individual comments if necessary (e.g. “//Inports”).

PLCnext_Ports_in_cpp.png

Figure 4-18 Sample IN and OUT ports in the source file *.cpp

The definition of IN and OUT ports consists of two parameters:

Parameter 1: Name of the IN port or OUT port. The name is used to create the refer­ence to the GDS configuration and to the *.progmeta file. The name must match in all three positions. However, the name can deviate from the variable name (see Notes on designating GDS ports).

Parameter 2: Reference to the variable to be used as port. The data direction (IN or OUT port) is determined in the *.progmeta file.

Save the changes to the source file.

Generating the
*.progmeta file

If you defined the variables as IN and OUT ports in the source file, you have to add and con­figure the IN and OUT ports in the *.progmeta file. You can also perform this task with the add-in. Generate the IN and OUT ports in the *.progmeta file using the Phoenix Contact add-in for Eclipse®. The add-in draws the required data from the existing *.progmeta file and extends it with the found call ups of “AddPortInfo()” in the *.cpp source file. For newly added ports, the data direction has to be added (see Figure 4-20). The file is read in by both the firmware and PC Worx Engineer. Both need the data direction, name and data type.

To generate the file, proceed as follows:

Right-click anywhere in the open editor of the *.cpp file (* = file name).

In the context menu that opens, select the entry “PLCnext, Generate .progmeta Meta­config”.

PLCnext_Generate_.progmeta_Metaconfig.png

Figure 4-19 Generating the *.progmeta file

The *.progmeta file is being generated. Now define whether the generated ports are IN or OUT ports (INTERNAL ports are intended for a later extension).

Open the newly generated *.progmeta file.

Open the “Design” tab.

In the “Node” column, navigate to the “Ports” entry.

In the “Content” column, right-click to open the context menu in the “kind” line of a port.

Choose the type of the desired port.

Repeat this procedure for all IN and OUT ports used.

The add-in enters the data type in the “type” line.

Inspect the data type in the “type” input field.

PLCnext_progmeta_Design.png

Figure 4-20 Example *.progmeta file in the “Design” tab

All settings made in the “Design” tab are applied to the source text of the *.progmeta file. You can also edit directly in the source text.

Switch to the “Source” tab to view the settings in the source code of the *.progmeta file.

PLCnext_CPP_Counter_progmeta.png

Figure 4-21 Sample settings in the source code of the *.progmeta file on the tab “Source”

Creating a C++ program

Open the *.cpp source file.

Program the code to be executed in each ESM task pass in the “Execute()” function (see “//TODO implement program”).

After the instantiation of a program, the assigned ESM task calls up the “Execute()” function of the program instance in each cycle.

inset_35.png 

 

Sample projects and the associated documentation are available for downloading on the AXC F 2152 controller page (order no.: 2404267): phoenixcontact.net/products.

Compiling a project

Switch to the “Project Explorer” to compile the project.

PLCnext_Projektexplorer_CPP_Counter.png

Figure 4-22 Project folder in the “Project Explorer”

Right-click to open the context menu for the project folder (in the example: “CP­P_Counter”).

In the context menu, click on “Build Project”.

To view whether the compilation of the project was successful or to view possible error mes­sages, switch to the “Console” tab.

PLCnext_Build_Project_Console.png

Figure 4-23 Example: “Console”

If the compilation was successful, a shared object (*.so) is generated. You will find this in the “Release” folder of the Project Explorer.

PLCnext_Shared_Object.png

Figure 4-24 Example: Shared object (*.so) in the Project Explorer

Generating a PC Worx Engineer library

In order that you can use the C++ project in PC Worx Engineer, you must create a library from the C++ project. This library can then be imported into PC Worx Engineer. To generate a library from the C++ project, proceed as follows:

Right-click to open the context menu for the project folder (in the example:
“CPP_Counter”).

Select “PLCnext, Export PCWLX”.

PLCnext_Export_PCWLX.png

Figure 4-25 Generating a PC Worx Engineer library

Click on the “Browse” button.

Select the path to the directory to which the *.pcwlx library is to be stored.

Recommended: Standard path for storing PC Worx Engineer:
C:\Users\Public\Documents\PC Worx Engineer\Libraries

PLCnext_Library_speichern.png

Figure 4-26 Selecting a memory path for the *.pcwlx library to be generated

Click on the “Finish” button to generate the *.pcwlx library.

4.4Importing generated libraries into PC Worx Engineer

inset_36.png 

 

You will find additional information on PC Worx Engineer in the online help and the quick start guide (PC WORX ENGINEER 7, order no.: 1046008).

Once you have generated a library from a C++ program, import this library into the PC Worx Engineer software. Imported libraries are handled and processed in tasks in the same way as IEC 61131-3 programs in PC Worx Engineer.

To import a library into PC Worx Engineer, proceed as follows:

Open your PC Worx Engineer project or create a new AXC F 2152 template.

In the “COMPONENTS” section, click on “References”.

Right-click on “Libraries”.

Adding a library

Right-click to open the “Add Library” context menu.

PLCnext_PCWE_Add_Library.png

Figure 4-27 “Add library” context menu

In the dialog that opens, select the desired *.pcwlx library.

Click on “Open”.

If you have created the *.pcwlx library in Linux PC Worx Engineer converts this into the Win­dows® format. You have the option of creating a backup copy of the Linux version.

Components and programs are now available in the section “COMPONENTS” under “Ref­erences”.

PLCnext_PCWE_Added_Library.png

Figure 4-28 PC Worx Engineer – Added library

Instantiating a program

To instantiate the program contained in the library, proceed as follows:

In the section “PLANT”, select the node “PLCnext”.

Open the “Tasks and Events” editor.

PLCnext_PCWE_Tasks_and_Events.png

Figure 4-29 Opening the “Tasks and Events” editor

In the editor, create a new task or open an existing task.

Drag and drop the library from the section “COMPONENTS, References” to the “ES­MX” column of the task. This way you generate a program instance.

PLCnext_PCWE_Bsp._Programminstanz.png

Figure 4-30 Example: Program instance CPP_Counter_P1 in the “Task and Event” ed­itor

Assigning IN/OUT ports

In the section “PLANT”, select the node “PLCnext”.

Open the “Data List” editor.

In the “Data List” editor, all IN and OUT ports saved to the GDS of the controller are dis­played. The IN and OUT ports of the newly instantiated program are displayed. To use the IN/OUT ports of the imported library, you must assign the IN and OUT ports of the imported libraries to the IN and OUT ports of other program instances so that consistent data ex­change can take place.

Assigning an IN port to an OUT port

In order to assign an IN port to an OUT port, click on “Select IN Port here” in the “IN Port” column.

The role picker opens. Only the IN ports that you can actually assign to the respective OUT port are displayed in the role picker.

Select the IN port that you want to assign to the relevant OUT port in the role picker.

The IN port is assigned to the OUT port.

Proceed as described for other IN ports.

Assigning several IN ports to an OUT port

Assign an IN port to an OUT port as described in the previous section.

To assign further IN ports to an OUT port, duplicate it.

Right-click on the OUT port and select the option “Duplicate Out port” in the context menu.

PCWE_duplicatePort.png

Figure 4-31 Duplicating an output port

To assign an IN port to the duplicated OUT port, proceed as described in the previous section.

Assigning an OUT port to an IN port

In order to assign an OUT port to an IN port, click on “Select OUT Port here” in the “OUT Port” column.

The role picker opens. Only the OUT ports that you can actually assign to the respective IN port are displayed in the role picker.

Select the OUT port that you want to assign to the relevant IN port in the role picker.

The OUT port is assigned to the IN port.

Proceed as described for other OUT ports.

Observe the instructions in section 2.6.2 “Supported data types”.

PLCnext_PCWE_Data_list.png

Figure 4-32 “Data List” editor

Once you have assigned all IN and OUT ports to be used, transfer the PC Worx Engineer project to the controller. Additional information on PC Worx Engineer is available in the on­line help, the quick start guide (PC WORX ENGINEER 7, order no.: 1046008) and the user manual for the AXC F 2152 controller (order no.: 2404267).

4.5Remote debugging

PLCnext\ Technology has an integrated GDB server. You have the option of setting up a re­mote debugging session using Eclipse®.

A description on how to set up a remote debugging session for a running process is avail­able in the PLCnext Community.

4.6Sample programs

Several example programs created with Eclipse® and the Phoenix Contact add-in for Eclipse® are available in the download area of the AXC F 2152 controller under the address phoenixcontact.net/products (Order No.: 2404267). The examples illustrate aspects of pro­gramming with C++ within the framework of the PLCnext\ Technology that may be of help to you when creating your own programs.