how to make var constant variables globally available

Rob
2019-12-05
2019-12-10
  • Rob - 2019-12-05

    Hi everybody,
    ...me again with another question. I have several constant variables in a project (for instance... number_of_motors, number_of_sensors, ...) They are declared under VAR CONSTANT...END_VAR, once in the Main Prog and once gain under a Wrapper Function Block, naturally under VAR CONSTANT...END_VAR too.
    In the Main Prog I have an instance of the Wrapper Function Block.
    I am aiming to achieve certain modularity in my project, therefore if I need to increase/decrease the number_of_motors constant within the project, it isn't that great to have to adjust this value at two different places (once for the instanced object of the Wrapper Function Block in the Main Prog and once in the Wrapper variables declaration area itself).

    I have tried to declare the constants as VAR_GLOBAL in the Main Prog, but they don't seem to be available for the Wrapper Function Block (by building the project again I get a lot of related errors)
    How could I manage to declare constants only once in the project and made them truly globally available?

    Thanks in advance for your help,
    Rob

     
  • i-campbell

    i-campbell - 2019-12-05

    Hi Rob! Right click your Application in the device tree and "add object > Global Variable List".
    Give your Global Variable List an easy name to type like "GVL".
    This is where you make all of your global variables.
    Reference them in your code with GVL.ConstantName for example.

    [Bonus level]
    If you are against global variables for idealogical reasons, you can use a VAR INPUT CONSTANT in your FB and pass it the constant from where you call it in your main PRG.

     
  • Rob - 2019-12-06

    I-Campbell hat geschrieben:
    Hi Rob! Right click your Application in the device tree and "add object > Global Variable List".
    Give your Global Variable List an easy name to type like "GVL".
    This is where you make all of your global variables.
    Reference them in your code with GVL.ConstantName for example.
    [Bonus level]
    If you are against global variables for idealogical reasons, you can use a VAR INPUT CONSTANT in your FB and pass it the constant from where you call it in your main PRG.

    Hi I-Campbell, once again thanks for your tips!
    The first approach you described seemed unfortunately to not work out for me but I went for the second one and it "nearly" has worked out except for one issue...I let here a couple of screenshots where is to see what kind of error messages I've gotten by compiling...

    IMG: Screen Shot 2019

    IMG: Screen Shot 2019

    IMG: Screen Shot 2019

     
  • i-campbell

    i-campbell - 2019-12-07

    Ah, yes my mistake. The compiler needs just one size for all instances of the functions, but the var input constant is only at the instance level.

    You will have to get GVL working.

     
  • Rob - 2019-12-08

    ...when attempting to use globally defined variables as array upper bound I get these messages...
    Border 'GVL.CONSTANT' of array is no constant value
    ..is this assignment not allowed in Codesys ?

     
  • dFx

    dFx - 2019-12-09

    No troubles with array bounds defined with constants here.
    If you try to define an array bound with variable bound, then use "*".
    see : Codesys online help

     
  • Rob - 2019-12-09

    ...too complex for me for now...I will certainly come back to this topic in a future when my programming oop skills will ever improve. But thanks to all of you for your comments and hints!

    In a function block seems to be easy:

    FUNCTION POU : INT
    VAR_INPUT
    END_VAR
    VAR
    END_VAR
    VAR_IN_OUT
    MyArrayInPOU:ARRAY[1..GVL.TestVar] OF INT;
    END_VAR

    ( But in the Main Prog I've tried out with no results )

     
  • Rob - 2019-12-10

    oh! I was on the right track except I hadn't declared my constants in the GVL as VAR_GLOBAL CONSTANT but was VAR_GLOBAL !
    Once again: Thanks to all of you and to I-Campbell for this very self-explanatory example..
    (Yesterday I went some sort of misguided by digging into other "advanced" (to me) topics as LOWER_BOUND / UPPER_BOUND or the * holder)

     
    πŸ‘
    1

Log in to post a comment.