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]OFPOINTERTOMYBLOCK;Â Â MY_BLOCK_INSTANCE_1:MYBLOCK:=(BLOCK_NUMBER:=1);END_VAR
PLC_PRG
MY_BLOCK_INSTANCE_1();
Block Code:
FUNCTION_BLOCKPUBLICMYBLOCKVAR_INPUTÂ Â BLOCK_NUMBER:BYTE;END_VARVAR_OUTPUTEND_VARVARÂ Â INITIALIZED:BOOL:=FALSE;END_VAR
IFNOT(INITIALIZED)THEN
  MYBLOCK_PTR_TAB[BLOCK_NUMBER]:=###ADR(ME)###; (*Adress of this instance of MYBLOCK HERE*)
  INITIALIZED:=TRUE;END_IF(*MorestuffHere*)
If anyone have an Idea on how to do that, I would owe you a beer when I come to your country
Thanks guys
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
FUNCTION_BLOCKPUBLICMYBLOCKVAR_INPUTÂ Â BLOCK_NUMBER:BYTE;Â Â ME:POINTERTOMYBLOCK;END_VARVAR_OUTPUTEND_VARVARÂ Â INITIALIZED:BOOL:=FALSE;END_VAR
IFNOT(INITIALIZED)THEN
  MYBLOCK_PTR_TAB[BLOCK_NUMBER]:=ME; (*Adress of this instance of MYBLOCK HERE*)
  INITIALIZED:=TRUE;END_IF(*MorestuffHere*)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
PLC_PRG
Block Code:
If anyone have an Idea on how to do that, I would owe you a beer when I come to your country
Thanks guys
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...
PLC_PRG
Block Code:
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.
Have you tried:
Altough I would go in favor of OO/interfaces as mentioned by timvH.