Union type construct in CLX ST

Mr Modbus

Member
Join Date
Jan 2014
Location
Lincoln
Posts
24
There is a data construct called a union in the 'c' programming language. This construct allows for several variables types to be overlaid at the same memory address. This is very useful for decoding bits out of a word. The S7 system has a equivalent way of achieving the same result using the AT construct thus:-

wData : WORD;
bData AT wData : STRUCT
Bit0:BOOL;
Bit1:BOOL;
Bit2:BOOL;
Bit3:BOOL;
Bit4:BOOL;
Bit5:BOOL;
Bit6:BOOL;
Bit7:BOOL;
Bit8:BOOL;
Bit9:BOOL;
Bit10:BOOL;
Bit11:BOOL;
Bit12:BOOL;
Bit13:BOOL;
Bit14:BOOL;
Bit15:BOOL;
END_STRUCT;

Is there anything similar in CLX ST?
I realise I could achieve the same with masks and shifts or by copying the word data to another structure. However the form requires a lot more code and the latter requires more memory usage. I would looking for a direct equivalent if there is one.
Any ideas anyone?
 
You can't do union type structures directly in Rslogix 5000, but similar can be done using XML export with hidden elements
see pub 1756-rm084 page 65-68
 
UDTs with bit overlays are the closest thing. Once implemented it is available to all Logix5000 languages. Its a powerful tool for the advanced programmer and one of the few advanced program techniques that at the same time makes the program more readable for Bubba and Otis. The one drawback is that a bit overlay cannot be changed online or offline. Once it is created to change it you have to delete it and re-import it.

Here is an example:
http://www.plctalk.net/qanda/showthread.php?t=59049
 
Last edited:
Thanks guys, that will suffice.
It's not ideal as you cannot edit the definition within the programming environment, but I can live with that.
For your information, I raised this with the official RA forum and you have beaten their response time by some considerable margin - in fact they are yet to reply :)
 
BTW, what you are describing in the first post is technically known as a bit field in C.

A union is a data structure that can hold different kinds of data in the same word, for example it can hold both an integer or a float at the same address - something that can be handy if doing modbus comms of float data in C.
 
BTW, what you are describing in the first post is technically known as a bit field in C.
Well in my particular example that is true as unions cannot support Boolean's as members of the union. So I concede the point :) In my defense, it was a bad example that I cited. I should have stated that also need the facility to say split a word into 4 bytes, which would fit my union analogy better.
The important point that I was trying to get across is that I wanted the sub-types overlaid over the base type at the same address.
 
Got it. I know this isn't quite the same, but if you use a UDT and define an array of SINT within it then it will occupy just 32 bits. You can then COP a DINT to UDT.SintArray[0] and fill all four SINTs or the other way around. Not sure if that helps you or not.

BTW, welcome to the forum.
 
Thanks for the welcome.
I think I can make something work now I know about overlaying types. Just a shame that you have to do it using an external XML file.
This PLC programming is such a reality wake up call after coming from the world of 'c' and assembler. It takes me weeks to do what I could get done in minutes in 'c'. At least ST is better then ladder. I hate ladder. It took me 3 days to write a traffic light example in ladder and it still didn't work correctly. I think I could done it in 'c' in 3 minutes and use about 1/10 of the code space.
 
I raised this with the official RA forum
I hope you understand that AB forum is not official AB support place and is not handled by AB support people, most of questions answered by users like you.

And I hope you realized that you are getting direct AB anwers here.
 
I hope you understand that AB forum is not official AB support place and is not handled by AB support people, most of questions answered by users like you.

And I hope you realized that you are getting direct AB anwers here.
Err no and err no. So please excuse my apparent ignorance.
I do now though :whistle:.
Thanks.
 
I came into the automation world with a background in C. I had been doing automation for a couple of years before I did my first PLC - I started on DEC PDP-8As using assembly language.

Don't write off ladder so quickly. It has its place and its easy for the guys who will have to service the equipment down the road to understand. If Bubba and Otis on the graveyard maintenance shift can understand it is less likely that they will call you at 3:00 AM in the morning.

Also, if you rate a language level by how far it removes you from the machine level, ladder is a higher level languge than you might at first think it is. Consider what you have to do in C to read or write the state of a single bit in a word. In c you first have to build a mask and then bitwise-AND the word with the mask and then do a compare, or to write a bit you have to build a mask and then do a bitwise OR. In ladder its just one instruction, XIC, all the rest is handled by the compiler/interpreter. In that respect ladder has a higher level of abstraction than C does. I agree that ladder can be cumbersome to a C programmer because I've been there myself. Keep an open mind though and learn it because it is not going away. You'll soon recognize that it excels for solving boolean logic and simplifies writing for it, which is what it was specifically designed for.

If you are going to be a really good automation engineer you need to learn all the tools that are available to you and where best to use each one.
 
Last edited:
OK I am experimenting a bit. What I have found out is that I cannot seem to import / export data types as l5k file types. The only option seems to be l5x file types. I would prefer l5k format as it is a lot easier to write than the xml l5x format.

I also followed the example in the manual and produced an xml file but it will not import.
This is the file:-
<DataType Name="bData" Family="NoFamily" Class="User">
<Members>
<Member Name="wData" DataType="INT" Dimension="0" Radix="Decimal" Hidden="true" />
<Member Name="bData0" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="0" />
<Member Name="bData1" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="1" />
<Member Name="bData2" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="2" />
<Member Name="bData3" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="3" />
<Member Name="bData4" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="4" />
<Member Name="bData5" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="5" />
<Member Name="bData6" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="6" />
<Member Name="bData7" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="7" />
<Member Name="bData8" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="8" />
<Member Name="bData9" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="9" />
<Member Name="bData10" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="10" />
<Member Name="bData11" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="11" />
<Member Name="bData12" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="12" />
<Member Name="bData13" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="13" />
<Member Name="bData14" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="14" />
<Member Name="bData15" DataType="BIT" Dimension="0" Radix="Binary" Hidden="false" Target="wData" BitNumber="15" />
</Members>
</DataType>

I have compared this with an exported data type that I defined in RSLogix 5000. The exported file has some additional header and footer information in it. Is this extra information required? It is not shown as being needed in the example in the manual.
 
Last edited:
What I have found out is that I cannot seem to import / export data types as l5k file types. The only option seems to be l5x file types. I would prefer l5k format as it is a lot easier to write than the xml l5x format.
L5K is for complete project only.
Datatype Export is only L5X XML format

The exported file has some additional header and footer information in it. Is this extra information required?
Yes, you must have a header that tells Import engine about your XML code. This is not optional.
 

Similar Topics

I declare a structure: TYPE sdoName: STRUCT i1: INT; i2: INT; END_STRUCT END_TYPE I declare a UNION: TYPE uName: UNION di : DINT...
Replies
14
Views
3,072
This machine we are dealing with has a bunch of these, and we haven't been able to ID them. The thread is straight metric 1.5M, but that's about...
Replies
6
Views
4,521
Has there been any recent changes regarding the use of AWG wire sizes in panels exported to the EU? Its been about 10 years since I sent a panel...
Replies
4
Views
6,346
Hey guys, I just wanted to see if any of the bright minds on here had any opinions on these relay interface modules. Particularly, I would love to...
Replies
1
Views
30
Hello, I'm hoping someone can help me with understanding how to set/change these data types. We recently swapped from Invertek to AB drives...
Replies
1
Views
77
Back
Top Bottom