IMDHMIControls LibraryShop main store

Troubleshooting

Why CODESYS rejects an array of derived function blocks

PLC programming Published 5 min read By IMDHMI controls team

Short answer

CODESYS and TwinCAT 3 refuse ARRAY OF BasicValve on a parameter typed ARRAY[*] OF AValve because the array itself is a value type: its storage is element size times element count, and inheritance is checked per object, never between array types. Convert each element into an array of the base interface, or build an array of POINTER TO the base function block, and pass that instead.

On this page
  1. What the compiler reports
  2. Why the whole array is one value type
  3. The stride arithmetic behind the refusal
  4. Fix it with an array of the base interface
  5. Fix it with pointers to the base function block
  6. The decision for each case
  7. Questions asked

CODESYS refuses ARRAY OF BasicValve wherever the dispatcher declares ARRAY[*] OF AValve, and the refusal is correct. In Structured Text the array itself is a value type: BasicValve EXTENDS AValve buys you conversion for one object, a VAR_IN_OUT parameter is a reference with a strict type binding, and ARRAY[*] OF BasicValve is therefore a different type from ARRAY[*] OF AValve — different element stride, different total size. No bound style or input mode converts between them. The pattern that compiles: build an array of the base interface, or an array of POINTER TO the base function block, one element at a time, and hand the dispatcher that array.

What the compiler reports #

Three attempts at the same call fail, and the messages name the same refusal:

  • Open bounds. The dispatcher declares VAR_IN_OUT valves : ARRAY[*] OF AValve; you pass an ARRAY[0..5] OF BasicValve. The compiler answers Cannot convert type 'ARRAY [0..5] OF BasicValve' to type 'ARRAY [*] OF AVALVE' of VAR_IN_OUT 'valves'.
  • Fixed bounds. Change the parameter to ARRAY[0..5] OF AValve and pass the same argument. The message becomes Type 'ARRAY[0..5] of BasicValve' is not equal to type 'ARRAY [0..5] OF AVALVE' of VAR_IN_OUT 'valves' — the mismatch is not the asterisk.
  • Plain assignment. aBase := aDerived; between two array variables of the two types, same bounds, outside any call, is refused as well.

A single element is a different story: an argument of type BasicValve binds wherever an AValve is expected, with no complaint. Inheritance is applied per object and refused per array type. That contrast, plus the fixed-bounds message, tells you the element type comparison — not the variable length — is what blocks the call.

Why the whole array is one value type #

Declaring ARRAY[*] OF FB_Base and ARRAY[*] OF FB_Ext declares two unrelated types; one does not extend the other even though FB_Ext extends FB_Base. An array's storage is its element size multiplied by its element count, and VAR_IN_OUT passes the array by reference with that layout frozen: the reference binds to one element type and one stride, strictly. The compiler checks inheritance when it compares an individual object against its base type, and it does not apply inheritance when it compares two complete array types. Fixed bounds, open bounds, VAR_INPUT and VAR_OUTPUT all reach the same refusal, because the conversion itself does not exist in the language.

The stride arithmetic behind the refusal #

The refusal has a concrete size behind it. The instance sizes here are illustrative — real sizes depend on each function block's members and the compiler — but the conclusion does not depend on the exact numbers, only on the stride. Suppose one AValve instance occupies 16 bytes and BasicValve adds 16 bytes of its own members, so one BasicValve instance occupies 32 bytes. Six elements per array: 16 × 6 = 96 bytes for the base array, 32 × 6 = 192 bytes for the derived array. If the compiler accepted the call, the dispatcher would read element i at offset 16 × i — element 1 at offset 16, element 3 at offset 48 — while in the caller's memory each BasicValve sits at offset 32 × i, so element 1 begins at offset 32 and element 3 at offset 96. From element 1 on, every access through the parameter lands mid-instance: the dispatcher reads one valve's members as another valve's tail, and any write corrupts a neighboring instance. The type check protects the stride, and the stride is what your design has to stop relying on.

Fix it with an array of the base interface #

Give every valve a base interface — the object style underneath this fix, base interfaces, derived function blocks, methods and properties, is laid out in the guide to designing TwinCAT 3 programs with interfaces, methods and inheritance. Declare the dispatcher against an open array of the base interface and convert the concrete array one element per statement:

VAR
    aValve    : ARRAY[0..2] OF BasicValve;   // BasicValve EXTENDS AValve, IMPLEMENTS IValve
    aiValve   : ARRAY[0..2] OF IValve;
    oDispatch : ValveDispatch;               // VAR_IN_OUT valves : ARRAY[*] OF IValve
    i         : INT;
