VAR_IN / OUT VS VAR_IN__OUT for Persistent Variables

Sham

Member
Join Date
Sep 2019
Location
Australia
Posts
152
Hi All,

Can anyone please explain the difference between var_in_out and Var_In / Var_Out?can anyone please give an example that can make me understand var_in_out variables?
Can I use Var_Out for persistent variables? or the values will get overwritten? Will it be better to use var_in_out for the persisten variables?

Thanks
 
Firstly: What system are we talking about?


Generally In_Out is a variable that you can both read as an input and write as an output.
Example would be if you have a HMI with a push button you may create a latch in the program but at trip you reset it.


What do you mean by persistant variables? Do you mean a variable with a constant value? In that case you can use some form of VAR CONSTANT, but you need to define the constant value.
If you mean a value that is persistant through out a power cycle then you want to look at some kind of retain.
 
Assuming this is a CodeSys question...
var_in can only be written to by the main program. It cannot be manipulated inside of a function block.

var_out can only be written to by the function block. It cannot be manipulated by the main program.

var_in_out can be written to inside and outside of the function block.
 
VAR_IN_OUTs pass a reference to the FB rather than the value i.e. the FB works directly on thae external address.
This is why FB INs and OUTs can be omitted, but VAR_IN_OUTs can't
 
The IN parameters can pass information IN (you cannot write to it inside the FB
The out parameters can only be written to inside the FB
That is why they created the IN/OUT.
An example is where you want a parameter say an integer where it needs to be exposed to both the outside programs and internally in the FB like a count value. So the idea is you can pass a value to the FB modify it and return it back to the main program.
The way a FB works is all inputs are passed to temp variables, all outputs are returned but within the function block you cannot write to input variables and vice versa.
FB:
IN My_In_Var_1 passed as a reference cannot be written to inside FB
IN My_In_Var_2 Same as above
OUT My_Out_Var_1 Can be written to in the FB
IN/OUT My_In_Out_Var_1 Can be written to and read in the FB.
The compiler does not allow you to write to IN and does not allow you to read OUT
This is because all input parameters are passed to the temps but those temps are not passed back
The opposite is for OUT.
 
The compiler does let you write to Var_in, the point parky is making is that you are writing to a copy of the variable that was taken when the FB was called. This particular compiler (Nidec Machine Control Studio) doesn't give you a warning.

inout1.jpg
 
With Siemens FB's, in_outs are either passed by value for 'simple' data types (bool, int, etc.) or by reference for 'complex' data types (arrays, structs etc.)

inout2.jpg
 
Yes my point was that the in parameter although in most IDE's you could write to it inside the function it will not return the modified value, as explained before the variables (used in the calling program) are passed to temporary variables for the function block to use, and the OUT are passed back to the calling program, so altering the IN variable (used inside the FB) is not passed back.
When Mitsubishi first developed the IEC programming (GXIEC) instead of producing a new hardware they made it so it was sort of compatible with the existing platforms. so when you create a FB then the compiler allocates existing memory i.e. D & M (word & bit) in the upper range to use as "SCRATCH" memory, so out of the D memory D0 to D9999 then the upper few hundred words are reserved for use in temporary variables in FB's, these can be accessed directly in the program but as these may be re-used by the functions a number of times the data may not be valid.
In the pics below is a simple call to a simple FB (not that you would use it as it creates more code but just is a simple example of how a FB and it's parameters are passed, and in some platforms it is assumed there is memory allocated for these local variables for the FB.
In the pics below you can see the actual FBD code where a FB is a simple ADD and is called in a program.
The actual compiled code is in the second pic.
The plc compiled code is as follows, the IN variables are moved to the temps
D12286 & D12287, it then jumps past the end of the sequence program to the pointer of the function the function ADD uses these and stores it in D12285 , it then returns back to the program and moves the return value back to the OUT variable.
Many years ago when SLC500 came out I had to write a program that required many bits of code that were the same, as I was used to Siemens who already had FB's and my knowledge of many programming languages including assembler I created a program that used my reserved N & B variables as temps, created the program block then in another program block transferred the required variables for each call to the temps, jumped to my "FB (standard program block) then after this moved any output variables from the temps back to my Effective OUT variables, this saved a lot of programming.

program.png Compiled.png
 

Similar Topics

Hello Inside a FB, I´m trying to transfer a string from a DB to a IN_OUT var that was define as a UDT. The problem is that i can´t determine the...
Replies
4
Views
110
I'm trying to read modbus data from modbus slave using Read_VAR function. I'm getting an error that my device is not linked to a module. I have a...
Replies
1
Views
132
All, We have a site that has multiple Altivar 66's that are being controlled by an old Square D Modbus+ network. One of the VFD's has failed...
Replies
2
Views
361
Hi, I'm using Control Expert (a.k.a Unity Pro) to program M340 PLC. I am looking for a patch to fix what I think is a bug in PLC library. Please...
Replies
10
Views
976
Hi! I am using a TM200CE40T PLC from Schneider to write data over Modbus. I have used Memory words (%MW) before using the Write variable...
Replies
1
Views
521
Back
Top Bottom