Large projects with multiple tasks or complex initialization. Quick, single-task projects or basic logic. Summary of Benefits
: Place code at the very end of your main program that sets this bit to FALSE . Because the variable is initialized to TRUE , it remains so for the entire first scan before being permanently toggled off. Comparison and Review PlcTaskSystemInfo.FirstCycle Manual Custom Bit Reliability Native to TwinCAT; handles task-specific restarts. Highly reliable if implemented at the program's end. Complexity Requires calling GETCURTASKINDEX . Extremely simple to declare and use. Best Use Case beckhoff first scan bit
The cleanest, most standard-compliant way to create a first scan bit in TwinCAT is by using a local BOOL variable that defaults to FALSE (or TRUE depending on your logic preference). Large projects with multiple tasks or complex initialization
PROGRAM MAIN VAR bFirstCycleDone : BOOL := FALSE; END_VAR Because the variable is initialized to TRUE ,
| TwinCAT Version | Variable Name | Scope | |----------------|---------------|-------| | TwinCAT 2 | bInit | Global (in Standard.lib ) | | TwinCAT 3 | FirstScan | Per-POU (automatic) |
IF SystemTaskInfoArr[1].firstCycle THEN // Send a configuration command to an EtherCAT terminal fbTerminalConfig.Execute := TRUE; // Specify the configuration index/value fbTerminalConfig.wIndex := 16#8000; fbTerminalConfig.wValue := 16#0001; END_IF
| Variable Type | Behavior on Power Cycle | Behavior on Program Download | Interaction with First Scan | |---|---|---|---| | | Re-initializes to declared start value | Re-initializes | First scan can set custom initial values | | RETAIN | Retains last value from before power loss | Retains last value (by default) | Useful for detecting first scan without losing value | | PERSISTENT | Retains value across program changes | Retains value across program downloads | Often used with first scan to determine if first run after major update |