Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Casting a function block

dawidr
2021-04-26
2021-04-27
  • dawidr - 2021-04-26

    I'm using Wago PFC200 for my home automation. I got base function block:

    FUNCTION_BLOCK ABSTRACT Room
    

    and two the interface:

    INTERFACE IBlinds
    - BlindsUp
    - BlindsDown 
    

    and

    ILights
    -TurnOffLights
    -TurnOnLights
    

    My room's instances looks like this:

    FUNCTION_BLOCK Garage EXTENDS Room IMPLEMENTS ILights, IBlinds
    

    In my PLC_PRG I've all instances of my rooms:

    PROGRAM PLC_PRG
    VAR
        Bedroom: Bedroom;
        Garage: Garage; 
        Hall: Hall;
        Boilerroom: Boilerroom;
        ...
    END_VAR
    

    Under the PLC_PRG I've some methods to e.g.: automate blinds:

    METHOD MoveBlindsToMorningPosition
    VAR CONSTANT
        siCount: SINT := 5;
    END_VAR
    VAR_INPUT
        xMoveSignal: BOOL;
    END_VAR
    VAR
        _siIndex: SINT;
        _rooms: ARRAY[0..siCount] OF POINTER TO IBlinds := [ADR(Livingroom), ADR(Diningroom), ADR(Kitchen), ADR(Toilet), ADR(Boilerroom), ADR(Garage)];
    END_VAR
    
    FOR _siIndex := 0 TO siCount DO
        _rooms[_siIndex]^.MoveBlindsToMorningPosition(xMove := xMoveSignal);
    END_FOR
    

    But I got following compilation errors for each room in the _rooms array: C0032: Cannot convert type 'POINTER TO Garage' to type 'POINTER TO IBlinds'

    My function block implements IBlinds. Is there a way to cast function block?

     

    Last edit: dawidr 2021-04-26
  • GaryPratt - 2021-04-27

    Try removing "POINTER TO" in the declaration of _rooms. It does seem intuitive to need the Pointer To, but the language takes care of the managing the pointer for you. The pointer is implied.

     
    πŸ‘
    1
  • dawidr - 2021-04-27

    @GaryPratt I learned that yesterday evening by reading the CodeSys documentation where it is stated that:

    CODESYS always treats variables declared with the type of an interface as references

     

    Last edit: dawidr 2021-04-27

Log in to post a comment.