Join us today!

Notifications
Clear all

Set variable after every cycle start

2 Posts
2 Users
3 Reactions
507 Views
Posts: 7
Topic starter
(@theshamot)
Active Member
Joined: 2 years ago

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?

 

Reply
1 Reply
Posts: 6
(@fisothemes)
Active Member
Joined: 1 year ago

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...

 

Reply
Share: