Beckhoff First Scan Bit
In Beckhoff TwinCAT (2 and 3), there is no single "magic" global bit like the S:FS in Allen-Bradley . Instead, you can access the "First Scan" status through built-in system variables or by creating a custom initialization flag. 1. Using Built-in System Info ( FirstCycle )
: Ensures equipment begins in a safe, known state.
IF SystemTaskInfoArr[1].firstCycle THEN // Perform one-time initialization code here bInitializationDone := FALSE; // ... other one-time tasks ... bInitializationDone := TRUE; END_IF
Populating operational variables with safe baseline data before the operator inputs custom values. beckhoff first scan bit
PROGRAM MAIN VAR fbGetCurTaskIndex : GETCURTASKINDEX; // Function block to get task index nTaskIndex : UINT; // Variable to store the index bIsFirstScan : BOOL; // Flag indicating the first scan // Example application variables nSystemInitCount : INT; // Tracks how many times init is run END_VAR // 1. Call the function block to get the index of the currently executing task fbGetCurTaskIndex(); nTaskIndex := fbGetCurTaskIndex.index; // 2. Read the system info for this specific task bIsFirstScan := _TaskInfo[nTaskIndex].FirstCycle; // 3. Execute First Scan Logic IF bIsFirstScan THEN // Place your startup and initialization code here nSystemInitCount := nSystemInitCount + 1; END_IF Use code with caution. 2. How the Code Works
Method 1: The Standard Structured Text Pattern (Recommended)
: Use bInit in FB_Init – it respects online changes differently. Or explicitly handle a "reinit" via a variable that you toggle manually. In Beckhoff TwinCAT (2 and 3), there is
Because the variable is declared with RETAIN , it will preserve its FALSE state across subsequent program starts, until a full power cycle. If you need it to trigger on every STOP->RUN transition, you would declare it without the RETAIN keyword, which forces a TRUE initialization on each warm start.
// -- First scan detection -- fbFirstScan(CLK := bInit); IF fbFirstScan.Q THEN bFirstScanDone := FALSE;
Even experienced programmers can make mistakes. Here are some pitfalls to watch out for. Using Built-in System Info ( FirstCycle ) :
Explain how to implement a to delay initialization.
// Disable drives driveEnable := FALSE;
Unlike some traditional PLCs (like Allen-Bradley or Siemens) that provide a dedicated, built-in system variable for this purpose out of the box, Beckhoff’s TwinCAT environment handles this differently. Because TwinCAT is based on the IEC 61131-3 standard, developers have a few explicit, flexible methods to detect and utilize the first execution cycle. Method 1: Using the Standard System Variable ( PLC_STARTUP )
Note: It is worth highlighting that restarting or stopping/starting the PLC code via the engineering UI may not always toggle this bit. It strictly relies on the full initialization of the TwinCAT runtime system.
IF bIsFirstScan THEN // Your one-time initialization code bIsFirstScan := FALSE; // Flag is cleared for future scans END_IF