Scan Bit !new! — Beckhoff First

In Beckhoff TwinCAT, the "first scan bit" is a fundamental tool used to execute initialization code only once when the PLC starts or transitions into Run mode. Unlike some other platforms that use a fixed system bit (like Siemens' S:FS), TwinCAT provides a more flexible approach using built-in system structures or manual variable initialization. Direct Solution for First Scan Bit

The most reliable way to access a "first scan" state in TwinCAT 3 is through the global array _TaskInfo. This structure contains real-time information for each task running on the controller. Standard Implementation (Structured Text):

VAR fbGetCurTaskIndex : GETCURTASKINDEX; bFirstScan : BOOL; END_VAR fbGetCurTaskIndex(); // Get the index of the current PLC task bFirstScan := _TaskInfo[fbGetCurTaskIndex.index].FirstCycle; // Extract the bit IF bFirstScan THEN // Place initialization logic here (e.g., loading presets or clearing memory) END_IF Use code with caution. Copied to clipboard 1. Understanding System Information Structure

The FirstCycle bit is a member of the PlcTaskSystemInfo data type. This structure is part of the TwinCAT 3 PLC library and provides diagnostic data directly from the real-time kernel. Accessing it requires the GETCURTASKINDEX function block to identify which task is currently executing, as TwinCAT can run multiple tasks with different cycle times. 2. Alternative "Manual" Method

Many developers prefer a manual method for simplicity or for use in older versions of TwinCAT where system structures might differ. This relies on the initialization value of a boolean variable.

Step 1: Declare a global or local boolean variable initialized to TRUE. beckhoff first scan bit

Step 2: Use this variable as a condition for your initialization code.

Step 3: At the end of your program or inside the conditional block, set the variable to FALSE. Manual Code Example:

VAR bInitialScan : BOOL := TRUE; // Initialized to TRUE on startup END_VAR IF bInitialScan THEN // Startup logic bInitialScan := FALSE; // Permanent reset after first run END_IF Use code with caution. Copied to clipboard 3. Applications and Best Practices The first scan bit is essential for:

Initializing Variables: Setting default values for non-persistent variables.

Triggering Communication: Starting handshake sequences with external devices or HMIs. In Beckhoff TwinCAT, the "first scan bit" is

Safety Checks: Ensuring all actuators and states are in a safe "Home" position before the main logic begins. Summary of First Scan Options Method Implementation System Info _TaskInfo[index].FirstCycle Native, accurate, and task-specific. Manual Bit Conditional BOOL reset to FALSE Simplest logic, works across all platforms. Function Block GETCURTASKINDEXEX() Faster implementation as it doesn't require instantiation.

Final ResultIn Beckhoff TwinCAT, the First Scan Bit is accessed via the _TaskInfo system array using the FirstCycle property. This bit is uniquely TRUE during the first execution cycle of a task, allowing for precise system initialization. First Scan Bit - OpenPLC Forum

1. Using TcSystem library – FB_FirstScan (recommended)

PROGRAM MAIN
VAR
    fbFirstScan : FB_FirstScan;
    bInitDone : BOOL;
END_VAR

fbFirstScan(); IF fbFirstScan.bFirstScan THEN // One-time initialization code here bInitDone := TRUE; END_IF

Why Do You Need It?

During the first scan, you typically want to: Why Do You Need It

  1. Set default values for global variables (since retain/persistent variables may hold old data).
  2. Home axes or reset servo drives to a known state.
  3. Clear alarm buffers that existed before the controller went offline.
  4. Initialize communication buffers (EtherCAT, OPC UA, etc.).
  5. Prevent unintended motion by setting all outputs to a safe state before logic runs.

Without a proper first scan routine, your machine might start with actuators in unpredictable positions or unfinished states from a previous run.


Using FB_FirstScan from Tc3_System

PROGRAM MAIN
VAR
    fbSystemFirstScan : FB_FirstScan;
    bGlobalFirstScan : BOOL;
END_VAR

// Call this every cycle (it will return TRUE only on first) bGlobalFirstScan := fbSystemFirstScan();

The FB_FirstScan function block monitors the system’s cycle counter and reliably returns TRUE for exactly one cycle after application start, even if multiple programs call the same FB instance.


⚠️ Important: Retain vs. First Scan

A common pitfall:
If you mark variables as RETAIN, they survive a warm start. But on a first scan (especially after download), you may want to override retain values.

IF fbFirstScan.bFirstScan THEN
    (* Force reset retain values on fresh download *)
    rMotorPosition := 0.0;    // Even if retain, reset on first scan
    bRecipeLoaded := FALSE;
END_IF

Pitfall 3: Race Conditions with EtherCAT

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

Product added to wishlist