In CODESYS there are two possibilities of dynamically creating an FB instance:
A.) By using the operators __NEW and __DELETE and a MemoryPool which has to be parameterized for the respecting application via CODESYS. |
However this method has a significant disadvantage:
|
B.) By using the library FBFactory there is a second and more reliable possibility to meet the demands of dynamically creating an FB instance. |
With the help of the FBFactory Library a static pool (which can be extended into a dynamic pool if needed) for each factory is defined in advance. The management of the memory space is organized in way which makes sure that no fragmentation is possible. Therefore there is no obstacle to a non-stop operation of the system. FBFactory Library can be used as a base for building factories. In order to get more information about how to use this library for building factories please download the Test Project which you will find in the documentation of the FBFactory Library |
There you will find a Test Project which uses a Test Library that extends the FBFactoryLibrary. It is recommended to look at how the example program (PLC_PRG) works by setting a breakpoint and stepping into each action of the program. Do not go online in simulation mode! Moreover the documentation of the FB Factory Test Library (which is already added to the Library Manager in the Test Project) explains what is done in each part of the library.
There is a template for libraries in CODESYS which is called CODESYS library which assists you building your own FB Factory. Therefore please open a new project and choose the category Libraries and afterwards the template CODESYS library.
In this project the folder Templates contains the subfolder FBFactory with all the important FBs, Methods and Properties.
All relevant Function Blocks should be copied in the folder with the title Function Blocks in order to be able to modify them.
Afterwards change the names of every element to suitable expressions.
By clicking on each element with the right mouse button and choosing Properties -> Build you can deactivate Exclude from build
Moreover it is advisable to adapt the array abyPoolMemory in the function block TestFBFactory to ones own needs.
Make sure that everywhere in TestFBFactory (for exemple in the first line of the method TestFBFactory.Create) the expression Custom is substituted by the name you chose for these elements.
By calling up the method Create it is now possible to dynamically create a FB instance.