TwinCat structure of bools from status int

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
960
I have an INT variable containing the value of a status word. I have also created a structure of 16 BOOL. Instead of having to manually set each structure element to my INT.bit, I am attempting to use the trick of addressing two variables at the same address.

Example:
ABB_StatWord_Intermediate AT %MB1000: INT;
ABB_Status AT %MB1000: ST_ABB_STATUS_WORD;


My Structure looks like:
TYPE ST_ABB_STATUS_WORD :
STRUCT
Ready: BOOL;
SwitchedOn: BOOL;
OpEnabled: BOOL;
Fault: BOOL;
VoltEnabled: BOOL;
QuickStopActive: BOOL;
Bit6: BOOL;
Warning: BOOL;
Bit8: BOOL;
Bit9: BOOL;
Bit10: BOOL;
Bit11: BOOL;
Bit12: BOOL;
Bit13: BOOL;
Bit14: BOOL;
Bit15: BOOL;

END_STRUCT
END_TYPE


This is not working, as the first two structure elements bascially becom INVALID.

What am I missing?
 
TwinCAT does not "pack" Booleans of a structure. Each Boolean of the structure takes 1 byte in memory.

You would probably have to create a function that transfers each bit of the word into the individual elements of the structure.
 
I have been in this situation before and if memory serves me I think that changing the BOOLs to BITs in your structure will fix the problem as Pireciter suggested.
 
At the end of the day, I am only interested in 6 or so of the bits. I just did it the normal way:
ABB_Status.Ready := ABB_StatWord_From_OverEcat.0;
ABB_Status.SwitchedOn:= ABB_StatWord_From_OverEcat.1;
etc.

I will play with some of this info at home.

Thanks
 
It looks like TwinCat2 does not support the BIT.


So I found a work around that seems to work very well.
I created an instance of the structure and a 16 element array of BOOL addressed at the same location:
VAR_GLOBAL
MyInt : UINT;
MyBoolArray AT %MW100: ARRAY [0..15] OF BOOL;
MyStruct AT %MW100: ST_ABB_STATUSWORD;
END_VAR

My Test structure definition is:
TYPE ST_ABB_STATUSWORD :
STRUCT
Bool0 : BOOL ;
Bool1 : BOOL ;
Bool2 : BOOL ;
Bool3 : BOOL ;
Bool4 : BOOL ;
Bool5 : BOOL ;
Bool6 : BOOL ;
Bool7 : BOOL ;
Bool8 : BOOL ;
Bool9 : BOOL ;
Bool10 : BOOL ;
Bool11 : BOOL ;
Bool12 : BOOL ;
Bool13 : BOOL ;
Bool14 : BOOL ;
Bool15 : BOOL ;

END_STRUCT
END_TYPE


I then created a simple FB:

FUNCTION_BLOCK FB_IntToBoolArray
VAR_INPUT
InputInt : INT;

END_VAR
VAR_OUTPUT
OutputBoolArr: ARRAY [0..15] OF BOOL;

END_VAR
VAR
END_VAR


OutputBoolArr[0] := InputInt.0 ;
OutputBoolArr[1] := InputInt.1 ;
OutputBoolArr[2] := InputInt.2 ;
OutputBoolArr[3] := InputInt.3 ;
OutputBoolArr[4] := InputInt.4 ;
OutputBoolArr[5] := InputInt.5 ;
OutputBoolArr[6] := InputInt.6 ;
OutputBoolArr[7] := InputInt.7 ;
OutputBoolArr[8] := InputInt.8 ;
OutputBoolArr[9] := InputInt.9 ;
OutputBoolArr[10] := InputInt.10 ;
OutputBoolArr[11] := InputInt.11 ;
OutputBoolArr[12] := InputInt.12 ;
OutputBoolArr[13] := InputInt.13 ;
OutputBoolArr[14] := InputInt.14 ;
OutputBoolArr[15] := InputInt.15 ;

###############


This works very well. I did this on my home test/practice CX8090, I will implement it back into the project/system I was working on today.

PIc3.PNG
 

Similar Topics

Hi! I am trying to run the 'SimpleSample' (https://infosys.beckhoff.com/content/1033/TF5100_TC3_NC_I/Resources/3438746891/.zip ) to understand the...
Replies
2
Views
96
I am developing a library in twincat and one of the function uses IID_ITcVnAccess_TcVnPoint2_DINT and the definition of this type is defined in...
Replies
0
Views
69
Sorry if this has been asked before, and apologies if this seems like a trivial issue, but I am new to Beckhoff and have been banging my head...
Replies
2
Views
141
Hi everyone, This is my first time posting, so please forgive any omissions or mistakes. I am attempting to control the velocity of a stepper...
Replies
18
Views
950
I am trying to communicate between a 1769-L16 and a beckhoff EL6652 EtherNet/IP-Master. I need 8 INT and 2 REALS. When I generate the eds file, in...
Replies
1
Views
161
Back
Top Bottom