Question: bit access and VAR_IN_OUT?

titi_nicolas

Member
Join Date
Apr 2009
Location
Lausanne
Posts
5
Hello,

I am developing with TwinCAT-PLC on Beckhoff.
I have a FB with a VAR_IN_OUT variable (lets say "code :INT") and I want to know if I can bit access this variable as I usually do when the variable is not a VAR_IN_OUT (eg "code.5 :=TRUE").

I am asking because I read here that (I quote):

[FONT=Arial,Bold][FONT=Arial,Bold]
Addressing bits in variables
[/FONT][/FONT]
In integer variables, individual bits can be accessed. For this, the index of the bit to be addressed is appended to the variable, separated by a dot. The bit-index can be given by any constant. Indexing is 0-based. Example:
a : INT;
b : BOOL;
...
a.2 := b;
...
A bit access must not be assigned to a VAR_IN_OUT variable!
---------------------------------------------------------------------------------------------------------------

Is this right?

The quote is NOT from a beckhoff manual and I could not find such warning anywhere else. That is why I am asking here!

I gave a try on a Beckhoff system and the code compiles without errors and runs fine. But still, I am afraid of missing something important and I don't want to get, one day, a "memory corruption" or something else strange.

Thank you if somebody can help!
 
Actually, I did found the exact same quote in the TwinCAT-PLC help.

So, what is the reason for this?
Is there a way to do the same thing without bit accessing a VAR_IN_OUT variable?
 
My guess is as follows:

A VAR_IN_OUT variable is passed into a function block by reference: when a function block receives such variable, it actually receives the pointer to it, i.e. the address in memory where the variable is stored. So, if you try to modify individual bits, you will modify the pointer, not the value. The results can be unpredictable and potentially damaging.

Any other input variables are passed into a function block by value.
 
Technically that is an error but the error message isn't very good

I tried that on our controller. Our controller complained "Error, the variable tag name does not exit". The reason is that 'a' is an INT. Our controller doesn't see a.2 as a valid tag. It should work if a is a WORD or DWORD. To make this work I had to
dw:=DINT_TO_DWORD(a); // We/DSPs support 32 bit registers only
dw.2:=b;
a:=DWORD_TO_DINT(dw);
Its an IEC 61131-3 thing.
 

Similar Topics

Hi, Expert: Is there any possible MAM PC bit turning on before reaching the position? Thanks!
Replies
3
Views
1,995
Good Afternoon, I’m looking at some examples in a SLC 500 and I took notice of a Bit Shift that has the File position with a # B3:25 . What...
Replies
4
Views
1,691
I tried searching but I came up empty handed, maybe it was the way I worded it. Anyways, I’m very meticulous about how I label all of my...
Replies
6
Views
1,765
Ok Red Lion guru's....... I have to make sure I can grab data in 3 separate 16-bit tags and assemble only the USABLE data in each one of these...
Replies
2
Views
1,566
New to Keyence--can some one clarify the difference between MR20000 output & LR20000 output bit. Having to rework a machine and just trying to...
Replies
2
Views
1,377
Back
Top Bottom