Beckhoff First Scan Bit [top]

IF TwinCAT_SystemInfoVarList._TaskInfo[1].CycleCount = 1 THEN // Execute first scan logic END_IF; Use code with caution.

The behavior of the first scan bit can vary depending on your specific Beckhoff hardware platform:

END_IF

A "first scan bit" is a special internal flag found in most PLCs (Programmable Logic Controllers). It turns for only the very first execution cycle after the controller is powered on or after it transitions from a stopped state to the run state. Once that first cycle is complete, the flag is automatically set to FALSE for all subsequent cycles. beckhoff first scan bit

To illustrate the utility of the first scan bit, here are a few common industrial automation use cases:

The most robust and recommended method is to use the firstCycle variable from the TcSystem library. This variable is part of the SYSTEMTASKINFOTYPE structure, which is accessible via the SystemTaskInfoArr array. The index in this array (typically 1) corresponds to the PLC task ID.

| Platform | Naming / Implementation | | :--- | :--- | | | _TaskInfo[].FirstCycle via GETCURTASKINDEXEX() | | Siemens TIA Portal | OB100 (Organization Block for startup) | | Rockwell Studio 5000 | S:FS (System Status Bit for First Scan) | | CODESYS | Accessible via _TaskInfo[].FirstCycle or PlcTaskSystemInfo | IF TwinCAT_SystemInfoVarList

TwinCAT provides a dedicated global data structure called PlcTaskSystemInfo , which contains a FirstCycle boolean for this exact purpose. The official and recommended approach to access it is:

Unlike some other PLC platforms (like Allen-Bradley or Siemens) that have a pre-defined system bit (e.g., First_Scan ), Beckhoff TwinCAT requires you to create this bit yourself. Fortunately, it is very simple to do. Method A: The Classic TwinCAT Approach (Code-Based)

: Provides the exact number of cycles the task has completed since boot—a fantastic diagnostic tool for tracking task execution frequency. Once that first cycle is complete, the flag

: This bit is TRUE during the very first execution cycle of the PLC task and automatically switches to FALSE for all subsequent cycles. Implementation Example (ST)

PROGRAM MAIN VAR bFirstScan : BOOL := TRUE; // Initialize to TRUE bIsInitialized : BOOL := FALSE; END_VAR // ---------------------------------------------------- // FIRST SCAN LOGIC // ---------------------------------------------------- IF bFirstScan THEN // --- Place Initialization Code Here --- // Example: Set initial values // AxisRef.bExecute := FALSE; // CurrentState := E_State.Idle; // ------------------------------------- bFirstScan := FALSE; // Turn off for next scan bIsInitialized := TRUE; // Mark as initialized END_IF // ---------------------------------------------------- // NORMAL CYCLIC CODE // ---------------------------------------------------- // ... rest of your program Use code with caution. The "TwinCAT Runtime" Method (Recommended)

If you are transitioning to the Beckhoff ecosystem from Rockwell Automation (Studio 5000) or Siemens (TIA Portal), you might quickly notice that . Because Beckhoff uses an object-oriented, multi-tasking real-time environment based on CODESYS, initialization is handled through explicit system data types, initialization code blocks, or custom software variables.