Jovel

Data Types- Structure and Union

9 posts in this topic

Hi, I'm Jovel, I'm new to learning how to program PLC. Just want to learn how to use Omron Sysmac Studio . I have a question about the Data Type

Data Type - Structure ( Offset Type - Offset Byte )  / Union

 

I Don't Understand this 2 Data types and how going to use them, can anyone teach me and have a sample program? Thank you

Share this post


Link to post
Share on other sites

Hi!

As an example, I use an UNION called "unDataMemory" in which I create 2 members

- "w" as a WORD;

- "b" as an ARRAY[0..15] OF BOOL;

I use these union to creat an array of DMs to communicate via Datalink with older CPU like CJ2M types.



Basically is the same variable but you can use it as a word or as a boolean array.

Share this post


Link to post
Share on other sites

NJ/NX-series CPU Unit Software User’s Manual (W501) explain details (page 6-44).

It is used for data alignment. In CJ data is packed and NJ is like Microsoft C default structure alignment.
In User alignment you specify your needs with byte offset and bit offset.

Share this post


Link to post
Share on other sites

Sorry for refloating up the post but I still don't get what kind of practical uses this Union thing can offer. Is it used to give a variable the possiblity to assign two different kind of data types? for example, variable "person" can have a "34" INT value OR "James" String value?

I don't understand the manual explanation, my english is limited.

Share this post


Link to post
Share on other sites

It allows you to re-interpret the bytes that hold integer or boolean data as another format because the variables' memory allocation overlaps.  Similar to what can be done with byte array copies.  I don't find the unions to be very helpful, mainly because you are limited which data types are allowed.

2 people like this

Share this post


Link to post
Share on other sites

Hopefully this doesn't muddy the waters for you Artz but i will try to show an example of using a Union.

Basically, a Union is the method to allow you to access the memory area in different data formats. 
For instance, a Union can be created to translate the numeric value in a WORD to allow you to access the bytes and individual bits that are on in the WORD. 
The Union.jpg image I attached shows creation of several Union possibilities of WORD, BYTE, BIT combinations.

One case I have found for Unions that can be useful is when you have devices that require a binary input to select a program/job/schedule.
I can enter a number from the HMI and use the Union interpretation to set the binary outputs without have to create a "translate" function.


The other attached images show you basically how to do this. Map the high-level WORD value of a 16 pt output Card (I also map the individual outputs for reference, but the variables are not used individually). Then you just write numeric values to the BYTES of the Union variable and then map the WORD of the Union to the WORD variable of the output card. The corresponding physical output bits will be set.

In the example of the pictures the Union gives you the flexibility so that instead of writing to the BYTES of the Union variable you could also directly set the BITS of the Union although that defeats the purpose of wanting to use a numeric value.
 

Union.JPG

Module.JPG

 

 

 

 

Schedules by Value.JPG

Schedules by BIT.JPG

Edited by Str8jCkt
1 person likes this

Share this post


Link to post
Share on other sites

Great explanations from many folks on this. I primarily see unions used to extract-out or pack-in bits within a variable. A much easier way is to use the address assignment. It requires no programming instructions:

 

Capture.PNG

Share this post


Link to post
Share on other sites

If you are new to PLC programming you do not need to use structure text. I find it very complicated. you could do it in ladder. 

I am writing code for 2 plc's to talk to each other cj2s and a sysmac NX1  they both send 1 word 16 bits i extract the bits to use them in my ladder program 

once you have the union done you can just use the bits of the Array like contacts name_array[bitnumber] u can use them in your program 

screen3.jpg

screen2.jpg

 

screen1.jpg

Edited by mike_cheesehead
1 person likes this

Share this post


Link to post
Share on other sites

Hmmm I never had to deal with Bits or Bytes in ladder to be honest... so Unions seems unpractical for the moment... it's good to know it is there for any complexity that could show up though

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now