Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Contact Us
  • Home
  • Products
  • Software
  • Sysmac Studio

How to Use Unions in Sysmac Studio

Written by Nick Lim

Updated at March 25th, 2025

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Products
    Automation Systems Control Components Motion & Drives Quality control & Inspection Robotics Safety Sensing Services Software Switching Components
  • Amplify e-Store
    e-Store public
  • Public CSC
+ More

What is a Union?

A union is a data structure that allows different data types to occupy the same memory location. Unlike a struct (which stores each member at a different memory location), a union enables several variables to share the same space, which can be used to save memory or interpret the same memory space in multiple ways.

The most common usage of unions would be found in processing control words.

Unions members occupy the same memory location

Modifying one member of a union will affect other members, so care must be taken when using them.

 

Creating Unions

1. In the Multiview Explorer select Data Types. Then select Union on the menu on the left.

2. Right click in the main edit window and click create new data type

3.  Give your union a name and then right click and select create new member

4. Give your member a name a data type then repeat 3 and 4 as necessary
     N.B. You can only use primitive data types when creating a union (Bools, Bytes, Words, DWords, LWords)

5. Declare your union as a variable using the Union data type you just created

6. Finally you can access members of the union using the following syntax [variable name] . [member name]

 
 

Example Usage

Take for an example we receive the control word 0x0F and we interested in the status of bit 2 and 3.
Breaking this down into binary it would be 0000 1111.
There is however no way of directly comparing to an individual bit in a word built into Sysmac and using a greater than operation would not work as ≥4(0000 0100) would not account for bit 3, ≥8(0000 1000) does not account for bit 2 and ≥C(0000 1100) would not work as 0x10 (0001 0000) is also a possible solution.

However using the union in our previous example you can see we can operate on individual elements in our bit array

 
 

 

utilization unions

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • Indirect addressing DM in CX-Programmer
  • How to Backup and Restore a Sysmac Project to a SD card
  • Procedure of Manually Uninstalling the Sysmac Studio

Definition by Author

0
0
Expand