Practical use case: Counting operation runs
- Ivaylo Fiziev
- Nov 21
- 1 min read

This post is inspired by a recent question in the Siemens community forum (https://community.sw.siemens.com/s/question/0D5Vb00000rlev1KAA/help-with-counter-triggered-by-operation-end-signal-in-process-simulate). It seems generic enough to be mentioned here. It is actually pretty simple but can be really useful in many situations.
So how do you count how many times an operation has been executed? You just increment a counter on each pulse of the operation's end signal.
A simple SCL script can help you do this:
FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_INPUT
clk : BOOL;
END_VAR
VAR_OUTPUT
out : DWORD;
END_VAR
VAR
trig : R_TRIG;
END_VAR
BEGIN
#trig(CLK:=#clk);
if #trig.Q then
#out := #out + 1;
end_if;
END_FUNCTION_BLOCKPaste this in the SCL editor, connect the operation end signal and a counter signal to it, save the changes and you are ready to go.

Change the input signal and count whatever needs to be counted.
Adapt the code to your needs and make it work for you!
It is that simple.




Comments