ARRAY with and ENUM

fraccaroli
2022-06-15
2022-06-16
  • fraccaroli - 2022-06-15

    Topic: ARRAY idexed by ENUM value

    Hi all,
    I'm working with ARRAY indexing and I get some problems. I'm used to work with C and there I define and ENUM with indexes like this:

    TYPE INDEX :
    (
        ID1,    //0
        ID2,    //1
        ID3,    //2
        NELEM := 3
    );
    END_TYPE
    

    Then I generally declare an ARRAY with "NELEM" as size. In CODESYS is quite difficult, because if I write

        values : ARRAY [0..INDEX.NELEM] OF INT;
    

    the array has 4 elements, not 3.

    If I write

        values : ARRAY [1..INDEX.NELEM] OF INT;
    

    the array has 3 elements, but INDEX.ID1 has no location, because the index in the array starts from 1.

    I can't write

        values : ARRAY [0..INDEX.NELEM-1] OF INT;
    

    because of error C0359: Arithmetics not allowed on strict ENUM type '...'.

    I read this topic:
    https://forge.codesys.com/forge/talk/Engineering/thread/f237737cda/

    but has no good solutions. The only remaining solution of that topic is to have unused array locations, but safety rules (for safety programming) generally state to avoid unused variables and empty location.

    Any other ideas?
    At now I probably define global variables, but it is an error-prone solution.

    Francesco

     

    Last edit: fraccaroli 2022-06-15
  • i-campbell

    i-campbell - 2022-06-15

    did you remove the 'strict' attribute from your enum definition already?

     
  • Fless

    Fless - 2022-06-15

    Welcome Francesco,

    this works for me:

    {attribute 'qualified_only'}
    {attribute 'strict'}
    TYPE INDEX :
    (
        ID1,    //0
        ID2,    //1
        ID3,    //2
        {warning disable C0125}
        LASTELEM := ID3
        {warning restore C0125}
    );
    END_TYPE
    
    PROGRAM POU
    VAR
        values : ARRAY [0..INDEX.LASTELEM] OF INT;
    END_VAR
    
    values[INDEX.ID1] := 1;
    values[INDEX.ID2] := 10;
    values[INDEX.ID3] := 100;
    
     
    πŸ‘
    1
    • fraccaroli - 2022-06-16

      Hi Fless, hi i-campbell,
      I added the warning disable as Fless suggest, but if I use the enum in the project the warning come:

      [WARNING] INDEX C0125: The constant 2 is assigned to more than one enumeration
      Compile complete -- 0 errors, 1 warnings

      I think that the only workaround (without using global constant) is to remove the 'strict' attribute.

      It seems that CODESYS does not support this way to use array and enums. I was only curious to understand if there were some other way conform with standard programming rules.

      Thanks!
      Francesco

       

Log in to post a comment.