英文:
CODESYS, TON not resetting
问题
我已经定义了INT和TON
VAR
state : INT := 0;
timer_on : TON;
END_VAR
现在,程序如下
IF (state = 0) THEN
timer_on(IN := TRUE, PT := T#5s);
IF (timer_on.Q = TRUE) THEN
timer_on.IN := FALSE;
state := 1;
END_IF
END_IF
当执行后,状态在5秒后变为0。这没问题。但是当我把状态改回0时,它立即变为1。根据文档
> 当IN为TRUE且ET等于PT时,Q为TRUE。否则为FALSE。
在我的情况下,5秒后Q总是为TRUE,即使IN为FALSE。
英文:
I have an INT and TON defined
VAR
state : INT := 0;
timer_on : TON;
END_VAR
Now. The program is like this
IF (state = 0) THEN
timer_on(IN := TRUE, PT := T#5s);
IF (timer_on.Q = TRUE) THEN
timer_on.IN := FALSE;
state := 1;
END_IF
END_IF
When it is executed state is changed to 0 after 5 seconds. That's ok. But when I change state back to 0 it immediately goes back to 1. According to the documentation
> Q is TRUE when IN is TRUE and ET is equal to PT. Otherwise it is FALSE.
In my case, after 5 seconds Q is always TRUE even when IN is FALSE
答案1
得分: 2
更改函数块的输入变量不会执行函数块。您需要运行它才能看到更改。
当您运行程序时会发生以下情况:
- 状态为0,因此首个IF被执行。
- timer_on被启动,持续5秒。
- 5秒后,状态仍为0且timer_on.Q变为true,因此第二个IF也被执行。
- timer_on.IN被设置为false,但timer_on没有被执行,状态也被设置为1。
- 状态为1,所以代码被跳过。
- 在手动将状态设置为0后,首个IF被执行。timer_on以IN为true运行,但因为这个计时器从未被重置,它会立即完成。
解决方案是将timer_on.IN := FALSE;
这行代码更改为timer_on(IN := FALSE);
。这将以IN为false运行计时器,正如文档所述,这将为下一次使用重置它本身。
英文:
Changing the input variables of a function block does NOT execute the function block. You need to run it to see the change.
What happens when you run your program:
- state is 0, so first IF is entered
- timer_on is started for 5 seconds
- after 5 seconds, state is still 0 and timer_on.Q becomes true, so second if is also entered.
- timer_on.IN is set to false, but timer_on is not executed, state is also set to 1
- state is 1 so code is skipped
- after state is set to 0 manually, the first if is enered. timer_on is run with IN true, but because this timer was never reset, it immidiately finishes.
The solution is to change the timer_on.IN := FALSE;
line to timer_on(IN := FALSE);
. This will run the timer with IN false which, as the documentation states, will reset itself for the next use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论