2

以下工作正常:

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

但是,如果我想在一行代码中修改所有值怎么办?

即以下是我想做的伪代码。(注意,它实际上不起作用)

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
4

2 回答 2

1

您需要编写一个函数以简化数组值的分配。

函数声明:

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

功能实现:

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;

你这样调用函数:

IF bChange THEN
    MyArray := F_SetArrayValues(2,3,4,5,6,7,8,9,10,11,12,13);
END_IF
于 2020-04-08T09:47:53.717 回答
0

仅在初始化期间。

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

是不允许的

IF bChange THEN
    (* еhis will fial *)
    MyArray := [
            [1,2,3,4,5,6],
            [1,2,3,4,5,6]
        ];
END_IF
于 2020-04-09T10:25:40.600 回答