Most of you know the common variable sections, VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT and VAR_PUBLIC. However in TwinCAT we have some more variable sections available. In this post I will discuss and explain the usage of VAR_TEMP, VAR_STAT and VAR CONSTANT. Variable sections which I find very use full from time to time.
VAR_TEMP
What is it: Variables declared in a variable section in functions and methods are always temporary variables. The variables are initialized when a method or function is called and are forgotten afterwards.
But variables declared in variable sections in a program or in a functions block are static within the function block or program. This is not always what you want, or need. Typically counters and temporary help variables do not need to be remembered. Most notably the FOR index variable ‘i’. With VAR_TEMP you can declare these variables as temporary variables. This means that the variables are reinitialized each time you program or function block instance is called!
How to use it:
FUNCTION_BLOCK Pou VAR END_VAR VAR_TEMP i:int; END_VAR
When to use it:
In my experience it is usually not a matter of performance, but a clarification of your variable usage. Explicitly distinguishing between static and temporary variables makes your code and it’s intention more clear to the reader.
VAR_STAT
What is is: Variables declared in ‘normal’ variable sections are saved in there owning program of function block instance. By declaring variable in a VAR_STAT section you create static variable of which only one instance exist. Also if there are multiple instances of it’s owner! It is good to know that the VAR_STAT is not described in the IEC61131-3, but is an extension to it made by Codesys.
For example the declaration of a function block ‘POU’
FUNCTION_BLOCK POU VAR_STAT i:int; END_VAR
The program Main has two instances of this function block. But assigns only a value to the first instance:
PROGRAM MAIN VAR POU1:Pou; POU2:POU; END_VAR
POU1.i:=1;
Even though only the variable in the first instance is assigned, the value of ‘i’ is the same in both instances!
How to use it:
FUNCTION_BLOCK POU VAR END_VAR VAR_STAT i:int; END_VAR
When to use it: When you want a ‘global’ variable which will be same variable in within the scope of the function block.
VAR CONSTANT
What is it: The VAR CONSTANT is no variable section itself, but the CONSTANT keyword can be added two different variable sections: VAR, VAR_INPUT, VAR_STAT and VAR_GLOBAL.
As you would expect the variables defined in a VAR CONSTANT are read only and can only be initialized with an variable.
How to use it:
FUNCTION_BLOCK POU VAR END_VAR VAR_STAT PI:REAL:=3.14; END_VAR
When to use it: As with VAR_TEMP it is not so much of a performance gain. But more a clarification of the intention of a variable.
Conclusion
This post discussed the usage of VAR_TEMP, VAR_STAT and VAR CONSTANT. Variable section I find useful from time to time. Let me know you experiences with different variable sections. For more info on variable declarations check out this post about variable lists!