IEC61131-3 Function block within a function block

kolyur

Lifetime Supporting Member + Moderator
Join Date
Oct 2004
Location
Wooster, Ohio
Posts
1,602
I'm using structured text in Beckhoff Twincat 3 but this question likely applies to Codesys in general.

Suppose I have two function blocks, fbA and fbB. fbA is called from the Main program and fbB is called from within fbA. Block fbA has local input variable varA which is set in the call from Main. How can I access varA from inside block fbB?

The only way I've found to do it so far is to define varA inside fbB also, and put varA:=varA in the call to fbB. I suspect there must be a more appropriate way to do this. I tried defining fbB as a Method within fbA, as that allows local variables to be shared, but then realized that a Method works more like a Function and can't retain variable values. I read about the Extend option in Beckhoff help but couldn't make heads or tails of it. What am I missing?
 
Hi
I do exactly as you suggest, for no other reason than it keeps fbB as autonomous, ie it does need to belong to fbA.
Interested if there's another way, but the autonomy reason is compelling for me.
Pp
 
Make it a global var, or use an I/O variabele, however this is only working good, if you only write it in one place, so be carefull not to mix.
You can make it yourself easier by using Complex Function Chart (CFC)
 
The only way I've found to do it so far is to define varA inside fbB also, and put varA:=varA in the call to fbB. I suspect there must be a more appropriate way to do this.

In my view that is the correct way to do it. Directly accessing variables outside your function block makes for code that is harder to maintain, with dependencies that are not clearly visible and increasingly difficult to maintain and troubleshoot as your program gets bigger and more complex. Passing values through as "parameters" by means of the VAR_IN section is the way to go about (or VAR_IN_OUT or VAR_OUT sections, if applicable).

Use of global variables may produce code that works indeed but is frowned upon by the programming community in general (not talking about PLC programmers only). There is a place for global variables, but most of the time it is not every day run-of-the-mill PLC code.
 

Similar Topics

Hello all :hand: I've been browsing Google and forums for a while but haven't found a solution to my problem so far. To give a bit of context, I...
Replies
8
Views
3,797
How can I make symbols in Global Variable? I am able to create "SYM_XML" file with the main project file (CoDeSys V2.3.9.13 and select PLC...
Replies
2
Views
2,067
Hi. I use TwinCAT, but I guess its a general IEC61131-3 question. So I have a Profibus slave with 16 digital input. I like to link this...
Replies
2
Views
4,223
if any body know the good book to learn IEC61131 programming language then please reply...(specifically ST language)
Replies
0
Views
1,398
Hello to all! I am a student of electrotechnics and automation. I have nearly reached the goal and have to do my diploma-thesis. I have to...
Replies
11
Views
2,826
Back
Top Bottom