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