'VAR_STAT' declaration not allowed in this place - bug?

Volvo742
2016-06-09
2020-05-04
  • Volvo742 - 2016-06-09

    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.

     
  • 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.

     
  • McFeardon - 2016-06-23

    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_BLOCK PUBLIC MyObject
    VAR_STATIC
       iCount : INT := 0;
    END_VAR
    ____________________
    iCount := iCount +1;
    

    Then in your main program:

    VAR
        oMyObject1 : MyObject;     // initializes iCount to 0
        oMyObject2 : MyObject;     // iCount already initialized
    END_VAR
    ____________________
    oMyObject1();     // iCount is now equal to 1
    oMyObject2();     // iCount is now equal to 2
    // oMyObject1.iCount = oMyObject2.iCount at all times
    

    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

     
  • angel - 2020-05-04

    Maybe you are using a Program instead a Function.
    I had the same problem using a POU, and not using a Function

     

Log in to post a comment.