Network Variable can't accept constants for array length??

bosulliv
2021-04-19
2021-04-22
  • bosulliv - 2021-04-19

    In the documentation for network variables it states that arrays must "have limits defined with a literal or constant". However any constant I have tried to use (defined with VAR_GLOBAL CONSTANT) throws the error:

    Network Variables: GVL.length. not allowed. literal or constant expected

    I put together a simple test project to verify this. Defining the following in a network variable is all it takes. The constant can be defined in another global list or it can be defined right in the NVL, it will fail to compile:

    VAR_GLOBAL CONSTANT
        t           :INT      := 3;
    END_VAR
    
    VAR_GLOBAL
        arr         : ARRAY[0..NVL.t] OF INT;   
    END_VAR
    

    Is the documentation wrong, and you can only use literals to define array length in Network Variables? Or am I maybe misunderstanding what the the Network Variable needs to be properly defined? Kind of at a brick wall here...

     

    Last edit: bosulliv 2021-04-19
  • Quirzo - 2021-04-20

    From the same page:

    Constant expressions are not permitted for this purpose.
    
    Example: β€œarrVar : ARRAY[0..g_iArraySize-1] OF INT ;” is not transmitted, but β€œarrVar : ARRAY[0..10] OF INT ;” is transmitted.
    

    So yes. Constants wont work.

     
    • bosulliv - 2021-04-20

      I made the assumption that the documentation would not have a sentence that immediately contradicts the sentence that precedes it. Maybe it meant that constant expressions are not allowed but that constants are allowed? The example you included shows an expression after all.

       
  • bosulliv - 2021-04-22

    For anyone reading this later, it turns out that you CAN use constants, you just have to remove the auto-populated pragma {attribute 'qualifiedonly'} and then only reference those constants using their un-qualified name. Ex:

    Constant list:

    //{attribute 'qualifiedonly'}
    VAR_GLOBAL CONSTANT
        test           :INT      := 3;
    END_VAR
    

    Network variable list:

    VAR_GLOBAL
        arr         : ARRAY[0..test] OF INT;   
    END_VAR
    

    The same is true for enumerations, you can use them, but only if you don't use the fully qualified name.

    I can't imagine why the compiler can't recognize the variable as a constant if you use its fully qualified name (NVL_Constants.test vs test). That really seems like a bug to me . . . and the requirement isn't documented in the Network Variable documentation. Hopefully this can be corrected.

     

    Last edit: bosulliv 2021-04-22

Log in to post a comment.