First cycle

One problem I face quite often is that I need to have some code executed only once in the first cycle of the PLC, and then never again. Up until now I’ve always used a boolean of some sort; “bFirstCycleExecuted”, instantiated to false and then set to true after the first cycle resulting in something like:

PROGRAM MAIN
VAR
    bFirstCycleExecuted : BOOL := FALSE;
END_VAR

IF NOT bFirstCycleExecuted THEN
    // The code that you want to be executed at the first cycle
    bFirstCycleExecuted := TRUE;
END_IF

There actually is a built-in way in TwinCAT to know whether the current cycle is the first one or not, using the global data type PlcTaskSystemInfo. Among other parameters in this data type is the boolean FirstCycle. In your program you have access to an array of the data type PlcTaskSystemInfo, accessing it by _TaskInfo[index_of_current_task]. The index of current task can be retrieved by using the function block GETCURTASKINDEX. Replacing the above piece of code with our new knowledge results in:

PROGRAM MAIN
VAR
    fbGetCurTaskIndex : GETCURTASKINDEX;
END_VAR

fbGetCurTaskIndex();
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN
    // The code that you want to be executed at the first cycle
END_IF

2 Comments, RSS

  1. Mark 2017-11-10 @ 09:04

    Hi Jakob, great blog, a friend put me onto this!

    I wrote a little class that wraps some of PlcTaskSystemInfo for easier access. see the T_Task class in my open source library : https://bitbucket.org/Intecre/intecrelibs.

    There’s also some work on a test base class in there – I see you’re also interested in TDD.

    All pull requests / comments welcome!

    Cheers,

    Mark

    • Jakob Sagatowski 2017-11-11 @ 12:02

      Hi Mark, thanks for your comments! Really neat to see that you’ve developed a library, had completely missed this one. Quickly looked into it, and saw that you’ve off with a good start with TDD FB’s. Noticed that you’re using the pragmas “instance-path” and “reflection”, which seem interesting!

Your email address will not be published. Required fields are marked *

*