Does not waste execution time checking an IF statement during normal cyclic operation. Critical Use Cases for First Scan Logic
When a PLC starts, variables often default to zero or their last persisted state. However, many industrial systems require a specific "safe state" or initial configuration before the main control loop takes over. The first scan bit acts as a system trigger , allowing programmers to: Set Initial Values:
Reading configuration .csv or .xml files from the IPC (Industrial PC) storage into the PLC memory at boot time.
: This is a built-in array of system information instances. It points exactly to the task index retrieved in the step before, and checks its .FirstCycle boolean.
You can easily test it by:
TwinCAT provides a global structure called TwinCAT_SystemInfoVarList which contains runtime metrics about the PLC. Within this structure, you can access the current cycle counter of the task executing the code.
// -- Main control loop -- RunMachineLogic();
Implementation Method 1: The Local Initialization Attribute (Recommended)
PROGRAM MAIN VAR bFirstScan : BOOL; // True only during the first execution cycle bInitialized : BOOL; // Global flag showing init status fbReadConfig : FB_FileRead; // Example init function block END_VAR // Determine First Scan using the TwinCAT Task Info structure bFirstScan := (_TaskInfo[1].CycleCount = 1); IF bFirstScan THEN // Execute Startup-Only Logic Here bInitialized := FALSE; // Example: Set default hardware overrides GVL_Hardware.TargetVelocity := 100.0; END_IF; // Rest of your cyclic PLC program runs below Use code with caution. Why this works:
Simple flag:
// EXIT section runs when program stops EXIT myOutput := FALSE;