Working with Visual Studio®

This topic describes the general approach to C++ programming on PLCnext Technology with Microsoft®  Visual Studio® IDE using the C++ PLCnext Technology Extension from Phoenix Contact.

Of course, you might use also the Eclipse® IDE, or any other IDE or code editor instead.

Prerequisites

 

Creating a C++ project from a PLCnext template 

  • Start a new project either via the Startup Wizard, or use the menu: File  →  New  →  Project...
  • Search for plcnext in the dialog box to show only the PLCnext project templates
  • Select the project template that fits your needs; then click Next
    Create new Project
  • In the next dialog box, configure your basic project data, then click Create.
    Project Configuration
  • In the pop-up dialog, select the target device(s) and firmware version(s) for the project, then click OK.
    PLCnext Project Configuration

If several SDKs are installed on your computer, you can select multiple targets and firmware versions to be supported by the project. The PLCnext CLI will build your project for all of them automatically and put them into a single PLCnext Engineer library.

Note: The Includes for the selected targets are sorted by lowest build number first, and then regarding alphabetical order. This only affects IntelliSense to provide the smallest common feature set of all setup project targets.

To create a program, proceed as follows:

  • Open the *.cpp source file.
  • Program the code to be executed during each ESM task cycle in the Execute() function (see this comment: //TODO implement program).

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

For additional information on programming in C++, please refer to the other C++ topics on this platform. Example projects and their associated documentation can be downloaded at github.com/PLCnext/CppExamples.

 

Creating a C++ program

To create a C++ program which can be imported as a library into PLCnext Engineer, you must first prepare a new project in accordance with the description above.

The new project has a defined structure in which the C++ program is created as shown in this example:

Project tree in solution explrorer

To create a program, proceed as follows:

  • Open the *.cpp source file.
  • Program the code to be executed during each ESM task cycle in the Execute() function (see this comment: //TODO implement program).

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

For additional information on programming in C++, please refer to the other C++ topics on this platform. Example projects and their associated documentation can be downloaded at github.com/PLCnext/CppExamples.

 

Adding or removing components and programs

Here's how to add or remove components or programs properly in a C++ project for PLCnext Technology when working with Visual Studio®.

Adding a component

  • Right-click on the project.
  • Select Add  →  New Item... from the context menu.
  • In the pop-up dialog box, search for plcnext in the search field on the upper right side:
    New component
  • Select the PLCnext Component item.
  • Give the component a name and click Add.
  • In the next step, select a namespace; by default it is the project namespace.

The component is added to your C++ project. 

Adding a program

  • Right-click on the project.
  • Select Add  →  New Item... from the context menu.
  • In the pop-up dialog box, search for plcnext in the search field on the upper right side:
    New PLM program
  • Select the PLCnext PLM Program item.
  • Give the component a name and click Add.
  • In the next step, select a Parent Component to use, and a namespace;  by default it is the project namespace.
    New Parent Component

The program is added to your C++ project.

Removing a component

Deleting a C++ component from your project requires three steps in this order:

  1. Delete the corresponding source file and header file of your component. Make sure to permanently remove them.

  2. Delete all programs assigned to this component, or reassign them to another existing component (see here).

  3. Remove all intermediate files from previous builds. For this, simply right-click on your project and select Clean.
    Clean project from intermediate files

The component is properly removed from your C++ project.

Removing a program

To delete a program, simply delete the corresponding source file and header file.

  • Select the two files, right-click on the selection, and choose Remove from the context menu (or simply press Del)
  • In the dialog box, click Delete to delete the files permanently from the file system.
    Delete program

The program is properly removed from your C++ project.

Reassigning a program to another component

To reassign a program, open the header file of the program and change the references to the component that owns this program:

  • Open the Search/Replace function of Visual Studio®.
  • Search for all references of the old component name.
  • Replace them with the new component name.
    Visual Studio search and replace

The program is properly reassigned to another component of your C++ project.

 

Compiling the project

To compile the project, right-click on your project and select Build from the context menu.

Build project
→ The compilation process of your C++ project starts.

Successful compilation or possible error messages are displayed on the Output frame and in the Error List:

Build Output

Note: Usually, you receive a warning: offsetof within non-standard-layout type for every defined port, e.g.:

[cmake]: /home/user/Workspace/Counters/intermediate/code/Counters Library.meta.cpp:22:47: 
warning: offsetof within non-standard-layout type ‘Counters::CppRetain’ is undefined [-Winvalid-offsetof]

To suppress these warnings, add the following line to your project's CMakeLists.txt and replace NameOfYourLibrary with the actual name of your library:

target_compile_options(NameOfYourLibrary PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wno-invalid-offsetof>)

If compilation was successful, a shared object (*.so) file for each target is generated. Otherwise, refer to the error and warning messages for remedy.

Using a C++ program with PLCnext Engineer

To be able to use your C++ program in PLCnext Engineer, you need it compiled into a*.pcwlx library for use with PLCnext Engineer . The LibraryBuilder (which is part of the C++ toolchain installed by PLCnext CLI) automatically generates the library within the process of compilation. The file is located in the project folder, e.g. C:/Users/<username>/workspace/FirstProject/Bin.

  • Import the library into your PLCnext Engineer project.
  • Assign the C++ program to a task.

 

 

 


•  Web browser recommendation: Chrome/Edge 88 or newer, Firefox ESR 90 or neweror Safari • 
• Published/reviewed: 2022-09-14 • Revision 046 •