The is a simple but powerful tool for safe PLC initialization. Always use the system library version ( FB_FirstScan ), and remember: first scan ≠ warm start. Use it to enforce a clean startup state, especially after program downloads or power cycles.
VAR fbGetCurTaskIdx : GETCURTASKINDEX; // Fetches the current task's index bFirstScan : BOOL; // Your usable First Scan bit END_VAR // 1. Get the current task index fbGetCurTaskIdx(); // 2. Read the FirstCycle boolean from the task system info bFirstScan := _TaskInfo[fbGetCurTaskIdx.index].FirstCycle; Use code with caution. Copied to clipboard
Implementation examples (conceptual)
Because TwinCAT executes code in tasks, accessing the correct task index ensures your first scan logic fires reliably regardless of where it is called in your program. 1. The Code Structure
: Triggering a one-time read from a CSV file or database.
The Beckhoff First Scan bit is a small, easily overlooked tool that separates professional, robust PLC code from fragile, “works-most-of-the-time” logic. By taking explicit control of the first cycle, you eliminate startup surprises, protect hardware, and ensure your TwinCAT application starts every time in a predictable, safe state.
Understanding the First Scan Bit in Beckhoff TwinCAT In industrial automation, executing specific logic only once when the PLC switches from Stop to Run mode is a critical requirement. This initialization phase establishes safe initial states, resets counters, pre-loads configuration parameters, and clears temporary buffers.
METHOD FB_init : BOOL VAR_INPUT bInitRetains : BOOL; // If TRUE, retain variables are initialized bInCopyCode : BOOL; // If TRUE, instance was copied (e.g., during online change) END_VAR // Initialization code here nTargetVelocity := 1500; bEnableAxis := TRUE; Use code with caution. Why Use It?
Use a local BOOL := TRUE; variable and clear it at the end of the IF statement. Use TwinCAT_SystemInfoVarList._TaskInfo[x].CycleCount = 1 . HMI Safe-State
When you transition to Beckhoff TwinCAT, you won’t find a system bit named exactly "First Scan" in the global variable list. This often leads to the question: How do I run logic exactly once when the PLC starts?
Wiping the slate clean on startup so old errors don't prevent a start.
After that single cycle, it automatically returns to FALSE and stays FALSE until the next restart.
FirstScan gives you a deterministic moment to reset, preset, or home everything before normal operation begins.
The PlcTaskSystemInfo data type exposes a wealth of diagnostic and system-level data beyond just the FirstCycle flag. By exploring the _TaskInfo array, you can access incredibly useful runtime properties, including:
Proper utilization of the first scan bit is a hallmark of clean, structured Beckhoff programming. Common use cases include:
Avenida São Borja, n° 2181 - Prédio A - Fazenda São Borja - CEP: 93035-411
São Leopoldo - RS - Brasil
See on Map