×
MindLuster Logo

Beckhoff First Scan Bit

The is a boolean variable that is TRUE only during the very first cycle of the PLC program after transitioning from Stop to Run mode [1, 2]. Immediately after that first cycle completes, the bit automatically turns FALSE and remains that way until the PLC is stopped and restarted [1]. Why Do You Need It?

METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL; bInCopyCode : BOOL; END_VAR

The most robust, precise, and professional way to find out if the runtime is on its very first loop execution is by tapping into TwinCAT's built-in task system structures. beckhoff first scan bit

VAR bFirstScan : BOOL := TRUE; // Initializes to TRUE on startup bSystemReady : BOOL; nInitializationCounter : INT; END_VAR // PLC Logic IF bFirstScan THEN // 1. Initialize system parameters nInitializationCounter := 0; bSystemReady := FALSE; // 2. Reset the first scan bit so this block never runs again bFirstScan := FALSE; END_IF // Normal cyclic PLC logic continues here Use code with caution. How It Works

FB_init is a specialized method that executes the first cycle of the PLC task. It runs during the transition from Stop to Run or during an Online Change. How to use FB_init: Right-click your Function Block in the solution tree. Select Add > Method . The is a boolean variable that is TRUE

In this example, the FirstScan system variable is used to execute an initialization code segment during the first scan cycle. Once the initialization is complete, the FirstScan bit is reset to FALSE.

The IF bFirstScan block should only contain variables that need to be set once. Do not put complex, long-running calculations inside it, as this can increase the scan time of the very first cycle. METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL;

: If you call the program multiple times (e.g., as an action or method), the INIT behavior may change. Use it carefully within the main cyclic task.

For modern TwinCAT 3 development utilizing Object-Oriented Programming (OOP), using a simple boolean flag inside a Function Block (FB) is often inefficient. Instead, Beckhoff supports the implicit FB_init method. Implementation

In the world of industrial automation and TwinCAT programming, ensuring a PLC program initializes correctly is crucial. Whether you are setting initial positions, resetting counters, or activating safety interlocks, you need a way to execute code only once—the very first time the PLC runs.