问题描述
使用 dot
分配参数和作为数组放入功能块有什么区别?
Timer1(IN:=TRUE,PT:=T#2S);
IF Timer1.Q THEN
i:=i+1;
Timer1.IN:=FALSE;
END_IF
Timer2(IN:=TRUE,PT:=T#2S);
IF Timer2.Q THEN
j:=j+1;
Timer2(IN:=FALSE);
END_IF
预计 Timer1
会被此 Timer1.IN:=FALSE;
赋值重置,但什么也没发生,尽管它在代码中将 FALSE
显示为实时值!
任何帮助将不胜感激。
解决方法
只有两个字符会在结果之间产生巨大差异:()
。括号表示功能块被调用,表示执行了功能块的实现部分。
为了说明差异,让我制作一个示例功能块来展示差异。
示例
这里我定义了一个具有单个输入 Increment
和单个输出 Count
的功能块。每次调用功能块时,都会运行实现部分的代码。实现部分会将当前的 Count
增加 Increment
。
FUNCTION BLOCK FB_Counter
VAR_INPUT
Increment : UINT := 1;
END_VAR
VAR_OUTPUT
Count : UINT := 0;
END_VAR
// Implementation part
Count := Count + Increment;
让我在程序 counter
中创建此功能块的实例 Runner
,以便我们可以看到调用它时会发生什么。
PROGRAM Runner
VAR
counter : FB_Counter;
END_VAR
counter(); // Count is set to 1 since the function block implementation is called
counter.Increment := 2; // Increment is set to 2,Count is still at 1 since the implementation of the function block is not called.
counter(); // Count is set to 3 since the implementation of the function block is now executed
counter(Increment:=1); // Increment is set back to 1 and the function block is called again,increasing the Count to 4.
如果您使用断点单步执行上述代码,您可以看到每一步都发生了什么。