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

Acessing a function block adress inside the function block.

Rotule
2014-07-31
2014-08-22
  • Rotule - 2014-07-31

    Hello everyone,

    I would like to have acess to the adress of a function block in the code of that same function block. I have tried to think of a workaround but it would be the best way to do what I want. (I am making a framework for me to speed up development and minimise error while coding in the field I would need an Array with the adress of all instances but I want to update that Array from the block itself because I don't want to add another line of code in the Main program.)

    here is an example of what I would like to do:

    VAR_GLOBAL
       MYBLOCK_PTR_TAB:ARRAY[0..10] OF POINTER TO MYBLOCK;
       MY_BLOCK_INSTANCE_1:MYBLOCK:=(BLOCK_NUMBER:=1);
    END_VAR
    

    PLC_PRG

    MY_BLOCK_INSTANCE_1();
    

    Block Code:

    FUNCTION_BLOCK PUBLIC MYBLOCK
    VAR_INPUT
       BLOCK_NUMBER:BYTE;
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
       INITIALIZED:BOOL:=FALSE;
    END_VAR
    
    IF NOT(INITIALIZED) THEN
       MYBLOCK_PTR_TAB[BLOCK_NUMBER]:=###ADR(ME)###; (*Adress of this instance of MYBLOCK HERE*)
       INITIALIZED:=TRUE;
    END_IF
    (* More stuff Here *)
    

    If anyone have an Idea on how to do that, I would owe you a beer when I come to your country

    Thanks guys

     
  • Rotule - 2014-07-31

    There might be a more clever way of doing it but that works:
    I think we should not need to pass his own adress in Input but...

    VAR_GLOBAL
       MYBLOCK_PTR_TAB:ARRAY[0..10] OF POINTER TO MYBLOCK;
       MY_BLOCK_INSTANCE_1:MYBLOCK:=(BLOCK_NUMBER:=1);
    END_VAR
    

    PLC_PRG

    MY_BLOCK_INSTANCE_1(ME:=ARD(MY_BLOCK_INSTANCE_1));
    

    Block Code:

    FUNCTION_BLOCK PUBLIC MYBLOCK
    VAR_INPUT
       BLOCK_NUMBER:BYTE;
       ME:POINTER TO MYBLOCK;
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
       INITIALIZED:BOOL:=FALSE;
    END_VAR
    
    IF NOT(INITIALIZED) THEN
       MYBLOCK_PTR_TAB[BLOCK_NUMBER]:=ME; (*Adress of this instance of MYBLOCK HERE*)
       INITIALIZED:=TRUE;
    END_IF
    (* More stuff Here *)
    
     
  • TimvH

    TimvH - 2014-08-21

    Off course I don't know what the reason is of your question, but it might be worthwile to study the options of Object Oriented Programming in CODESYS. This way you can have access to Function Blocks using interfaces. This gives much better flexibility and readibility than using pointers.

    We developed a training for this which explains how to use the Object Oriented Programming techniques in CODESYS:
    In general the topics are about using:
    - Methods and properties
    - Interfaces and implementing them
    - Extending function blocks (inheritance)

    You could also check the store.codesys.com site which has some OOP example applications.

    If you are interested in the training you can contact Extend Smart Coding. We are situated in the Netherlands.

     
  • francois - 2014-08-22

    Have you tried:

    IF NOT(INITIALIZED) THEN
       MYBLOCK_PTR_TAB[BLOCK_NUMBER]:=THIS; (*Adress of this instance of MYBLOCK HERE*)
       INITIALIZED:=TRUE;
    END_IF
    

    Altough I would go in favor of OO/interfaces as mentioned by timvH.

     

Log in to post a comment.