2

The following works fine:

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [1,2,3,4,5,6,7,8,9,10,11,12];
        i : INT;
        j : INT;
    END_VAR

    // change to random values
    FOR i:=0 TO 1 DO
        FOR j:=0 TO 5 DO
            MyArray[i,j] := i+j;
        END_FOR
    END_FOR

    // Or individualy set numbers
    MyArray[0,1] := 56;
    MyArray[0,4] := 156;
END_PROGRAM

But what if I would like to modify all values in a single line of code?

i.e. the following is pseudo code of what I would like to do. (Note, it does not actually work)

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [1,2,3,4,5,6,7,8,9,10,11,12];
        bChange : BOOL;
    END_VAR

    IF bChange THEN
        MyArray := [1,58,3,53,5,6,128,8,9,10,20,12];
    END_IF
END_PROGRAM
tomatoeshift
  • 333
  • 1
  • 10

2 Answers2

1

You need to program a function in order to simplify the assignment of the array-values.

Function declaration:

FUNCTION F_SetArrayValues : ARRAY[0..1,0..5]OF USINT
VAR_INPUT
     v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12 : USINT;    
END_VAR

Function implementation:

F_SetArrayValues[0,0] := v1;
F_SetArrayValues[0,1] := v2;
F_SetArrayValues[0,2] := v3;
F_SetArrayValues[0,3] := v4;
F_SetArrayValues[0,4] := v5;
F_SetArrayValues[0,5] := v6;
F_SetArrayValues[1,0] := v7;
F_SetArrayValues[1,1] := v8;
F_SetArrayValues[1,2] := v9;
F_SetArrayValues[1,3] := v10;
F_SetArrayValues[1,4] := v11;
F_SetArrayValues[1,5] := v12;

You call the function like this:

IF bChange THEN
    MyArray := F_SetArrayValues(2,3,4,5,6,7,8,9,10,11,12,13);
END_IF
Filippo Boido
  • 950
  • 5
  • 11
  • Ok, I was hoping there was a simpler way. I thought that I had some syntax error in my provided example. Fortunately, I don't have to rewrite the array so many times in the code, so I think I will skip implementing a own function for it. – tomatoeshift Apr 08 '20 at 12:22
0

Only during initialization.

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [
            [1,2,3,4,5,6],
            [1,2,3,4,5,6]
        ];
    END_VAR
END_PROGRAM

This will not be permitted

IF bChange THEN
    (* еhis will fial *)
    MyArray := [
            [1,2,3,4,5,6],
            [1,2,3,4,5,6]
        ];
END_IF
Sergey Romanov
  • 2,580
  • 2
  • 21
  • 37
  • I noticed that it was not permitted. I was hoping there was a simple solution for this, but it seems like a) make a function for it as in the other answer or b) individually set all desired elements. – tomatoeshift Apr 09 '20 at 11:26
  • FYI, your syntax `MyArray : ARRAY[0..1,0..5]OF USINT := [[1,2,3,4,5,6],[1,2,3,4,5,6]];` will give an error in my IDE. I am using B&R's AutomatioStudio, so the only correct syntax according to its compiler is `MyArray : ARRAY[0..1,0..5]OF USINT := [1,2,3,4,5,6,7,8,9,10,11,12];` – tomatoeshift Apr 09 '20 at 11:32
  • Right, the implementation of ST is different from IDE to IDE. I referred to CoDeSys 3.5. Did you use my VS Code extension for ST code? People say it is good for B&R. – Sergey Romanov Apr 10 '20 at 10:43
  • I not sure that I understood your question, but I [assume you mean this.](https://marketplace.visualstudio.com/items?itemName=Serhioromano.vscode-st). I am not using Visual Studio. Should I start using it? – tomatoeshift Apr 11 '20 at 05:57
  • Yes, this is my extension. Some people who work with B&R in the review say that this extension is good for that. You can try to open your project home with VS Code IDE and see how it looks. – Sergey Romanov Apr 12 '20 at 15:36
  • I briefly tried your extension. color codings do look nice. One thing that is an advantage with B&R's automation studio, is that I can right-click on any variable/ type/ function/ action and choose to see its definition and type. I.e. the IDE then goes to that location. I know the same is possible with VS code, Arduino extension, so maybe it is some further implementation you could include in your extension – tomatoeshift Apr 12 '20 at 19:20
  • Yes, it is planned. It is called LSP (Language Service Protocol). I plan to create fully-fledged ST IDE as I am also developing Linux based soft PLC (Runtime). Ultimately user will be able to write ST code with my extension and compile it to run on Linux machine or PLC. – Sergey Romanov Apr 14 '20 at 06:26
  • 1
    Sounds interesting! But also a lot of work needed to be done :) . The best of luck to you. – tomatoeshift Apr 14 '20 at 06:36