Simatic S7-1200/1500 InOut parameter problem

pnowak

Member
Join Date
Jul 2010
Location
Control room
Posts
5
This thread is reaction to another two:
https://support.industry.siemens.com/tf/ww/en/posts/inout-parameter-problem/260345
https://support.industry.siemens.co...en-ob-interrupts-fb/269783?page=0&pageSize=10


I do have MAIN OB1 and fast task OB31. Both write to the same BOOL output %Qx.y. I use some global variable to synchronize both OBs who uses BOOL output. Sometimes OB1, othertimes OB31.
All works perfectly. The magic lies in fact that if one OB doesn't have priority to write to output it simply doesn't write anything = do nothing and the other OBx can write to output.

The situation gets complicated as soon as I need more instances of functionality executed by OB1+OB31. I put functionality into some FB (in OB1 or OB31 or probably both). FB has InOut formal parameter. The real bool output is assigned to formal parameter during FB call. And because elementary InOut parameters is "by value" and not "by ref" (why Siemens, why ???!!!) the magic is broken. InOut is always copied to real parameter at the end of FB call, whether or not the formal parameter was written inside of FB (if not, the last value of InOut is used).

Has anybody hit this problem? What strategy did you use to overcome it ? Any thoughs ?

Some ideas (some from threads mentioned above):

1. If the number of instances is low I can select real output by use of some instance identification (use CASE OF) and write to output inside of FB. "By ref" isn't broken. It works. But this solution is very bad (encapsulation principle broken, every change in output address, number of instances etc deserves rewriting a lot of code; bad readability).

2. Write to 3 state output variable inside FB (TRUE/FALSE/do nothing) and write(false/true) or donothing to real output outside of FB. This deserve some code after FB call.

3. Remember value of real output before FB call. After FB call compare actual and remembered value od real output. If it's unchanged use write output of FB to real output. It seems as simple solution but I think it has two problems: first it doesn't respect what really happen inside FB call (it ignores synchronization between OB1-OB31) and second: if OB31 interrupts OB1 and changes real output value exactly after comparing remembered/actual value and before writing real output this could work wrongly.

4. OB1-FB simply doesn't write to real output but sends write command to OB31 that executes write to real output. I think this is principially the same as solution 2.

5. Declare InOut parameter as UDT (UDT parameters are always "by ref"). Impossible. Can't assign real parameter BOOL (real output) to formal parameter UDT.


Whole problem is caused by Siemens decisison that formal elementary InOut parameters are by value. But elementary InOut by value could be easily reached by combination of one In and one Out. And InOut could be by ref. I understand it's probably caused by history and backward compatibility. But this has easy solution - compiler option.


------------------------------
//EXAMPLE

//OB1 - MAIN - about 10ms

...some code...

FBxx(inoutPar1 := outBool%Qx.y);
//PROBLEM: FB call end always writes some value to outBool%Qx.y even if "do nothing" variant happened inside FB

...some code...


//end of OB1
--------------
//FBxx

IF condition THEN
inoutPar1 := TRUE;
ELSEIF condition2 THEN
inoutPar1 := FALSE;
ELSE
//do nothing
END_IF;

----------------
//OB31 - 1ms

...some code...

IF condition3 THEN
outBool%Qx.y:p := TRUE;
ELSEIF condition4 THEN
outBool%Qx.y:p := FALSE;
ELSE
//do nothing
END_IF;

...some code...

//end of OB31
--------------
 
Last edited:
Only have 1 FB or FC that writes to the outputs.
Call this FB or FC from OB1 and OB31.
Let the code in the FB or FC decide how the outputs behave. If the outputs must behave differently based on the call is via OB1 or OB31, then pass the information to the FB or FC and handle the difference inside the FB or FC.
 
Only have 1 FB or FC that writes to the outputs.
Call this FB or FC from OB1 and OB31.
Let the code in the FB or FC decide how the outputs behave. If the outputs must behave differently based on the call is via OB1 or OB31, then pass the information to the FB or FC and handle the difference inside the FB or FC.

I need the output be parametrizable so I can have more instances of that functionality. If I create FB/FC writing to one concrete output it isn't solution.

So the problem is not synchronicity between OB1 and OB31. The problem is FB/FC outputs InOut are not by ref.
 
If you program OUT pins on FCs or FBs, then these outputs are by nature 'parameterizable'.
That is what FBs and FCs are about.

You are making this unnecessarily complex.
What is stopping you from doing what I suggest ?
 
If you program OUT pins on FCs or FBs, then these outputs are by nature 'parameterizable'.
That is what FBs and FCs are about.

You are making this unnecessarily complex.
What is stopping you from doing what I suggest ?

I'm afraid I don't understand your solution. I can have one FB/FC writing to output called by OB1 and OB31 too. But how I can instantiate whole functionality to more instances ?
I have 3 instances of FB. Every instance writes to another output. How to solve this?
 
You can call the same FB with the same instance-DB several times, so in both OB1 and OB31 if you so wish.
You are in principle calling the same 'object'.

But how I can instantiate whole functionality to more instances ?
You are misusing the terms 'instantiate' and 'instances'.
When you call an FB with the same instance-DB several times (in OB1 and in OB31) then it is not 'instances', it is merely 'calls'.

I have 3 instances of FB. Every instance writes to another output. How to solve this?
In that case it is same FB with different instance-DB. That is a case of different 'instances'.
 
Change the parameter from BOOL to ANY and then you can choose to write to the Q address or not. Call will look the same.

byref.jpg
 
As this needs to apply to both the S7-1200 and S7-1500, I should have said Variant instead of ANY
Example shown below:

var1.jpg
 

Similar Topics

HI i would like to know how to get a variable that will store the amount of times a program has been executed. The issue is I have 3 DBs for 1 FB...
Replies
2
Views
82
Good Evening , It has been several years since I used Siemens Software . I’m looking to purchase the software for a Siemens Simatic S7...
Replies
4
Views
498
Hello.We are trying to convert a program written with S7-300 Simatic Manager to S7-1200 V16.Unfortunately, there is only LAD,FBD option in S7-1200...
Replies
2
Views
754
I have new Siemens PLC / Simatic S7-1200 Need software download to use the PLC Please advise Thanks Wally
Replies
8
Views
2,241
I have new Siemens PLC / Simatic S7-1200 Need software download to use the PLC Please advise Thanks Wally
Replies
1
Views
643
Back
Top Bottom