TwinCAT - How to measure program execution time?

Viewed 841

I would like to measure the execution time of a structured text (ST) program. The task associated with the program is running at 10 ms.

How do I measure execution time?

2 Answers

You can use free TwinCAT library Tc2_Utilities that has a function block Profiler.

https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclib_tc2_utilities/35053195.html&id=1344160655692967299

The "Profiler" function block can be used to allow the execution time of PLC code to be measured.

The Infosys page has an example code also:

VAR
    Profiler1     : PROFILER;
END_VAR


Profiler1(START := TRUE, RESET := TRUE);

//Do something here

Profiler1(START := FALSE);
//Now Profiler1.Data has the execution time

Of course, you can use Profiler but just for demonstration purpose you can measure execution time like this.

PROGRAM PLC_PRG
    VAR
        tStart: TIME; (* Time program start *)
        tWork : TIME; (* Execution time *)
    END_VAR

    (* First line of main program *)
    tStart := TIME();

    // Your program here

    (* Last line of your program *)
    tWork := TIME() - tStart;
END_PROGRAM
Related