0

I'm using Wago PLC - PFC200 - for home automation. I already did most of the things like lights or blinds automation. Recently I decided to do some refactoring and I started to think that such PLC is not a PC with a garbage collector and it might be beneficial instead of passing POU's in the table pass pointers to those POU's.

I started with something like this:

Declaration:

VAR
    Lighs: ARRAY [1..siTotalLightsCount] OF Relay;
END_VAR

where Relay is my POU (program organization unit - like a class).

Initialization:

Lighs[1] := MainLightRelay;
Lighs[2] := WindowLightRelay;
Lighs[3] := IslandLightRelay;

Usage in POU method:

FOR siCurrent := 1 TO siTotalLightsCount DO
    Lighs[siCurrent].xCentralOff := xCentralOffSignal;
END_FOR 

But I might be beneficial to save some memory for variables and go with such an approach:

Declaration:

VAR
    Lighs: ARRAY [1..siTotalLightsCount] OF POINTER TO Relay;
END_VAR

Initialization:

Lighs[1] := ADR(MainLightRelay);
Lighs[2] := ADR(WindowLightRelay);
Lighs[3] := ADR(IslandLightRelay);

Usage in POU method:

FOR siCurrent := 1 TO siTotalLightsCount DO
    Lighs[siCurrent]^.xCentralOff := xCentralOffSignal;
END_FOR

Is that make sense? I had like 15 rooms in my home, most of the rooms has more than one light (so more Relay objects). It's not much, however, PLC is not PC, there are some memory constraints.

Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36
  • 1
    I do not think it is much improvement for such a PLC. May be one nanosecond in speed, few bytes in memory. That does not affect work. You can go either way. – Sergey Romanov Jan 27 '21 at 11:42
  • 1
    Alternatively, you could use Structures and Functions instead. They would use less memory overall (as far as I know). Also, I wouldn't use Pointers to FBs (Function Blocks), but instead define Interfaces and use them to pass FBs (they work something like References, which themselves are like Pointers) – Guiorgy Jan 28 '21 at 13:09
  • @Guiorgy I'm using interfaces for different things like room POU. I have IRoom interface which has methods to e.g.: turn off all lights. You suggest using `Lighs: ARRAY [1..siTotalLightsCount] OF IRelay` instead of `Lighs: ARRAY [1..siTotalLightsCount] OF Relay`. It seems to me that in both cases we are passing the same thing. – Dawid Rutkowski Jan 29 '21 at 10:12
  • 1
    @DawidRutkowski, others in the [CODESYS forum](https://forge.codesys.com/forge/talk/Engineering/thread/36f763cd5b/) recommend the use of interfaces. [Here's](https://help.codesys.com/api-content/2/codesys/3.5.16.0/en/_cds_obj_interface/) a quote from the codesys documentation: "CODESYS always treats variables declared with the type of an interface as references". Put simply, yes, you can use both, but using pointers goes against the OOP convention. 1 thing to be aware however is, that in case of an online change, POU may be moved to new memory location. Interfaces automatically correct that! – Guiorgy Jan 31 '21 at 14:12

0 Answers0