创建定时循环结构化文本

电动雷81

我试图创建一个有时间延迟的 FOR TO 循环。但它似乎无法正常工作我无法弄清楚错误在哪里,但似乎循环运行得很彻底并且没有考虑时间延迟。

FOR vCount := 0 TO 800 DO

fbBlink(ENABLE := TRUE, TIMELOW := T#1s, TIMEHIGH := T#1s, OUT => bSignal);

    IF bSignal THEN
        vVsample[vCount] := INT_TO_REAL(WORD_TO_INT(vVin));
    END_IF

END_FOR

所以我希望这个循环会占用 801 秒。因此,每一秒都会将一个新值添加到数组中。但是当我在很短的时间(例如 1 秒)后运行代码时,我在数组中已经有了 801 个值,所以计时器不起作用。

我已经尝试过使用 TON 的代码,现在我在这个论坛上找到了 FB Blink,但它仍然不起作用。

谁能帮帮我

吉奥吉

您似乎对 CODESYS 如何运行代码有误解。与代码从上到下执行一次的 C/C++ 等语言不同,在 codesys 中,只要控制器有电,代码每秒就会执行多次(在我工作的 PLC 上,它每 20 毫秒执行一次,或者换句话说,每秒 50 次)。

因此,您不能拥有停止/延迟执行的功能。计时器在 CODESYS 中的实际作用是记住您第一次调用它们的时间,然后在每次调用时它们只检查经过了多少时间(它们不会停止!)。一旦设定的时间过去,他们会在他们的输出上提出一个标志,这样你就可以执行一些任务。

因此,让我们分解运行代码时发生的情况:

// first run:
FOR vCount := 0 TO 800 DO
    // on first itewration (vCount = 0) this will remember the current time,
    // however it won't stop here and it will continue to the next line.
    // on next iterations (vCount >= 1) the timer will check the elapsed time,
    // which will be 0, so it will do nothing and continue to the next line.
    fbBlink(ENABLE := TRUE, TIMELOW := T#1s, TIMEHIGH := T#1s, OUT => bSignal);
    // the timer just started so bSignal is FALSE
    IF bSignal THEN
        vVsample[vCount] := INT_TO_REAL(WORD_TO_INT(vVin));
    END_IF
END_FOR

// second run:
FOR vCount := 0 TO 800 DO
    // the timer will compare current time to the recorded first time.
    // the elapsed time is below the target, so skip to the next line.
    // this will be repeated 800 times for no good reason...
    fbBlink(ENABLE := TRUE, TIMELOW := T#1s, TIMEHIGH := T#1s, OUT => bSignal);
    // the timer hasn't reached target time so bSignal is FALSE
    IF bSignal THEN
        vVsample[vCount] := INT_TO_REAL(WORD_TO_INT(vVin));
    END_IF
END_FOR
// the above will reapeat for another 48 cycles

// 50th run:
FOR vCount := 0 TO 800 DO
    // the timer will compare current time to the recorded first time.
    // the elapsed time has reached the target 1 second (TIMELOW := T#1s),
    // so it will raise the OUT flag to TRUE.
    // the comparison will happen on every 800 iterations, and every time
    // it will be set to TRUE.
    fbBlink(ENABLE := TRUE, TIMELOW := T#1s, TIMEHIGH := T#1s, OUT => bSignal);
    // since bSignal is TRUE on every iteration, ener the if for every element.
    IF bSignal THEN
        vVsample[vCount] := INT_TO_REAL(WORD_TO_INT(vVin));
    END_IF
END_FOR

如果您想要每秒向数组添加一个值,那么您可以这样做(请注意,这不会停止/停止/延迟执行,并且此后的代码仍将在每次运行时执行.如果这是不受欢迎的,你必须单独处理):

timer(IN := vCount <> 801, PT := T#1S); // timer is TON
IF (timer.Q) THEN
    timer(IN := FALSE); // reset the timer so it starts counting again nextr run
    vVsample[vCount] := INT_TO_REAL(WORD_TO_INT(vVin));
    vCount := vCount + 1; // increment the counter
END_IF

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章