// 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
IF fbFirstScan.bFirstScan THEN // First cycle only bResetDrives := TRUE; bStartupComplete := FALSE; tStartupTimer(IN:=FALSE); END_IF
Add the TwinCAT_SystemInfoVarList to your project, then:
PROGRAM MAIN VAR fbFirstScan : FB_FirstScan; bInitDone : BOOL; END_VAR beckhoff first scan bit
: Tracks how long (in milliseconds) the task has been running.
If you perform an "Online Change" (modify code without full download), the first scan bit does trigger. Your initialization code will not run. To force reinitialization, use Reset or Reset Cold from the TwinCAT runtime.
The standard method to detect the first cycle in TwinCAT is to use two key tools: the GETCURTASKINDEX function block and the global _TaskInfo array. // This runs once when the FB is
// First scan detection IF bFirstScan AND NOT bFirstScanDone THEN bFirstScanDone := TRUE; // First scan logic here END_IF
Ensure all actuators are locked out until initialized.
The First Scan Bit is essential in various scenarios, including: To force reinitialization, use Reset or Reset Cold
Understanding the interaction between the first scan bit and different variable types is crucial for robust PLC design:
fbGetCurTaskIndex(); // Call the FB to refresh the index
The First Scan Bit is a that is TRUE only for a single PLC cycle immediately after the runtime system starts executing the application. On the second cycle, it becomes FALSE and remains FALSE until the next controller reboot or program download.
⚠️ Caution : This resets on warm start. For cold start retention, use VAR RETAIN .
Now that you have a comprehensive understanding of the first scan bit, go forth and build more robust and reliable Beckhoff TwinCAT applications!