0

How do you group variables in Structured Text?
Say I have n global variables for lamps:

lamp1
lamp2
lamp3
... // and so on

Then I have a button, and pressing it should set all variables to TRUE:

IF buttonPressed Then
    lamp1 := TRUE;
    lamp2 := TRUE;
    lamp3 := TRUE;
    ... // and so on
END_IF

How can I group the lamps in a way to not to have to set every varriable to TRUE manually?

Guiorgy
  • 1,405
  • 9
  • 26
Kai Lee
  • 3
  • 2
  • Hi Kai Lee, welcome to StackOverflow! You can format your code by putting it in between triple quotes ```, see [here](https://stackoverflow.com/editing-help#code) for an example. Your title can be improved a bit. Maybe a better title would be "How can I set the same value for multiple variables with a single command in structured text?", if I understood the question correctly. – Roald Dec 10 '21 at 08:37

3 Answers3

1

Codesys has a very good example that is in the internet in many places of why programming in OOP makes tasks like this much easier. Instead of thinking of a lamp as a variable, think of it as an object (I.e. function block).

Create an interface called ILamp and have two methods: TurnOn and TurnOff. Then create a FB called FbLamp that implements ILamp.

You can then create arrays of type ILamp and you can put your lamp objects in that array. From there you can interrate through the array like Roald’s answer. But I prefer to create a master AllLamps object that also implements ILamp. Then when you call AllLamps.TurnOn() then AllLamps is programmed to iterate through all Lamp objects and call TurnOn.

Scott
  • 116
  • 2
0

To set multiple variables at once, you would first have to collect the values you want to set in an array:

VAR 
    lamp1 : BOOL;
    lamp2 : BOOL;
    lamp3 : BOOL;
    lamps : ARRAY[1..3] OF BOOL := [lamp1, lamp2, lamp3];
END_VAR

and then set the values in a for loop.

FOR i := 1 TO 3 DO
    lamps[i] := TRUE;
END_FOR

Result

enter image description here

Function

You can also define a custom function if you have to do it a lot:

FUNCTION SetAllBools : BOOL
VAR_IN_OUT
    bools : BOOL;
END_VAR
VAR_INPUT
    newValue : BOOL;
END_VAR
VAR
    i : INT;
END_VAR

which can then be used as SetAllBool(lamps, TRUE);.

Roald
  • 2,459
  • 16
  • 43
0

To correct the answer by @Roald:

If what you want is to modify the original values and not the copies that are in the ARRAY, then either use an array to begin with:

VAR 
    // lamp1 : BOOL; lamp2 : BOOL; lamp3 : BOOL;
    lamps : ARRAY[1..3] OF BOOL; // use "lamps[1]" instead of "lamp1" everywhere
END_VAR

Or instead of adding copies to the ARRAY, add their POINTERs:

VAR 
    lamp1 : BOOL;
    lamp2 : BOOL;
    lamp3 : BOOL;
    lamps : ARRAY[1..3] OF POINTER TO BOOL := [ADR(lamp1), ADR(lamp2), ADR(lamp3)];
END_VAR

This way if you modify any value in the ARRAY the original also changes (though don't forget to dereference by using ^ when modifying the ARRAY elements!):

FOR i := 1 TO 3 DO
    lamps[i]^ := TRUE;
END_FOR
Guiorgy
  • 1,405
  • 9
  • 26