sulee
Subject: OpenPLC Editor: REPEAT…UNTIL behaves differently in FUNCTION vs FUNCTION_BLOCK (returns 10 vs 1)
 
Hi OpenPLC team,
I’ve run into what looks like a code-generation or runtime bug in OpenPLC Editor regarding REPEAT … UNTIL loops producing different results in a FUNCTION vs a FUNCTION_BLOCK.
 
Environment
OpenPLC Editor: [OpenPLC Editor 3.0 Release:2024-10-16]
Runtime/Target: [simulator]
OS: [window]
 
Minimal example
FUNCTION version (REPEAT_FC)
Input: MaxN_FC := 10
Local: cnt_FC
cnt_FC := 0;
REPEAT
cnt_FC := cnt_FC + 1;
UNTIL (cnt_FC < MaxN_FC) END_REPEAT;
REPEAT_FC := cnt_FC;

Observed result: CountRet_m = 10
 
 
FUNCTION_BLOCK version (REPEAT_FB)
Input: MaxN_FB := 10
Local: cnt_FB
cnt_FB := 0;
REPEAT
cnt_FB := cnt_FB + 1;
UNTIL (cnt_FB > MaxN_FB) END_REPEAT;
cnt_total_FB := cnt_FB;

Observed result: cnt_total_m = 1
 
Main call sequence
CountRet_m := REPEAT_FC(MaxN_FC := MaxN_m);
REPEAT_FB0(MaxN_FB := MaxN_m);
cnt_total_m := REPEAT_FB0.cnt_total_FB;
 
Expected behavior
With Siemens (TIA Portal), both versions return 10 with equivalent logic. In other words, the REPEAT … UNTIL semantics are consistent between FUNCTION and FUNCTION_BLOCK.
 
Actual behavior in OpenPLC Editor
FUNCTION returns 10
FUNCTION_BLOCK returns 1
 
This suggests that either:
The REPEAT … UNTIL termination condition is being evaluated differently for FBs vs FCs, or
Local variable initialization / retention semantics for FB locals are affecting the loop unexpectedly, or
Code generation for FBs flips or short-circuits the UNTIL condition.
 
Why this looks like a bug
IEC 61131-3 defines REPEAT … UNTIL <Condition> END_REPEAT; with the condition checked after each iteration; when the condition is TRUE, the loop terminates. That semantic should be identical regardless of POU type (FUNCTION vs FUNCTION_BLOCK). Seeing 10 vs 1 for the same overall intent strongly indicates inconsistent handling in OpenPLC’s FB path.
 
Notes
The Siemens toolchain yields 10 for both cases.
In OpenPLC, changing only the POU kind (FUNCTION → FUNCTION_BLOCK) shifts the result from 10 to 1.
 
I may have made a mistake, so please review and correct it in the next version.
Thanks!
Reply 0 0
thiagoralves
Your editor is almost one year old. Please update your editor to the latest version (file->check for updates) and repeat your tests to verify if the issue is still present in the last version of the editor
Reply 0 0
Reply