In Beckhoff TwinCAT, the equivalent of a "first scan bit" is the firstCycle variable found within the system task information. This bit is automatically set to TRUE only during the very first execution cycle of a PLC task, making it ideal for one-time initialization logic. How to Access the First Scan Bit
You can access this feature through the implicit _TaskInfo array, which contains data for every task running in your PLC project. Syntax (Structured Text):
IF _TaskInfo[GETCURTASKINDEXEX()].firstCycle THEN // Your initialization code here (e.g., setting default parameters) END_IF Use code with caution. Copied to clipboard Key Characteristics
Automatic Reset: The system automatically resets this bit to FALSE after the first cycle completes.
Task-Specific: Because it is part of the task info structure, it correctly identifies the first scan for the specific task it is called within.
Reliability: It is more robust than manual "first scan" flags (like using a boolean that you set to false at the end of the code), as the PLC runtime handles its state directly. Usage Example
Commonly used to load recipes, initialize communication drivers, or reset state machines to their starting position upon a PLC cold or warm start. If you'd like, I can show you: beckhoff first scan bit
How to manually create a first-scan bit if you're using an older version of TwinCAT.
How to use it to initialize persistent variables from a file.
The difference between a cold start and a warm start in relation to this bit. Beckhoff CX1010 first scan | PLCtalk - Interactive Q & A
Here’s a concise guide to the First Scan Bit in Beckhoff TwinCAT (IEC 61131-3).
During the first scan, you typically want to:
Without a proper first scan routine, your machine might start with actuators in unpredictable positions or unfinished states from a previous run. In Beckhoff TwinCAT, the equivalent of a "first
Note: exact symbol names can vary by TwinCAT version and project conventions.
In TwinCAT, the PROGRAM or FUNCTION_BLOCK structure has a specific order of execution. The VAR section declares variables, but it doesn't execute logic. To run logic on the first scan, you declare a boolean flag and check its state.
The Code:
PROGRAM MAIN VAR bFirstScan : BOOL := TRUE; (* Initialize as TRUE *) nCounter : INT; END_VAR(* Logic Section ) IF bFirstScan THEN ( --- This runs ONLY on the first scan --- ) nCounter := 0; ( Reset variables ) ( Perform other startup routines *)
bFirstScan := FALSE; (* Set to false so this never runs again *)END_IF
(* Your regular cyclic code runs here *) nCounter := nCounter + 1;Without a proper first scan routine, your machine
VAR RETAIN bInitialized : BOOL; END_VAR VAR bFirstScanSys : BOOL; END_VARbFirstScanSys := TwinCAT_SystemInfoVarList._FirstScan;
IF bFirstScanSys AND NOT bInitialized THEN // Run once in device lifetime bInitialized := TRUE; END_IF
If you set outputs on the first scan before the EtherCAT bus is fully operational (state OP), your writes may be ignored or cause errors. Always wait for EtherCAT Master State = OP before critical I/O initialization.
IF fbFirstScan() AND (nEtherCATState = 8) THEN // 8 = OP
EnableOutputs();
END_IF
The First Scan Bit is a Boolean flag that is TRUE only for a single PLC cycle immediately after the runtime system starts executing the application. On the second cycle, it becomes FALSE and remains FALSE until the next controller reboot or program download.