1

I'm trying to create an FB which can complete work on an interface that can be implemented in many FINAL FBs.

INTERFACE IBaseInterface

FUNCTION_BLOCK  Base IMPLEMENTS IBaseInterface

FUNCTION_BLOCK SYSTEM // the function block that will do the work 

METHOD GetMax : REAL

VAR_IN_OUT
    arrData   : ARRAY[*] OF IBaseInterface;
END_VAR

//then in PLC_PRG
PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    result: REAL;
    boolResult : BOOL;
    System : SYSTEM;
END_VAR

result := System.GetMax(BaseArray);

I'm getting an error that Base can't be converted to IBaseInterface. Can someone let me know what I'm doing wrong? how can I program to an interface, and then pass a dynamic array of a final FB that implements the interface?

1 Answers1

1

In codesys, you can think of a FunctionBlock as a Structure with Method (function) Pointers. However, "CODESYS always treats variables declared with the type of an interface as references [of the Function Block].", as such, an array of FB in codesys can't statically be mapped to an array of references (interfaces). Other languages either also treat objects as references, or do the conversion automatically for you. Unfortunately afaik, in codesys you'll have to do that manually:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface := [
        BaseArray[1],
        BaseArray[2],
        BaseArray[3],
        BaseArray[4],
        ...
        BaseArray[50],
    ];
END_VAR

result := System.GetMax(InterArray);

Or:

PROGRAM PLC_PRG
VAR
    BaseArray : ARRAY [1..50] OF Base;
    InterArray : ARRAY [1..50] OF IBaseInterface;
    i: UDINT;
END_VAR

FOR i := 1 TO 50 DO
    InterArray[i] := BaseArray[i];
END_FOR
result := System.GetMax(InterArray);
Guiorgy
  • 1,405
  • 9
  • 26