Search code examples
arraysenumsstiec61131-3structured-text

Symbolic adressing of array elements


I have an array of objects, lets say MyArray[1..x] of Object.

When programming I want to have a more "readable" way of addressing each object. Instead of saying MyArray[1] := ...etc. I would like to say MyReadableName :=...

I've looked into references, but I'm worried a bit about the whole pointer stuff. How could I do this in a good way and support online change? And where should I put the declaration and assignment of the references, it could be many many hundreds, and I don't want to clutter the Main VAR window when in online mode?

Another thing I've looked at is having an enum with the readable names and using this as an index into the array. The lookup is then MyArray[Enum.MyReadableName] :=... but I'm not sure if that is a good solution.

Any solutions or hints are very welcome! Thanks!


Solution

  • You have already mentioned all possible ways for this.

    Pointers

    You should not be worried of them. Pointers are not a part of IEC-61131 so it's implementation varies from manufacturer to manufacturer. It would be good if you've mention IDE you are using, and a structure of array elements.

    The best way how I work with tasks like this, I create ACTION and there I map all variables into arrays or out of arrays. I run this tasks only once on PLC load and call this action Mapping.

    VAR
        aA: ARRAY [1..2] OF StructureName;
        stMyName1: POINTER TO StructureName;
        stMyName2: POINTER TO StructureName;
        xInit: BOOL;
    END_VAR
    
    ACTION actMap
        stMyName1 := ADR(aA[1]);
        stMyName2 := ADR(aA[2]);
    END_ACTION
    
    IF NOT xInit THEN
        actMap();
        xInit := TRUE;
    END_IF
    

    But usually, in IDE actions are created differently, not with ACTION keyword. In Codesys it is right click on POU.

    I would go with pointers, because it is mot logical way. It requires little bit more for application setup, but later save time with coding.

    Enumeration

    This one as you described. In Codesys you should use # like Color#red. But if you make names unique, you can use them without name of enumeration. In addition if you make name of your array short it can looks informative like a[MyArrayName].