TIA Portal v18: Function Block "Static" or "InOut"?

Your block should be a function with a return value of type int for the needle value. You can use out in a function but why get off on the wrong foot with functions. (flame suite on).
 
Wow thanks so much for the detailed replies. I'd be completely lost without this kind of help.

I Reckon I'll be using this FB at least 20 times in the code.

Basic question...when I create each new instance of the FB, presumably I need to give each one a unique name, right? Otherwise I'm asking for trouble. Is that right?

The general best practice is for each FB to have its own unique instance, like you said. Note that you can nest the instance of an FB inside the instance of another FB calling it, it's called multi-instance.

There are certain edge cases where you would call an FB with the same instance more than once. As an example, some instructions trigger on the REQ bit going high. If you want to call the block every scan, if you leave the REQ bit high, it'll never see a rising edge, and thus never trigger. Thus, you call the block once with REQ high and once with REQ low, each scan, so that it triggers each time.

parky's mention of having many calls of the same FB sharing the same instance and then managing access to them in the program.... I mean, I guess you can. It depends what you're trying to do. If the instance of the FB is massive i could see it making sense to save space, but then it feels like you'd probably be pulling things in as pointers via INOUT anyway. It's not something I've seen done, at least with the current generation CPUs, and I feel like if that's a path you're considering, it might be a sign that you're approaching things the wrong way somehow somewhere.

Agree that, based on what you showed so far, you code would be perfect for an FC instead of an FB.
 
