twincat

Is there a way to create a generic "GetTaskInfo" method in TwinCAT?


I tried creating an abstract function block that can be extended with certain FBs that need the task information, most of the time this is only task cycle time. I went ahead and created the function block :

FUNCTION_BLOCK ABSTRACT FB_HasTaskInfo
VAR
    _PlcTaskInfo    : PlcTaskSystemInfo;
END_VAR

Property TaskInfo

{attribute 'monitoring':='call'}
PROPERTY TaskInfo : REFERENCE TO PlcTaskSystemInfo

Get accesssor:

VAR
    taskIndex   : DINT;
END_VAR

IF _plcTaskInfo.CycleTime = 0 THEN
    taskIndex := Tc2_System.GETCURTASKINDEXEX();
    IF taskIndex <> 0 THEN
        _PlcTaskInfo := _TaskInfo[taskindex];
    END_IF
END_IF

TaskInfo REF= _PlcTaskInfo;

This works fine inside the library and the tests also pass. The problem comes when I import the library into another project, the property's get accessor is throwing an error that it basically can't find _taskInfo[taskIndex] which kind of makes sense since the data type is auto generated for each PLC project by TwinCAT I assume (it is located in external folder). I am guessing the _taskInfo is also auto generated, somehow? enter image description here

The image show the error inside the imported library: enter image description here

Is there a way to make the generic function, or do you need to explicitly always do the call from within the PLC project?


Solution

  • Instead of just asking for

    _TaskInfo[taskindex]
    

    prepend it with a namespace

    TwinCAT_SystemInfoVarList._TaskInfo[taskindex]