When i createing a new variable i have the possibility to choose that the variable should be declared as a static variable.
When i do that i got that error that i can't declare static variable in the declaration area.
Is that a bug in Codesys?
I use Codesys 3.5 patch 4.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2016-06-22
Originally created by: scott_cunningham
From the online help:
Zitat:
This feature is an extension to the IEC 61131-3 standard.
Static variables can be used in function blocks, methods and functions. They have to be declared locally between the keywords VAR_STAT and END_VAR and will be initialized at the first call of the respective POU.
Static variables are only accessible within the scope in which they are declared (like a static variable in C), but like a global variable do not loose its value after the POU is left. For example in a function they might be used as a counter for the number of function calls.
Regard the possibility of adding an attribute to 'VAR_STAT'.
In reality, it is not necessary for function blocks, since regular VARs also keep their value from call to call.. Program VARs also hold their value from call to call.
I've never had the need to use them. If I need some type of counter or "memory" in a method, I define the variable as a VAR in the function block the method is attached to. I only use functions for conversions of sorts, so no need to keep a value from call to call. I suppose in the help's example one may want to know how many instances of a function block exist... but in general, you already know that.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Volvo: I am using static variables in Codesys 3.5.7 without any issues.
Scott: Static variables are generally used to share a common data between multiple instances of the same object/function block.
For example, if you were to declare:
FUNCTION_BLOCKPUBLICMyObjectVAR_STATICÂ Â iCount:INT:=0;END_VAR____________________iCount:=iCount+1;
Then in your main program:
VARÂ Â oMyObject1:MyObject;Â Â Â //initializesiCountto0Â Â oMyObject2:MyObject;Â Â Â //iCountalreadyinitializedEND_VAR____________________oMyObject1();Â Â Â //iCountisnowequalto1oMyObject2();Â Â Â //iCountisnowequalto2//oMyObject1.iCount=oMyObject2.iCountatalltimes
I haven't found a good resource on static variables in Codesys, but the following link is a reference to static variables in Java. The same concepts still apply. https://www.caveofprogramming.com/java/ ... -they.html m
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello.
When i createing a new variable i have the possibility to choose that the variable should be declared as a static variable.
When i do that i got that error that i can't declare static variable in the declaration area.
Is that a bug in Codesys?
I use Codesys 3.5 patch 4.
Originally created by: scott_cunningham
From the online help:
In reality, it is not necessary for function blocks, since regular VARs also keep their value from call to call.. Program VARs also hold their value from call to call.
I've never had the need to use them. If I need some type of counter or "memory" in a method, I define the variable as a VAR in the function block the method is attached to. I only use functions for conversions of sorts, so no need to keep a value from call to call. I suppose in the help's example one may want to know how many instances of a function block exist... but in general, you already know that.
Volvo: I am using static variables in Codesys 3.5.7 without any issues.
Scott: Static variables are generally used to share a common data between multiple instances of the same object/function block.
For example, if you were to declare:
Then in your main program:
I haven't found a good resource on static variables in Codesys, but the following link is a reference to static variables in Java. The same concepts still apply.
https://www.caveofprogramming.com/java/ ... -they.html m
Maybe you are using a Program instead a Function.
I had the same problem using a POU, and not using a Function