Skip to main content

Outsourcing Function Blocks to a Library

In the Creating a Device Project with CODESYS Control Win V3 x64 in Structured Text and Ladder Diagram tutorial, you have created an application with a fan control unit. In this tutorial, you will create your own library project from the fan control unit. This will turn the control unit into reusable code that you can use throughout the project in other applications.

Steps in this tutorial

A reusable library FanControlLib contains the ReglerFB_FanControl. The application in the FanControl device project uses the function block from the library, instead of from a local copy.

  1. Create a library project

  2. Extract the functionality into the library

  3. Use and reference the library project

Requirements

Create the library project

  1. Click the _c4_openWorkspace64px.svg button and open the TemperatureControl.fbsws workspace.

  2. Click the _c4_icon_add_project64px.svg button and select the Create Library Project command. In the Create a new Library Project dialog, specify the following data:

    • Path: D:\TemperatureControl

    • Project name: FanControlLib

    • Title, Version, and Company

  3. Click Create.

    The new library named FanControlLib.fbslib will now be available in the workspace navigator.

Extract the functionality into the library

  1. Move the FB_FanControl control unit from the application to the library:

    In the file explorer of your operating system, copy the FB_FanControl.fb.ld file from the D:\TemperatureControl\FanControl.fbsdev\Application.iecapp^ folder to the library folder D:\TemperatureControl\FanControlLib.fbslib away.

    Note

    CODESYS 4 does not currently support copying and pasting elements between projects.

    CODESYS 4 monitors the project folders in the file system. When you move the element in the file system, CODESYS 4 automatically creates it in the workspace navigator.

    In CODESYS 4, FB_FanControl.fb.ld will be displayed in the FanControlLib.fbslib project.

  2. Remove the FB_FanControl.fb.ld function block from the FanControl.fbsdev project. To do this, right-click in the device project under Application.iecapp, click the FB_FanControl.fb.ld item, and select the Delete Element command. Click Delete to confirm the step.

Use and reference the library project

  1. In the FanControl.fbsdev project, reference the FanControlLib.fbslib library. To do this, open the Libraries.json file of the device project in the editor:

    • Add the namespace FanControlLib.

    • Under the namespace, add a reference of type RelativePath to the library folder.

    The FanControlLib library will be referenced in the application.

    {
        "references": {
            "Standard": {
                "$type": "Placeholder",
                "placeholder": "Standard"
            },
            "FanControlLib": {
            "$type": "RelativePath",
            "path": "../../FanControlLib.fbslib/"
            }
          },
        "placeholderOverrides": {}
    }
  2. In the navigator, right-click Libraries.json and run the Resolve All Libraries command.

  3. Open the LIBRARY RESOLUTION view to check the notifications.

    The libraries have now been successfully resolved.

  4. In the PLC_PRG.prg.st program, use the function block from the library. To do this, open PLC_PRG in the editor and change the type of the fanControl variable to the name of the function block of the referenced library FanControlLib. Only the declaration changes. The call remains the same.

    Tip

    The autocomplete feature will help you as you type.

    // before
    fanControl : FB_FanControl;
    
    // now
    fanControl : FanControlLib.FB_FanControl;
  5. Right-click the Application.iecapp item and select the Build Application command.

  6. Log in and start the application. For more information, see the following: Downloading a program to the controller.

    The fan controller is working exactly as before: the temperature oscillates between 25 and 30 °C, while fanOn toggles. The behavior is identical. The logic is now contained in a library which you can use throughout the project. You have just created and used your own CODESYS 4 library.