0

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!

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
krakers
  • 111
  • 10

1 Answers1

0

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

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
  • Actually the third way is to use reference, not pointer. I would suggest that if it is possible. As far as I know, they are safer to use and references can also be cheched if they are valid by using `__ISVALIDREF`. But naturally pointers work too IF you remember to initialize them every cycle, or at least every now and then. If you assign a pointer ONLY at PLC start, they might cause PLC exceptions after online change. – Quirzo Apr 18 '18 at 07:05
  • @Quirzo pointers is reference. It is the same tерштпюthere is not other way to create reference in ST. – Sergey Romanov Apr 19 '18 at 10:30
  • Well, yes, `REFERENCE` uses pointers behind the curtains. But for programmer, it is not a pointer. When using a reference, you dont need ^ to access the variable (with pointers you have to). And in addition, you can check validity by the `__ISVALIDREF` function. More info: [Beckhoff](https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/18014401038940811.html&id=1281794799891646250) – Quirzo Apr 19 '18 at 12:02
  • Thanks, both of you. Are references updated automatically at online change? And are references set to 0 if they are no longer valid, or just before the first use? I'll wait a few more days, then I'll select your answer Sergey. – krakers Apr 19 '18 at 15:33
  • @krakers references are not online. I mean they are online but that is incorrect term. When you create `point to` your variable refer to the same memory location of variable, as original variable. It is like you have 2 variable but actually point to the same value. – Sergey Romanov Apr 19 '18 at 15:49
  • I have answered that in [other question](https://stackoverflow.com/a/45792182/8140625). As far as I know, references are updated automatically, at least in TwinCAT so it should be in Codesys too. But for safety, I would update references at start of every cycle, but that's probably not needed. – Quirzo Apr 20 '18 at 05:15
  • For what its worth, I decided to go the enum route. It feels more transparent for my use case, and no initialization/updating of pointer/references. And I really like the warning if two keys have the same integer. – krakers Apr 20 '18 at 20:11
  • @krakers if you use enum it will never create 2 keys with same ID (integer) – Sergey Romanov Apr 22 '18 at 05:50