With the C code integration plugin, externally implemented C code files can be included in CODESYS projects and C stubs can be generated from IEC objects.
In CODESYS, the “C code module” object type is available for this purpose. The C code files and the used IEC objects are located below a “C code module”. A file directory on the hard disk with C code files is assigned to each C code module.
In the project, you can generate IEC objects from a C code file in the format *.h or *.hpp (header file) in order to use them in other POUs.
The generation of C-stubs is intended for the following use cases:
-
A C code file accesses an IEC object: A C code file cannot access an IEC object directly. It can access only the C stub that was generated from the IEC object.
-
Generation of precompiled modules that you can merge into a library project.
After being imported, the imported source code files are part of the CODESYS project and they are therefore decoupled from the original files on the disk.
During compilation, a dynamic module is generated from a C code module and saved as part of the project. Information, warnings, and errors are displayed in the message window in the “C code module” category.
All dynamic modules of an application are transferred and loaded to the runtime system during the download. The runtime system must support dynamic linking for this.
License for the runtime system
License for the runtime system
The runtime system requires a license that permits C modules to be loaded. Without this license, dynamic modules cannot be linked during the download, and therefore the download will be aborted.
The dynamic modules are part of the boot application and they are reloaded and activated when the controller is restarted. The “Reset origin” command unloads all C code modules in the application. The “Reset cold” and “Reset warm” commands do not lead to a repeated initialization of the C code modules.




NOTICE

No C code for simulation mode
In simulation mode, C code is not generated and loaded to the runtime system. To simulate the code contained in the C modules anyway, you can implement it for this purpose in the respective IEC objects of the C code module.
CODESYS does not support the monitoring of variables in C code files or the setting of breakpoints in C source code.
User Library POUs are listed on Input Assisted (F2) but the feature does not give any meaningful results in the context of a C code POUs. These POUs are listed as standard and not as user defined Function / Function Blocks / Programs.
Precompiled modules in libraries
C code integration provides the capability of assigning a precompiled runtime module (example: *.dll) in the library to a device and then to save it in the library. Then, these modules can be loaded dynamically.
Configuring C code modules
Requirement: A project is open that already includes a C code module.
-
Click the
object “C code module” in the device tree.
-
Select the command “Properties” in the context menu.
-
Open the “Build” tab in the “Properties” dialog box.
-
Specify the file path of the Visual Studio installation on your computer. The input assistant (
) and the search tool (magnifying glass) are also available.
-
Specify the file path of the MS Windows SDK installation on your computer. The input assistant (
) and the search tool (magnifying glass) are also available.
-
Specify a file path for CODESYS to store the temporary compile files.
Importing folders with C source files from the file directory
Requirement: A project is open. The project controller supports the integration of C code.
-
Select “Application” in the device tree and click “Project Add object C code module”.
-
If necessary, specify a new name for your C code module in the “Add C code modules” dialog box. If you do not, then your object will be given the standard name “C code module”.
-
Click the symbol (
) next to the “Source directory” input field.
-
The “Find folder” dialog box opens.
-
In the “Find folder” dialog box, select the folder containing the C source files (
*.c
,*.cpp
,*.h
, or*.hpp
). -
When you select the “Monitor folder for source code changes” option, CODESYS displays a message when changes have been made to the C source files in the selected folder of the file system.
-
Click “Add”.
CODESYS inserts the C code module into the device tree with the folders “Extensions”, “IEC interface”, and “Source files”.
-
In the device tree, click the plus symbol (“+”) of the “Source files” folder.
The imported C source files are listed in the open folder.
-
If you double-click one of the C source files (
), then the C code file opens in your editor.
Importing individual C code files
Requirement: A project is open that already includes a C code module.
-
Click the
object “C code module” in the device tree.
-
Click “Project Add object C code file”.
-
In the “Add C code-file” dialog box, use the input assistant (
) to select a file in
*.c
, *.cpp
, *.h
, or*.hpp
format, and then click “Add”.CODESYS inserts the selected C code file into the device tree below the
“C code module”.
-
If you double-click the new C code file (
) in the device tree, then it opens in the editor for modification.
Generating empty C code files
Requirement: A project is open that already includes a C code module.
-
Click the
object “C code module” in the device tree.
-
Click “Project Add object C code file”.
-
In the “Add C code file” dialog box, specify the name for the new C code file with the appropriate file extension and click “Add”.
CODESYS inserts the selected C code file into the device tree below the
“C code module”.
-
If you double-click the new C code file (
) in the device tree, then it opens in the editor for modification.
Converting C code files into IEC objects for use as programming objects in applications
Requirement: A project is open that includes a C code module and
C code files. For example, the C code file contains the following C code:
int adder(int a, int b);
-
Click a
C code file with the file extension
*.h
. In this example, it istest.h
. -
Click “Build C-Integration Create IEC interface”.
The dialog box “Create C interface” opens and lists the file
test.h
and its functionadder (int, int)
. Both are activated for the import. -
Click “Import”.
-
CODESYS generates the “adder (FUN)” function and inserts it as an object in the “IEC interface” folder in the device tree.
-
If you double-click the object “adder (FUN)”, it opens in the editor. It contains the following declaration part:
-
You can now call the
adder
function in the implementation part of a POU (example:adder (diVar1, diVar2);
).
Creating C stubs
Requirement: A project is open that includes a C code module. A POU is added to the C code module and this POU has implemented code.
-
In the device tree, select the POU below the C code module and click “C-Integration Create stub implementation in C”.
CODESYS creates the objects “iec_external.c” and “iec_external.h” and adds them to the “Extensions” folder in the device tree.
In the messages window (“C code module” category), you will find a message that an m4 file has been successfully created.
When you click “Create stub implementation in C”, the application is compiled automatically. If errors occur in the process, then these are indicated in the message window. In addition, please monitor the messages in the “C code module” category.
Assigning precompiled runtime modules to devices and saving them in libraries
Requirement: A library (*.library) is open in CODESYS.
-
Click “View POUs”.
The “POUs” view opens and displays the library project and its objects.
-
Select the library project and click “Project Add object C implemented library”.
-
Click “Add” in the “Add C implemented library” dialog box.
CODESYS adds the object
“C implemented library” to the “POUs” view.
-
Double-click the
“C implemented library” object.
The object opens in its editor
-
Click “Add” in this editor.
The “Select device” dialog box opens.
-
In the “Object file” input field, specify the name of a dynamically loadable module in the format *.dll or *.so.
NOTICE
The *.dll file must contain the title of the library project in its name. For example, if the library project is named XYlib, then the “Object file” must be called:
<Name>_XYlib.dll
. -
In the “Device” window, select a device for assignment of the “Object file”.
-
Click “Select device”.
CODESYS displays the created device file assignment in the editor on the tab “Compiled components”.
-
Save the library project.