I will try to explain how it might work when compiled
Assume you want to make a block (call it a block because it could be a function or function block depending what you need to do.
This block is a simple one i.e. an add (I know there will be an add function but bear with me).
so you have two variables A & B and you need a result C the block will require two input vars & one output.
So in the block it will contain the logic ADD A + B out to C (IN (A) IN (B) OUT (Result)
When you call the block with an instance call it ADD_FUNCTION
Assume your variables are called Var_1 & Var_2 these contain say 3 & 5
The output variable is called Var_3.
When you call the block the compiler will do something like this
Main program
.
.
//Note: A, B, Result are the parameter assignments for the block
Call ADD_Function // just to indicate you are calling ther function
Move Var_1 > A //moves the var into the block memory
Move Var_2 > B // moves the second var into the block memory

JUMP ADD_FUNCTION //Jumps past this main routine after the end of the main program see code later
Move Result > Var_3 // when the function returns it moves the result back to the variable.
... Rest of program
....
....
END // End of cyclic program


ADD_FUNCTION: // program jumps here
ADD A & B MOV Result // adds the vars & moves it to result
RET // returns back to main program.

END
If you call this block many more times with different variables but as the same instance it jumps to the same code, however, if you create each one as a seperate instance then the compiler produces multiple add functions after the end of the main program.
So for example
Main code
Call Add_Function_1 (instance 1 of the function)
Call Add_Function_2 (instance 2) & so on
.
.
.
END

Add_Function_1 (first instance)
.
.
.
RET
Add_Function_2 (second instance
.
.
.
RET
& so on for as many instances you create.
EDIT: forgot to mention that I have just a true contact in the call to the function but if you wanted to have many using the same variables i.e. at particular parts of the program then just use some logic to call the function when required in that EN pin.
Here is an example of calling the same instance more than once with different variables (Note: the FB instance is the same name so only one block of code is generated, if the names were different then there would be two blocks of code ).

Multi call one instance.png
 
Last edited:
Q: Is there a way to copy a FB from one Project to another?

I ask because I created a scratch project as a safe place to develop the FB.

Now I'm happy the FB's working, I want to use it in a different project.

I tried copy/paste, but when the new project opens up, the buffer content is lost.


Reference project is one way, see LD's post. Fast and easy.



Creating your own Global Libraries is another, if you have objects you'll want to re-use often. Basically anything can be stored in a library, whether it's a block, several blocks, tags, HW devices, an HMI screen, a whole PLC, etc. These are master copies, where you just use it, and it's like it was copy/pasted, there is no link back to the library. Some objects can be a Type (FB/FC, HMI faceplate, UDT), where you actually get some limited version control in the library, and when you call it it remembers the version of the type it came from, so you can automatically update things as the library object gets improved.
 
Many thanks everyone.
I'm leaning towards an FC now (as opposed to FB), because there's only a single output here and none of the internal tags need to be retained in memory.
 
@ MK42:
there is no problem using the same instance as the data is copied to the function & back, it probably is not so important nowadays with most PLC's having plenty of memory, S7 is like the older S5 I believe so the code is compiled into MC7 (was MC5 in the S5 system), this is just a hex value where part of it is the instruction & the other the address of the variable. so there almost certainly be an small interpreter that uses the MC7 & interprets that into machine code relevant to the processor.
S7 probably has a structure much like the older S5, The memory in the PLC is structered very much like a hard disk. When a block is downloaded to the PLC it places it in memory & at a convenient time (probably after program scan) the information is put into an area of memory very much like a FAT table.
So when you modify a block & save it to the PLC it does not write over the existing block but creates a copy, after the scan the file allocation information of the existing block is replaced by the new information i.e. the address it is in memory in the early days of S5 the old blocks were not deleted & after a time the memory would fill up & you had to do a compress to tidy up the memory, at a later date Siemens made this automatic on a download.
This is why siemens is one of the few PLC's where you can download a whole project while in run mode, care is needed though by downloading blocks in order for example if you download a FB that calls say DB10 you need to ensure DB10 exists or it will shut down the PLC.
In S5 days DB's were limited to 255 words, in normal programming like Call DB10 Load DWxx, if you load DW256 > it failed there was a system to access above 255 called 20 bit addressing to access this. Also not well documented was the ability for the PLC programatically generate a DB in simple form this was a single instruction G DBxx I learned that it was possible on startup to check how much memory was free in the PLC & generate a DB to fill this space, This was used as a print buffer for a critical process in case the printer failed or ran out of paper, the trouble was that if the program needed to be changed there was not enough space to download the replacement block because as stated before the PLC needed spare memory to load the modified block before removing the old one. So it was required to delete the enormouse DB & compress the memory in stop mode before downloading the block & a cold + Warm re-start to generate the the large DB.
Another not well known programming way was a self modifying code, if you populated a DB with the MC5 instructions by processing the DW you could effectively have code in the dw that actually was code like A I0.0 SET Q2.0
It meant that the code could be modified during runtime for example if DB10.DBW10 held a hex MC5 code for load I0.0 then in the program at some point you replaced the hex code to load I0.1 it becomes self modifying code, not that I would do it but it was possible I tried it & had aimple program running that changed instructions as it ran.
I have not had much exposure to TIA but I bet there are some useful instructions where some of the above could be programmed.
Here is a list of some MC7 instructions
https://gist.github.com/nnaumenko/ecbf6c83569212bbc989ad8a923ea633
 

Similar Topics

hello every one. i'm new to tiaportal, i have created new project and HMI screen the program works fine with PLC-sim, but when i try to cntrol the...
Replies
9
Views
414
My PLC (S7-1200) and HMI (KTP-1200 Basic) has been delivered on-site to the customer. To be able to do "off-line" updates to the code, I am using...
Replies
4
Views
223
hi everyone. i hope you guys are doing great. i am trying to built communication between aveva intouch hmi 2023 and tia portal v18. i dont have...
Replies
1
Views
566
Hi, My PLC hardware has been delivered to customer site - many thanks for the invaluable help on this forum! I was looking into see if it's...
Replies
12
Views
2,507
Tia portal v18 issue,online icon greyed out but plc comms must be established as.blocks showing and no offline copy on laptop.
Replies
7
Views
1,103
Back
Top Bottom