PROGRAM Example VAR FirstScan : BOOL; END_VAR
TwinCAT provides a global structure called TwinCAT_SystemInfoVarList which contains runtime metrics about the PLC. Within this structure, you can access the current cycle counter of the task executing the code.
VAR fbGetCurTaskIdx : GETCURTASKINDEX; // Function block to find the current task ID bFirstScan : BOOL; END_VAR fbGetCurTaskIdx(); // Call the FB // Access the system's internal task info array bFirstScan := _TaskInfo[fbGetCurTaskIdx.index].FirstCycle; IF bFirstScan THEN // Insert initialization logic here END_IF Use code with caution. Copied to clipboard
The first scan bit is a fundamental concept for writing robust and predictable PLC programs in Beckhoff TwinCAT. Whether you choose the system variable SystemTaskInfoArr[1].firstCycle for standard cold-start initialization or a custom RETAIN flag for more control, understanding this feature will help you build more reliable automation systems.
In PLC programming, ensuring that a system starts in a known state is critical for safety and process reliability. The (sometimes called First Cycle bit) is a special bit that is TRUE only during the very first scan of a PLC program after powering on or transitioning from STOP to RUN mode. beckhoff first scan bit
PROGRAM MAIN VAR fbSystemFirstScan : FB_FirstScan; bGlobalFirstScan : BOOL; END_VAR
Here’s a technical feature article exploring the in Beckhoff TwinCAT systems.
Do not place complex, time-consuming code within the IF FirstScan block. The first cycle should be fast to avoid triggering watchdog errors.
: This bit usually only triggers when the TwinCAT Runtime is started or restarted. Simply stopping and starting the PLC code with the "Start/Stop" commands in the IDE may not reset this bit. 2. Custom Initialization Variable PROGRAM Example VAR FirstScan : BOOL; END_VAR TwinCAT
METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL; // IF TRUE, the retain variables are initialized bInCopyCode : BOOL; // IF TRUE, the instance afterwards is moved into copy code END_VAR // Any code placed here executes once before the main PLC scan starts. // Ideal for calculating static constants, initializing pointers, or clearing arrays. Use code with caution. Common Use Cases for the First Scan Bit
For advanced TwinCAT 3 users working with Function Blocks, the most elegant and robust "first scan" is not a bit at all—it's the object constructor: FB_Init .
: Use bInit in FB_Init – it respects online changes differently. Or explicitly handle a "reinit" via a variable that you toggle manually.
Resetting R_TRIG or F_TRIG function blocks so they do not inadvertently trigger on the very first cycle due to stale data variables stored in RETAIN memory. Crucial Pitfalls to Avoid 1. Online Changes vs. Cold Restarts Copied to clipboard The first scan bit is
To make your machine's startup routine bulletproof, consider these best practices.
Clear temporary, non-latching alarms on power-up. 3. How to Detect the First Scan in TwinCAT 3
for function block-specific initialization. These approaches enable reliable initialization of communication, variables, and logging upon system startup. AllTwinCAT First cycle - AllTwinCAT