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.
Create a library project
Extract the functionality into the library
Use and reference the library project
Requirements
You have completed the steps in Creating a Device Project with CODESYS Control Win V3 x64 in Structured Text and Ladder Diagram. The
TemperatureControlworkspace and theFanControldevice project already exist.The SoftPLC CODESYS Control Win V3 x64 is installed and running.
Create the library project
Click the
button and open the
TemperatureControl.fbswsworkspace.Click the
button and select the Create Library Project command. In the Create a new Library Project dialog, specify the following data:
Path:
D:\TemperatureControlProject name:
FanControlLibTitle, Version, and Company
Click Create.
The new library named
FanControlLib.fbslibwill now be available in the workspace navigator.
Extract the functionality into the library
Move the
FB_FanControlcontrol unit from the application to the library:In the file explorer of your operating system, copy the
FB_FanControl.fb.ldfile from theD:\TemperatureControl\FanControl.fbsdev\Application.iecapp^folder to the library folderD:\TemperatureControl\FanControlLib.fbslibaway.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.ldwill be displayed in theFanControlLib.fbslibproject.Remove the
FB_FanControl.fb.ldfunction block from theFanControl.fbsdevproject. To do this, right-click in the device project underApplication.iecapp, click theFB_FanControl.fb.lditem, and select the Delete Element command. Click Delete to confirm the step.
Use and reference the library project
In the
FanControl.fbsdevproject, reference theFanControlLib.fbsliblibrary. To do this, open theLibraries.json fileof the device project in the editor:Add the namespace
FanControlLib.Under the namespace, add a reference of type
RelativePathto the library folder.
The
FanControlLiblibrary will be referenced in the application.{ "references": { "Standard": { "$type": "Placeholder", "placeholder": "Standard" }, "FanControlLib": { "$type": "RelativePath", "path": "../../FanControlLib.fbslib/" } }, "placeholderOverrides": {} }In the navigator, right-click
Libraries.jsonand run the Resolve All Libraries command.Open the LIBRARY RESOLUTION view to check the notifications.
The libraries have now been successfully resolved.
In the
PLC_PRG.prg.stprogram, use the function block from the library. To do this, openPLC_PRGin the editor and change the type of thefanControlvariable to the name of the function block of the referenced libraryFanControlLib. 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;
Right-click the
Application.iecappitem and select the Build Application command.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
temperatureoscillates between 25 and 30 °C, whilefanOntoggles. 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.