First Scan Bit | Beckhoff
The most robust way is to use the PlcTaskSystemInfo structure, which contains a FirstCycle boolean. This bit is only during the very first execution of that specific task after the TwinCAT runtime starts. Implementation Example (Structured Text):
IF TwinCAT_SystemInfoVarList._FirstScan THEN // One-time actions END_IF
Keeps initialization logic tightly coupled with the object.
METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL; // if TRUE, the retain variables are initialized bInCopyCode : BOOL; // if TRUE, the instance afterwards gets moved into the copy code END_VAR Use code with caution. beckhoff first scan bit
| Variable | Data type | Description | | :--- | :--- | :--- | | active | BOOL | TRUE if the task is active. | | taskName | STRING(16) | The name of the task. | | | BOOL | TRUE only on the first scan cycle after system startup; FALSE thereafter. | | cycleTimeExceeded | BOOL | TRUE if the task's execution time exceeds the configured cycle time. | | cycleTime | UDINT | The configured cycle time of the task (in 100 ns increments). | | lastExecTime | UDINT | The execution time of the task during the previous cycle (in 100 ns increments). | | priority | BYTE | The priority of the task. | | cycleCount | UDINT | The number of completed cycles since the task started. |
PROGRAM MAIN VAR fbFirstScan : FB_FirstScan; bInitDone : BOOL; END_VAR
The is a system-generated boolean flag that is TRUE for exactly one PLC cycle after: The most robust way is to use the
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.
This ensures that regardless of any retained data from previous runs, the machine will always begin its operation logic from a defined starting point.
FB_Init : InitFB; // InitFB runs once, sets InitDone BOOL when finished IF InitDone THEN // normal operation ELSE // hold outputs safe END_IF METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL;
When the TwinCAT PLC runtime transitions to , memory is allocated and bFirstScan is initialized to TRUE .
By properly implementing the first scan concept in TwinCAT, you ensure that your Beckhoff industrial PCs boot safely, predictably, and seamlessly transition into active machine control.
→ Counter increments by 1
: The timer’s output starts FALSE . On the first cycle, IN is TRUE , but the timer hasn't elapsed, so Q remains FALSE . Thus bFirstScan = TRUE . On the second cycle, Q becomes TRUE , IN becomes FALSE , and bFirstScan becomes FALSE permanently.
An compiles only the modified code without stopping the PLC.