Join us today!
Set variable after every cycle start
Hello,
I wish to have a method which I would like to call many times each cycle but the method contains code that should be called only single time each cycle. Is there a way to somehow recognize new cycle?
I hope I am not too late.
You can use PlcTaskSystemInfo to get the CycleCount and save its value to a local variable in the function block. On every call of your method, get the CycleCount from the PlcTaskSystemInfo and compare it to the function block's local variable value. If they're both the same, were in the same cycle, don't call the code to be executed, if they're not the same, it's a new cycle, execute the code. Here's an example:
Getting the CycleCount the way Beckhoff described it is annoying. So a I made a function to do the work.
FUNCTION F_GetPlcTaskInformation : PlcTaskSystemInfo VAR fbGetCurrentTaskIndex : GETCURTASKINDEX; END_VAR ------------------------------------------------------------ fbGetCurrentTaskIndex(); F_GetPlcTaskInformation := TwinCAT_SystemInfoVarList._TaskInfo[fbGetCurrentTaskIndex.index];
METHOD PUBLIC DoSomething VAR_INST nCurrentCycle : UDINT := 0; END_VAR VAR stTaskInfo : PlcTaskSystemInfo; END_VAR --------------------------------------------- stTaskInfo := F_GetPlcTaskInformation(); IF (nCurrentCycle = stTaskInfo.CycleCount) AND (NOT stTaskInfo.FirstCycle) THEN RETURN; END_IF nCurrentCycle := stTaskInfo.CycleCount; // Code to be called once per cycle goes here...
- 17 Forums
- 265 Topics
- 932 Posts
- 1 Online
- 689 Members