Changing Values with Recipes
Use recipes to change or read recipes values for a specific set of variables (recipe definition) on the controller at the same time.
You define the basic settings for recipes, such as location and format, in the Recipe Manager object. Insert one or more recipe definitions below this object. A recipe definition is composed of one or more recipes for the contained variable. The recipe consists of specific variable values.
You can save a recipe to a file or write directly from files to the PLC.
Recipes can be loaded via the CODESYS development interface, the visualization element, or the application program.
Using recipes on remote devices
The variable values from recipes are transferred automatically to and from another controller when they are data source variables and a data source exchange is configured. Reading and writing occurs synchronously. Therefore, CODESYS updates all variables in a recipe at the same time. After reading or writing, you can use the call g_RecipeManager.LastError
to check whether or not the transfer was successful (g_RecipeManager.LastError = 0
).
Handling of recipes in the CODESYS user interface
The development interface of CODESYS provides commands for generating recipes as well as reading/writing in online mode.
Using recipes in applications
At runtime, you can use recipes in the user program and visualization elements.
In the user program, you use the methods for the function block RecipeManCommands
from the library RecipeManagement
. In the visualization, you use recipes via the input configuration (execute command of visualization elements.
Tip
During the initialization process, the recipe management reads the values of the variables that are defined in the recipe definition. This operation takes place at the end of the initialization phase of the application. At this point, all initial values of the application variables are set. This is performed to initialize missing values from recipe files correctly.
Creating a recipe
In the device tree, select the Application object.
Click
.CODESYS adds the Recipe Manager to the device tree.
In the device tree, click the Recipe Manager object.
Click
.CODESYS adds the recipe definition below the Recipe Manager.
Open the editor of the recipe definition by double-clicking the object.
Double-click the blank field below Variable. Specify the name of a variable to define a recipe for. You can use the Input Assistant (
button) for this.
Click
and specify a name for the new recipe.A column with the new recipe name appears in the editor.
Enter the variable value for this recipe in this field.
Insert additional fields as needed.
Select a variable value for the recipe and click
. Select a location and file name.CODESYS saves the recipe in the format as defined in the Recipe Manager.
Loading a recipe from a file
Requirement: A Recipe Manager is available in the application. In a recipe definition, there is a myRec recipe with variable values. A myRec.txt
recipe file is located on the file system and contains the entries for this recipe.
Example of the recipe file:
PLC_PRG.bVar:=0 PLC_PRG.iVar:=2 PLC_PRG.dwVar:=35232 PLC_PRG.stVar:='first' PLC_PRG.wstVar:='123443245'
Double-click the Recipe Definition object in the device tree to open the tabular editor for the definition of the individual recipes.
You see the
myRec
column with the current values for this recipe.Edit the
myRec.txt
file in an external text editor and replace the variable values with other values that you want to load into the recipe definition in CODESYS. Save the file.In the recipe definition, click the myRec column and in the context menu click Load Recipe.
A dialog prompt notifies you about the possibly needing to perform an online change when logging in again. An online change is necessary when you change the current values of the recipe variables by loading the recipe.
Click Yes to close the dialog and continue. Select the
myRec.txt
file from the file explorer for loading.The recipe values in the recipe definition are updated according to the values read in the file.
Tip
If you want to overwrite only individual recipe variables with new values, then remove the values for the other variables before loading to the recipe file. Entries without value definitions are not read, and therefore updating leaves these variables unchanged on the controller and in the project.
For values of the data type REAL/LREAL, the hexadecimal value is also written to the recipe file in some cases. This is necessary so that the exactly identical value is restored when converting back. In this case, change the decimal value and delete the hexadecimal value.
Recipe management on the controller; memory usage
When you clear the Recipe management in the PLC option, the Recipe Manager and recipe definitions will not use any memory on the PLC.
If you select this option, then code is generated for the Recipe Manager and all recipe definitions, and this code is stored on the PLC. The size of the used memory primarily depends on the number pf recipes and their variables, as well as the data type of the variables. Whether or not the fields of the recipe definition are filled also has an effect. The memory usage of recipes cannot be calculated. It has to be determined by experimentation at the time it is needed. The following table merely provides some guiding principles.
Code Size (bytes) | Data Size (bytes) | Total (bytes) | |
---|---|---|---|
Recipe definition with 100 INT variables | 194406 | 79400 | 267352 |
Recipe definition with 200 INT variables | 238318 | 121284 | 459344 |
Recipe definition with 300 INT variables | 282230 | 163084 | 543856 |
Recipe definition with 100 BOOL variables | 192742 | 69884 | 343168 |
Recipe definition with 200 BOOL variables | 235446 | 101568 | 436872 |
Recipe definition with 300 BOOL variables | 278146 | 133284 | 510072 |
Recipe definition with 100 string variables | 203278 | 870084 | 1154000 |
Recipe definition with 200 string variables | 255570 | 1709784 | 2973296 |
Recipe definition with 300 string variables | 307886 | 2549484 | 2964112 |
Loading recipe values from the controller
You can apply recipe values on the controller to recipe definitions in the project, even if these definitions have been modified in the project.
Requirement: The Recipe management in the PLC is option is selected in the Recipe Manager.
Create a recipe definition
RecDef1
in the project, containing the variablesPLC_PRG.ivar
andPLC_PRG.bvar
. Insert a recipe R1: value forPLC_PRG.ivar
:33
; value forPLC_PRG.bvar
:TRUE
.Log in to the controller and download the application.
The recipe file
R1.RecDef1.txtrecipe
is saved to the default directory of the controller ($PlcLogic$).Logout and add another variable
PLC_PRG.dwvar
to the recipe definition in the project.Edit the recipe definition file
R1.RecDef1.txtrecipe
on the device by changing the value forPLC_PRG.ivar
from33
to34
.Moreover, add another recipe R2 on the device. To do this, copy the
R1.RecDef1.txtrecipe
and rename it toR2.RecDef1.txtrecipe
. Then edit this file and change the recipe values:PLC_PRG.ivar
:1
,PLC_PRG.bvar
:FALSE
.Now two recipes R1 and R2 are available on the device. In the project, there is only R1, and it also contains other values than R1 on the device.
Log in to the controller by means of an online change.
Click Load Recipes from Device from the context menu.
A dialog prompt notifies you that executing the command at the next login may trigger an online change, and that the recipes on the runtime system will overwrite the recipes of the current recipe definition.
Confirm that you want to continue.
A dialog prompt notifies you that the recipe for
PLC_PRG.dwvar
loaded on the device cannot yield a value from the controller.Confirm that you want to continue.
The value of
PLC_PRG.ivar
in recipe R1 of the recipe definition in the project changes to34
. The recipe R2 with the values1
andFALSE
is also listed in the recipe definition now.PLC_PRG.dwvar
remains in the recipe definition.