Skip to main content

Processing Device Projects and Library Projects in One Workspace

This guide shows a model of how development is done on a device project and a library project together in one workspace. Changes made in the library project are directly available in the device project.

Requirements:

  • CODESYS 4 is open.

Creating a workspace and a library project

  1. Click the Menu → Create Workspace command.

    Alternatively, you could select the command by using the _c4_icon_create_workspace.png button or the Command Center.

    The Select a Path for the Workspace File dialog will open.

  2. In this dialog, give a name to the new workspace – for example Calculator – and click the _c4_drei_Punkte_path_selection.png button to select the desired path in the file system. Then click OK.

    The Add Project to Workspace dialog will open.

  3. In this dialog, select the Create Library Project command.

  4. In the Create a new Library Project dialog, specify the data of your new library project.

    • Click the _c4_drei_Punkte_path_selection.png button to select the location.

    • Specify the project name, for example CalculatorLibrary.

    • Specify values for all other required parameters.

      The version number must be a 3 or 4-part semver, for example 2.1.0 or 1.0.0.0.

  5. Click the Create button.

    The library project will be created.

    The library project will be displayed in the workspace navigator as follows:

    _c4_img_calculator_library.png

Creating library POUs in the library project

  1. In the workspace navigator, right-click the CalculatorLibrary.fbslib item and select the Add Item → Function command.

  2. In the Add Function dialog, specify a name for the function – for example Addition – and INT as the Return value.

  3. Select Structured Text (ST) as the Implementation language and click Create.

    The Addition.fn.st function will be added to the workspace navigator and opened in the editor.

  4. In the editor, add the variable declarations for the input variables x : INT; and y := INT; as well as the implementation code Addition := x+y; so that the final function looks like this:

    // Small function to perform addition
    // x + y
    FUNCTION Addition : INT
    VAR_INPUT
        x : INT;    
        y : INT;
    END_VAR
    VAR
    END_VAR
    Addition := x+y;
    END_FUNCTION
  5. In the workspace navigator, add another function named Subtraction with the Return value INT to the library project. Select Structured Text (ST) as the Implementation language and click Create.

  6. In the editor, also add the variable declarations and implementation code to the Subtraction function so the final function looks like this:

    // Small function to perform a subtraction
    // x - y
    FUNCTION Subtraction : INT
    VAR_INPUT    
        x : INT;    
        y : INT;
    END_VAR
    VAR
    END_VAR
    Subtraction := x - y;
    END_FUNCTION

Creating a device project that uses the library

Requirement: The workspace with the library project created in the guide above is open.

  1. Select the Menu → Create Device Project command, or click the _c4_icon_add_project64px.svg icon and select the command.

  2. In the next dialog, specify the data for your new project and then click Create.

    • Click the _c4_drei_Punkte_path_selection.png button to select the location. Choose the same location as for the library project.

    • Choose the project name, for example CalculatorDevice.

    • Specify values for all other required parameters.

      The version number must be a 3 or 4-part semver, for example 2.1.0 or 1.0.0.0.

  3. In the Add PLC dialog, select the controller – for example CODESYS Control Win V3 x64 – and click OK.

  4. In the next dialog Add Program, PLC_PRG is already given as the Name and Structured Text (ST) is selected as the Implementation language. Click Create to confirm the dialog.

    The device project will be created and added to the workspace navigator parallel to the already created library project:

    _c4_img_calc_device_and_lib.png

    The PLC_PRG.prg.st program will be opened in the editor.

  5. In the workspace navigator, double-click the Libraries.json file of the device project and reference the CalcLib library project.

     {
        "references": {
            "Standard": {
                "$type": "Placeholder",
                "placeholder": "Standard"
            },
             "CalcLib": {
                "$type": "RelativePath",
                "path": "../../CalculatorLibrary.fbslib"
            }
         },
        "placeholderOverrides": {}}
  6. Right-click the Libraries.json file and select the Resolve Libraries command in the context menu.

    • The libraries will be resolved.

    • The Libraries.lock.json item will be updated with the library references.

  7. Switch to the editor of the PLC_PRG.prg.st program and add a variable declaration and the implementation code. When you type the first few letters of the name of your library name, the autocomplete function will suggest the library. The completed program will look like this:

    PROGRAM PLC_PRG
    VAR
        iVar : INT := 0;
    END_VAR
    
    iVar := CalcLib.Addition(iVar, 2);
    iVar := CalcLib.Subtraction(iVar, 1);
    
    END_PROGRAM
  8. Right-click the Application.iecapp item and select the Build Application command.

    CODESYS 4 compiles the project. If no errors have occurred, then you can proceed with Setting up communication with the controller and logging in to the controller.

  9. Make any desired changes to the library directly in the library project.

    When logging in again, CODESYS 4 detects the change and opens the Confirm Download dialog. It is not necessary to resolve the libraries again.

Setting up communication with the controller and logging in to the controller

  1. Double-click the Communication.settings.json item.

    The Communication Settings wizard will open in the editor.

  2. Click the Configure Device button.

    The Device Browser dialog will open.

  3. Click the Add Gateway button.

    Important

    The controller must be running in order to set up the gateway.

  4. In the Add Gateway dialog, choose a name for your gateway and click Add.

    Note: The values for IP address and Port remain unchanged.

    The gateway will be displayed in the device browser.

    The available controllers will be displayed.

  5. Click your controller to select it and click the Connect Device button.

    Because a user management has not been set up on your device yet, the Create Initial User for Device dialog will open.

  6. Choose a user name and password. Confirm the password and click Apply.

    Note: The chosen password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one special character, and one digit.

  7. In the Log In to Device dialog, provide your new credentials and click Log In.

    The notification Connection successful will be displayed.

  8. In the Device Browser dialog, click Select.

    For more information, see the following: Communication.settings.json

  9. Right-click the Application.iecapp application and select the Download Application command.

  10. In the Confirm Download dialog, click the Download button.

  11. Click the _c4_icon_login.png icon (top right) or press the Alt+F8 shortcut to log in to the controller.

    The notification Login succeeded will be displayed.

  12. Click the _c4__start_application_play64px.svg icon (top right) to run the program.

  13. Open the PLC_PRG program in the editor.

    The inline monitoring in the editor will show the changing value of the iVar variable.

  14. Click the _c4_stop_application_stop64px.svg icon (top right) to stop the program.

  15. Click the _c4_icon_logout.png icon (top right) or press the Ctrl+F8 shortcut to log out of the controller.