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:

    bFirstCycleExecuted : BOOL := FALSE;

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

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:

    fbGetCurTaskIndex : GETCURTASKINDEX;

IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN
    // The code that you want to be executed at the first cycle
  • Share on:

3 Comments, RSS

  1. Mark

    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 :

    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!



    • Jakob Sagatowski

      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!

      • Armando Narvaez

        Hi Jakob and Mark,

        One question,

        What would be the difference between using this approach for the first cycle against using the built in, at least in CODESYS, FB_INIT method?

        Apart from, when you are inheriting from another class implementing an FB_INIT and therefore you can not overwrite/overload it?

        Thanks and great blog!


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


This site uses Akismet to reduce spam. Learn how your comment data is processed.