FB_init is a specialized method you can add to any Function Block. It executes automatically before the PLC task starts its cyclic execution. Right-click your Function Block →right arrow →right arrow Method . Name the method exactly FB_init . It automatically generates the required signature:
PROGRAM MAIN VAR fbGetCurTaskIndex : GETCURTASKINDEX; bFirstScan : BOOL; END_VAR // Fetch the index of the currently running task fbGetCurTaskIndex(); // Read the first cycle flag for this specific task bFirstScan := _TaskInfo[fbGetCurTaskIndex.index].FirstCycle; // Use the first scan bit to initialize logic IF bFirstScan THEN // Execute one-time configuration routines rTargetVelocity := 100.0; sMachineState := 'INITIALIZING'; END_IF Use code with caution. Why This Works
IF bFirstScan THEN bFirstScan := FALSE; // One-time init code END_IF
In Beckhoff TwinCAT (2 and 3), there is no single "magic" global bit like the S:FS in Allen-Bradley . Instead, you can access the "First Scan" status through built-in system variables or by creating a custom initialization flag. 1. Using Built-in System Info ( FirstCycle )
: Setting default values for non-persistent variables. beckhoff first scan bit
This public link is valid for 7 days and shares a thread, including any personal information you added. This link or copies made by others cannot be deleted. If you share with third parties, their policies apply. Can’t copy the link right now. Try again later.
An compiles only the modified code without stopping the PLC.
When the TwinCAT runtime enters Run Mode, bInitialized is allocated memory and set to FALSE . The IF NOT bInitialized condition evaluates to TRUE .
: Setting initial values for variables that aren't retentive. Communication Setup FB_init is a specialized method you can add
The array index _TaskInfo[1] points to your primary PLC task. The property .CycleCount increments automatically on every cycle. On the very first pass, it evaluates to 1 , rendering bFirstScan true. On cycle two and all subsequent cycles, bFirstScan becomes false automatically. Method 2: The Classic IEC 61131-3 "Inverted Flag" Approach
The initialization code executes, and bInitialized is toggled to TRUE .
: Simplest to implement and easy to read for engineers coming from other platforms.
END_IF
A common practice in IEC 61131-3 programming is to create a local or global boolean variable that defaults to and is turned at the end of the first cycle. Initialization : Declare a variable like bFirstScan with an initial value of : At the very end of your main program, set it to
structure or create a custom initialization variable to manage first-scan logic. Beckhoff Information System Key Ways to Implement a First Scan Bit
Using the first scan bit improperly can lead to hard-to-debug startup issues. Follow these best practices:
: 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. Name the method exactly FB_init