变量初始化
所有声明的默认初始值是 “0”,但是可以在每个变量和数据类型的声明中添加用户自定义的初始值。
用户自定义初始化由赋值运算符“:=”来引入,并且可以是任何有效ST表达式ST语句。因此,恒定值,以及其它的变量或函数都可能用来定义一个初始值。程序员只是具有确保用于另一个变量的初始化的变量本身自己初始化。
有效变量初始化例子:
VAR |
|
var1:INT := 12; | (* Integer variable with initial value 12*) |
x : INT := 13 + 8; | (* init value defined by an expression with constants *) |
y : INT := x + fun(4); | (* init value defined by an expression containing a function call; be aware of the order in this case! *) |
z : POINTER TO INT := ADR(y); | (* not described by the IEC61131-3: init value defined by a address function; Be careful in this case: the pointer will not be initialized during online change! *) |
END_VAR |
|
数组的完整初始化例子:
arr1 : ARRAY [1..5] OF INT := [1,2,3,4,5];
arr2 : ARRAY [1..2,3..4] OF INT := [1,3(7)]; (* short for 1,7,7,7 *)
arr3 : ARRAY [1..2,2..3,3..4] OF INT := [2(0),4(4),2,3];
(* short for 0,0,4,4,4,4,2,3 *)