Integrating C Modules

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:

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 view 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.

Note

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.

Hint

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.

Precompiled module in a library:

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.

See also

Configuring C code modules

Requirement: A project is open that already includes a C code module.

  1. Click the object C code module in the device tree.

  2. Select the command Properties in the context menu.

  3. Open the Build tab in the Properties dialog.

  4. Specify the file path of the Visual Studio installation on your computer. The input assistant () and the search tool (magnifying glass) are also available.

  5. 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.

  6. 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.

  1. Select Application in the device tree and click Project ‣ Add object ‣ C code module .

  2. If necessary, specify a new name for your C code module in the Add C code modules dialog. If you do not, then your object will be given the standard name C code module.

  3. Click the symbol () next to the Source directory input field.

  4. The Find Folder dialog opens.

  5. In the Find folder dialog, select the folder containing the C source files (*.c, *.cpp, *.h, or *.hpp).

  6. 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.

  7. Click Add.

    ⇒ CODESYS inserts the C code module into the device tree with the folders Extensions, IEC interface, and Source files.

  8. In the device tree, click the plus symbol (+) of the Source files folder.

    ⇒ The imported C source files are listed in the open folder.

  9. 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.

  1. Click the object C code module in the device tree.

  2. Click Project ‣ Add object ‣ C code file .

  3. In the Add C code-file dialog, 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.

  4. 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.

  1. Click the object C code module in the device tree.

  2. Click Project ‣ Add object ‣ C code file .

  3. In the Add C code file dialog, 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.

  4. 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);

  1. Click a C code file with the file extension *.h. In this example, it is test.h.

  2. Click Build ‣ C-Integration ‣ Create IEC interface .

    ⇒ The dialog Create C interface opens and lists the file test.h and its function adder (int, int). Both are activated for the import.

  3. Click Import.

  4. CODESYS generates the adder (FUN) function and inserts it as an object in the IEC interface folder in the device tree.

  5. When you double-click the adder (FUN) object, it opens in the editor.

    ⇒ It contains the following declaration part:

  6. 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.

  1. 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 message view (C code module category), you will find a message that an m4 file has been successfully created.

    Note

    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 view. 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.

  1. Click View ‣ POUs .

    ⇒ The POUs view opens and displays the library project and its objects.

  2. Select the library project and click Project ‣ Add object ‣ C implemented library .

  3. Click Add in the Add C implemented library dialog.

    ⇒ CODESYS adds the object C implemented library to the POUs view.

  4. Double-click the C implemented library object.

    ⇒ The object opens in its editor

  5. Click Add in this editor.

    ⇒ The Select device dialog opens.

  6. In the Object file input field, specify the name of a dynamically loadable module in the format *.dll or *.so.

    Hint

    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.

  7. In the Device window, select a device for assignment of the Object file.

  8. Click Select device.

    ⇒ CODESYS displays the created device file assignment in the editor on the tab Compiled components.

  9. Save the library project.

See also