// Rest of the user program... END_PROGRAM
This public link is valid for 7 days and shares a thread, including any personal information you added. This link or copies made by others cannot be deleted. If you share with third parties, their policies apply. Can’t copy the link right now. Try again later.
Have a specific first scan challenge? Visit the Beckhoff Community forums or consult your local Beckhoff support engineer.
Maintained directly by the TwinCAT real-time kernel.
⚠️ Caution : This resets on warm start. For cold start retention, use VAR RETAIN . beckhoff first scan bit
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.
A common point of confusion during commissioning revolves around what actually triggers a "first scan" in TwinCAT. _TaskInfo[x].FirstCycle Behavior Custom Software Flag Behavior (Switching Config -> Run) Triggers TRUE Triggers TRUE Cold Reset / PLC Download Triggers TRUE Triggers TRUE Stop & Start PLC Code (Red Square / Green Triangle in VS) Remains FALSE Triggers TRUE (Variables re-initialize) Online Change (Modifying code on the fly) Remains FALSE Remains FALSE (Memory is preserved) The Stop/Start Runtime Trap
During the very first scan, some hardware modules, fieldbus nodes (EtherCAT distributed clocks), or pointers linked via ADR() might still be establishing stable states. Avoid deep pointer referencing or complex string manipulations inside the first scan block unless you have explicitly verified that memory handles are valid. 2. Keep Axis Initialization Sequential
Instead of an absolute global bit, TwinCAT provides an array of structures named _TaskInfo . This array tracks the real-time performance and diagnostics of every active PLC task running inside the TwinCAT execution environment. Within the PlcTaskSystemInfo structure , Beckhoff natively updates a boolean property called FirstCycle . During the first-ever execution cycle of that specific task, this variable is set to TRUE ; on every subsequent cycle, it automatically remains FALSE . // Rest of the user program
If you use a manual first scan bit, ensure it is set to FALSE at the very end of your program. If you do it at the top, the rest of your logic won't see the TRUE state.
The First Scan Bit is a system-defined boolean variable that is TRUE only during the very first PLC scan after a TwinCAT runtime startup or program download. On the second scan and all subsequent scans, the bit is FALSE . Why Use the First Scan Bit?
In TwinCAT, you can run multiple independent tasks at different cycle times (e.g., a 1ms motion control task and a 50ms visualization/HMI task). A single global first scan bit would fail because different tasks start at different times or may be restarted independently. Therefore, initialization logic in TwinCAT must be managed at the application, task, or function block level. Method 1: The Local Variable Approach (Recommended)
Unlike some traditional PLCs (like Allen-Bradley’s S:FS bit) that have a predefined system variable, Beckhoff’s TwinCAT allows for several ways to achieve this functionality depending on your version and preference. Methods to Implement First Scan in TwinCAT 1. Using the TwinCAT System Info (The Pro Way) If you share with third parties, their policies apply
a global or local variable: bInitialized : BOOL := FALSE; Logic :
mapped to a retentive or standard flag named bInitDone .
BEGIN IF FirstScan THEN // Execute initialization code here // e.g., set default values, initialize variables FirstScan := FALSE; END_IF
// In your implementation section bFirstScan := NOT rst; rst := TRUE;
When the TwinCAT runtime enters Run Mode, bInitialized is allocated memory and set to FALSE . The IF NOT bInitialized condition evaluates to TRUE .