Getting started with C++ on a Beckhoff PLC - Part One: Setup

Getting started with C++ on a Beckhoff PLC - Part One: Setup

Beckhoff PLCs are one of the few industrial PLCs that allow you to run real-time C++ directly on the PLC hardware. This allows for C++ code to run cyclically on a separate system task, or as a TcCOM object allowing for function calls from structured text.

This guide will cover how to create the external interface and how to make C++ function calls and algorithms within your PLC program.


  1. Setting up the TwinCAT C++ IDE
  2. Creating your C++ and PLC programs.
  3. Adding a Test Signature Certificate.

Step 1: Setting up TwinCAT C++ IDE

Back to Steps List

While structured text development on TwinCAT can be done within the TwinCAT XAE Shell, TwinCAT C++ development must be done within Visual Studio 2019 with the TwinCAT extensions installed. If a TwinCAT solution with a C++ project is opened in XAE shell, you will be unable to view or edit the C++ code. TwinCAT integration will support Visual Studio 2022 in build 4026.

If XAE Shell was installed before Visual Studio 2019, XAE shell must be uninstalled before installing Visual Studio, then XAE Shell can be reinstalled with the integration added.

You can follow TwinCAT’s guide on integrating with Visual Studio here.

Step 2: Creating the C++ and PLC programs

Back to Steps List

The C++ integration can be added to a new or existing PLC program. A series of DMC guides on how to set up and connect to a TwinCAT PLC can be found here.

To add or create the C++ project: 

  1. Under the C++ node of the solution, right click and select “Add New Item…”

  1. A popup will appear displaying the different types of TwinCAT C++ Projects.
    1. Select “Versioned C++ Project” to create an interface accessible by structured text.
    2. “Driver projects” are no longer recommended by TwinCAT and have fewer features compared to “Versioned C++ Projects”.
    3. “Static Library Projects” create static libraries that create functions and classes that can be accessed from other C++ projects; however, they cannot create an interface accessible from structured text code.
  2. For this external interface, I will select "Versioned C++ Project".
  3. A popup will appear asking what type of Module Class to add a preset configuration.
    1. To create a project that provides functions to call from structured text, select “TwinCAT Module Class for RT Context”. 
    2. The Cyclic Caller, Cyclic IO, and Data Pointer options all create a CyclicUpdate() function that can be attached to a task and have C++ run cyclically. This will run C++ code every scan.

  1. Enter a name and click OK.

Step 3: Adding a TwinCAT Test Certificate

Back to Steps List

To run C++ programs on the PLC, a certificate must be added to ensure all C++ code is verified. TwinCAT allows for officially signed certificates, but a test certificate is sufficient for developing and testing code. TwinCAT recommends that all new projects use Versioned C++ projects, which requires the new TwinCAT Certificate. If developing software that uses the older "Driver Project", then an Operating System certificate is needed instead.

For more information about Driver signing, reference the Beckhoff Information System.

To add a new test certificate for a C++ Versioned Project:

  1. In Visual Studio 2019, go to Extensions/TwinCAT/Software Protection. In XAE Shell, go to TwinCAT/Software Protection.

  1. In the wizard, click “Create New…” and fill out the OEM name, Unique Name, and select the checkbox for “Sign TwinCAT C++ Executables (.tmx). Click "Yes" on the popup that appears.

  1. Click “Start” and save the file in the default location (C:/TwinCAT/3.1/CustomConfig/Certificates/). 
  2. Enter a password. 
    1. It is recommended to use the TcSignTool to create the test signature password as TwinCAT will store the password in plaintext in the project file which can be easily shared.
  3. Once the certificate has been created, it needs to be added to the C++ project. Right click the C++ Project and click "Properties." Under the “Tc Sign” tab, select "Yes" for TwinCAT signing, and enter the certificate name and password.

  1. Because these test certificates are not countersigned by Beckhoff, the target PLC must be put into test mode to run the C++ code. Once the certificates have been countersigned by Beckhoff, your target PLC will not need to be in test mode.
    1. On your Target PLC, open Command Prompt with Administrator privileges.
    2. Run “bcdedit /set testsigning yes” in Command Prompt (Admin).
    3. Restart your computer/PLC.
    4. The Target PLC will now allow test certificate-signed C++ code to run on the PLC.
  2. Right click the C++ Project and click “Publish Modules”. It should create an error that says the certificate is not valid or ADS has failed.
  3. Open (C:/TwinCAT/3.1/Target/OemCertificates) on the target PLC and locate the file with the same name as the certificate’s “Unique Name”.
  4. Double click the file to add the certificate to Window’s certificate registry. The "Publish Modules" button should now work, and the C++ project can build and run on your machine. 

Activating a TwinCAT 3 project 5:


Now that the IDE and test certificate have been configured, the C++ project can be configured with the desired functions and interfaces and linked to a structured text function block, which will be covered in part two of this blog series.

Learn more about our Beckhoff and TwinCAT 3 Programming expertise and contact us for your next project.


There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

Enter the code shown above:

Related Blog Posts