Scan Bit — Beckhoff First
In legacy TwinCAT 2 and early TwinCAT 3 projects using the Tc2_Standard library, the standard way to get a first scan bit is:
PROGRAM MAIN VAR fbFirstScan : TON; bFirstScan : BOOL; END_VAR
// Implementation fbFirstScan(IN := NOT fbFirstScan.Q, PT := T#1MS); bFirstScan := NOT fbFirstScan.Q;
How it works: 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.
Caveat: This method is cycle-dependent. If your cycle time is 10ms, set PT to at least 1ms — but ensure it's longer than one cycle but shorter than two.
The Beckhoff First Scan Bit 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.
Have you encountered any unexpected behavior with first scan in TwinCAT? Let me know in the comments — I’ve debugged many tricky startup issues and can help!
In Beckhoff TwinCAT, there is no single global system bit like the Allen-Bradley S:FS. Instead, you use task-specific system information or a manual initialization variable. 1. Built-in FirstCycle Bit
The most robust way is to use the PlcTaskSystemInfo structure, which contains a FirstCycle boolean. This bit is TRUE only during the very first execution of that specific task after the TwinCAT runtime starts. Implementation Example (Structured Text):
VAR fbGetCurTaskIdx : GETCURTASKINDEX; // Function block to find the current task ID bFirstScan : BOOL; END_VAR fbGetCurTaskIdx(); // Call the FB // Access the system's internal task info array bFirstScan := _TaskInfo[fbGetCurTaskIdx.index].FirstCycle; IF bFirstScan THEN // Insert initialization logic here END_IF Use code with caution. Copied to clipboard
Behavior: This bit usually only triggers when the TwinCAT Runtime is started or restarted. Simply stopping and starting the PLC code with the "Start/Stop" commands in the IDE may not reset this bit. 2. Custom Initialization Variable
If you need a bit that resets every time you "Stop" and "Start" the PLC program (warm restart), the standard practice is to create a non-persistent variable that defaults to TRUE and is set to FALSE at the end of the scan. Implementation Example:
Declare a global or local variable: bInitialized : BOOL := FALSE; Logic:
IF NOT bInitialized THEN // Initialization code here bInitialized := TRUE; END_IF Use code with caution. Copied to clipboard
Behavior: Because TwinCAT resets non-persistent variables to their initial state upon a program start, bInitialized will be FALSE on the first scan, allowing your logic to run once. 3. Legacy Hardware (CX1010)
On older hardware like the CX1010, some technical references mention a specific status bit (Bit 4) in certain control parameters that is set for the first scan after power-up. Key Comparison PlcTaskSystemInfo.FirstCycle Custom bInitialized Variable Best for System-level startup/initialization Logic-level resets/warm restarts Reset Trigger TwinCAT Runtime Restart PLC Program Start (Start/Stop) Setup Requires GETCURTASKINDEX Simple declaration
In Beckhoff TwinCAT systems, there is no single global "S:FS" bit like those found in Rockwell (Allen-Bradley) controllers . Instead, users typically leverage the PlcTaskSystemInfo
structure or create a custom initialization variable to manage first-scan logic. Beckhoff Information System Key Ways to Implement a First Scan Bit
There are two primary methods used in TwinCAT to achieve "first scan" functionality: System Variable Method : The most robust way is using the FirstCycle member of the PlcTaskSystemInfo structure. How it works : Every PLC task has a system variable which contains a boolean FirstCycle . This bit is only during the very first cycle of that specific task.
: It is built into the runtime and is highly reliable for initializing state machines or variables. Implementation Example
IF _TaskInfo[GETCURTASKINDEX()].FirstCycle THEN // Your initialization logic here END_IF; Use code with caution. Copied to clipboard Custom Variable Method beckhoff first scan bit
: You can manually create a non-retentive boolean variable initialized as How it works : Define a with an initial value of
. At the very end of your main program, set this variable to
. It will then act as a first-scan bit for every subsequent cycle. : Simple to understand and portable across different IEC 61131-3 platforms. Beckhoff Information System User Experience and "Reviews" Behavioral Quirks : Some users have noted that the FirstCycle
bit may only trigger when the TwinCAT runtime is fully restarted, rather than just stopping and starting the PLC code execution via the developer interface. Initialization Importance
: In industrial settings, a first-scan bit is considered essential for resetting retentive memory and ensuring equipment starts in a safe, predictable state. Alternative for Advanced Users
: For complex setups, some developers prefer using a dedicated Initialization (INIT) block
or state machine that runs once before the main cyclic logic begins. DigiKey TechForum
For official documentation on these system variables, you can refer to the Beckhoff Information System Are you looking to initialize specific variables or are you migrating logic from another PLC platform RSLogix 5000 First Scan Bit (S:FS) Programming Guide
In the world of industrial automation, specifically within the Beckhoff TwinCAT environment, the "first scan bit" is a fundamental concept used to initialize logic, reset variables, or trigger one-time events when a PLC program transitions from Stop to Run mode.
Understanding how to implement and utilize this bit effectively ensures that your machines start up in a safe, predictable state every time the controller is powered on or the code is restarted. What is a First Scan Bit?
A first scan bit is a boolean flag that remains TRUE for exactly one execution cycle of the PLC task. After the first logic solve is complete, the bit drops to FALSE and stays there until the PLC is restarted.
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)
The most robust method involves using the built-in PlcTaskSystemInfo structure. This provides real-time data about the current task. Variable: _TaskInfo[index].FirstCycle
How it works: This system variable is automatically managed by the TwinCAT runtime.
Benefit: No manual coding is required to reset the bit; it is inherently tied to the task execution. 2. Manual Logic (The Classic Way)
If you prefer a portable method that works across almost any IEC 61131-3 platform, you can create your own logic using a global variable.
Step 1: Declare a global boolean, e.g., bFirstScan : BOOL := TRUE;.
Step 2: At the very end of your MAIN routine, add: bFirstScan := FALSE;.
How it works: On startup, the variable initializes to TRUE. The logic runs once, and then the assignment at the bottom kills the bit for all subsequent cycles. 3. Using the 'Init' Attribute
TwinCAT 3 supports the attribute 'init_on_on_online_change' or specific FB_init methods. While more advanced, these are used to run initialization code specifically when a function block is instantiated or the PLC starts. Common Use Cases In legacy TwinCAT 2 and early TwinCAT 3
🚀 Initialization of SetpointsEnsures that PID gains, speed limits, or timers start with default "safe" values rather than zeros.
🔄 Resetting State MachinesForces your Sequential Function Chart (SFC) or CASE statements to jump to the 'IDLE' or 'INIT' state regardless of where they were during the last shutdown.
📡 Communication HandshakesTriggers a "Hello" or synchronization pulse to external devices, such as HMIs or SQL databases, to signal that the PLC is back online. Best Practices and Pitfalls
Execution Order Matters: 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.
Distributed Tasks: If your TwinCAT project has multiple tasks (e.g., a fast 1ms task and a slow 10ms task), remember that each task has its own "first cycle."
Avoid Logic Overload: Don't cram too much heavy processing into the first scan. If you have massive data to load, consider a dedicated "Initialization" state that spans multiple cycles.
⚠️ Key Reminder: Always use the first scan bit to put your machine into a Safe State. Never assume the hardware is in the same position it was when the power went out.
Here’s a concise guide to the First Scan Bit in Beckhoff TwinCAT (IEC 61131-3).
The First Scan Bit is a system-generated boolean flag that is TRUE for exactly one PLC cycle after:
After that single cycle, it automatically returns to FALSE and stays FALSE until the next restart.
In TwinCAT 2, it is typically
FirstCycleorFirstScan.
In TwinCAT 3, it is available viaTc2_Systemfunction block or directly asFirstScanin some contexts.
Network interfaces, serial buffers, or fieldbus master configurations may contain stale data after a restart.
IF FirstScan THEN // Clear receive/transmit buffers receiveBuffer := ''; sendBuffer := '';// Reset bus coupler ETC_ClearDeviceState();
END_IF
For advanced TwinCAT 3 users working with Function Blocks, the most elegant and robust "first scan" is not a bit at all—it's the object constructor: FB_Init.
When you instantiate a function block, TwinCAT automatically calls its FB_Init method once.
FUNCTION_BLOCK FB_DriveController VAR_INPUT bEnable : BOOL; END_VAR VAR_OUTPUT bReady : BOOL; END_VAR VAR fSpeed : REAL; END_VAR
// This runs once when the FB is created (first scan of the FB) METHOD FB_Init : BOOL VAR_INPUT bInitRetains : BOOL; // TRUE if retain variables are restored bInCopyCode : BOOL; // TRUE if FB is copied END_VAR fSpeed := 0.0; // Initialize internal variable bReady := FALSE; END_METHOD
Advantages:
When to use this: In large OOP-based TwinCAT projects with many reusable function blocks. How it works : The timer’s output starts FALSE
If you are using Object-Oriented Programming (OOP) with Function Blocks, you should generally use the FB_Init method for hardware checks or setup, rather than a "First Scan" bit inside the body logic. This runs before the first cyclic call and is cleaner for object initialization.
However, for standard ladder logic or structured text programs, the boolean flag method above is the industry standard for Beckhoff systems.
Have you found other uses for a "First Scan" bit in your machines? Let me know in the comments!
#Beckhoff #TwinCAT #PLCProgramming #Automation #ControlsEngineering #IEC61131
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:
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
In the world of industrial automation, a clean start is everything. When a Beckhoff PLC (Programmable Logic Controller) boots up—whether from a power cycle, a download of a new TwinCAT configuration, or a manual restart—the system enters a critical, fleeting state. Variables are uninitialized, previous runtime values linger in memory, and physical outputs may hold their last state. How do you reliably distinguish this “first scan” from normal cyclic operation?
The answer lies in a small, single-purpose variable: FirstScan (or bInit in older TwinCAT 2 nomenclature). Though it appears only once per startup, its impact on system reliability is profound.
VAR bFirstScan : BOOL := TRUE; END_VAR
IF bFirstScan THEN bFirstScan := FALSE; // One-time init code END_IF
⚠️ Caution: This resets on warm start. For cold start retention, use VAR RETAIN.