END_VAR
FOR i := 0 TO 2 DO
    aiValve[i] := aValve[i];   // the element-level conversion the compiler does perform
END_FOR
oDispatch.Run(valves := aiValve);

Inside the dispatcher, valves[i].Open() reaches the derived implementation through the interface. A valve that implements only a richer interface still binds: assign it to a base-typed interface reference first, using the compiler's explicit interface query operator where a direct derived-to-base assignment is not accepted.

The test that proves the result: compile, and both messages above disappear; then give BasicValve.Open() a per-instance effect — for example diOpens : DINT := 0 incremented on each call — run the dispatcher once, and confirm elements 0 to 2 each show their own increment rather than three hits on one instance.

Fix it with pointers to the base function block #

When the valves are plain function blocks with no interface, convert each element to a pointer of base type:

VAR
    aValve    : ARRAY[0..2] OF BasicValve;   // BasicValve EXTENDS AValve
    apValve   : ARRAY[0..2] OF POINTER TO AValve;
    oDispatch : ValveDispatch;               // VAR_IN_OUT valves : ARRAY[*] OF POINTER TO AValve
    i         : INT;
END_VAR
FOR i := 0 TO 2 DO
    apValve[i] := ADR(aValve[i]);
END_FOR
oDispatch.Run(valves := apValve);

The dispatcher dereferences each entry — apValve[i]^.Open() — and the call lands on the derived implementation, because the pointee is typed to the base and the address carries the derived data. Build the array from ADR of the exact elements at initialization and rebuild it after an online change that adds, removes or reorders the valve instances; a stale entry addresses whatever now occupies that memory, and the dispatcher writes into it.

The decision for each case #

  • Valves implement a shared base interface: build the interface array. It holds mixed derived types side by side, keeps the runtime type for every element, and needs no pointer arithmetic.
  • Valves are plain or abstract function blocks without an interface: build the pointer array with ADR. Keep every entry typed as a pointer to the base, never a raw address.
  • Retyping the parameter, any input mode, base array type: refused. Do not spend time there; the conversion does not exist between the two array types.
  • One or two call sites with a handful of valves: skip the array and pass each derived instance directly where the base is expected, element by element.

Whether the dispatcher itself should be a function or a function block is decided by state and reuse, not by this conversion problem; the two tests that decide between a function and a function block settle it in a minute. Either way, the array you pass to it must be built element by element — that is the whole fix.

Questions asked

Can I declare the dispatcher input as VAR_INPUT with the base array type instead?

No. The conversion between the two array types is unsupported through every input mode, not only VAR_IN_OUT, and a plain assignment between two array variables of the two types is refused as well. The only conversion the compiler performs is element by element, which is why both working fixes build a new array one element at a time.

Why does an open array with variable bounds not relax the check?

The asterisk changes only the bounds. It lets one dispatcher accept arrays of any length, all with the single element type named in its declaration. The element type must still match exactly, so an open array of the base type remains a different type from any array of the derived type, whatever bounds the actual argument has.

Which fix do I choose when the derived valves implement different interfaces?

Give the dispatcher the base interface that every valve shares and build the array of that base interface. A valve that also implements a richer interface still binds, because a base interface reference accepts any object that implements it. Where a method exists only on the richer interface, convert that one element back to it with the compiler's interface query operator before the call.

Does the pointer fix work when the base function block is abstract?

Yes. An abstract base function block cannot be instantiated on its own, but a pointer typed to it can address any derived instance, and a call through that pointer reaches the derived implementation. Take the address of each concrete element, type every entry as a pointer to the abstract base, and dereference inside the dispatcher.

Keep reading

  • How-to

    EL3314 status word error handling in TwinCAT PLCs

    Declare the EL3314's four channels as one array of value-and-status structures with AT %I*, link each element through an index-to-member assignment in the TcLinkTo attribute, and decode the status WORD with three AND masks: 16#0001 underrange, 16#0002 overrange, 16#0040 error. Dividing the raw integer by 10.0 gives degrees Celsius at 0.1 °C resolution; one function block instance per channel packages scale and decode so four indexed calls replace copy-pasted rungs.

    PLC programming

  • Troubleshooting

    Use the PLC Coder timer block correctly in TIA Portal

    A Stateflow delay generated with PLC Coder follows wall-clock time only when the timing implementation and the PLC task call interval agree with the model sample time. Pick the target IDE timer for real elapsed time; use the Target-Independent Counter only when the generated code runs at a fixed periodic interval equal to the model step.

    PLC programming

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming

Map a 32-byte IO-Link PROFINET item to a PLCnext array