LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unions like C and ordered structures

LabVIEW has no unions. LabVIEW is a strict datatype language and unions are the antithesis of strict datatypes.

 

But unions are simply syntactic sugar for lazy typecasts. And typecasts are generally considered the evil in person in programming since it makes all sorts of errors very easy to commit.

 

In LabVIEW the equivalent to a struct is a cluster. That is not a problem. You just have to consider the bit size of the individual elements and possible alignment requirements. LabVIEW itself does not use alignment bits in the flattened representation of its datatypes. So if your struct is aligned you have to explicitly add the according alignment elements in the LabVIEW cluster.

 

To set the correct order of the elements in a cluster you right click on the cluster border and select "Reorder Controls in Cluster".

 

You should not use Typecast to flatten or unflatten a LabVIEW cluster to a binary stream unless you are sure your flattened data is in big endian form. Instead use the Flatten and Unflatten functions where you can explicitly select which endian form it should use.

 

For boolean elements in a numeric value, you use boolean operators. The AND, OR, and XOR nodes applied to a numeric element perform boolean operations.

 

To see if a specific element in a numeric is set you AND it with the according bit value and then test for !=0

 

To set a specific bit in a numeric you OR the numeric with the according bit value. To clear a specific bit you invert the according bit value (binary complement) and AND that with the numeric. That's it!

 

Yes, it is not as trivial as simply addressing the alternative union bit value in C and similar languages but LabVIEW does not only not know unions but also not bit arrays. LabVIEW boolean arrays are in reality arrays of uint8 numerics with only the values 0 and NOT 0.

 

The likelihood that LabVIEW ever will support unions before version 3000 is close to 0, so don't hold your breath for that. 😁

Rolf Kalbermatter
My Blog
Message 11 of 13
(772 Views)

To set the correct order of the elements in a cluster you right click on the cluster border and select "Reorder Controls in Cluster".

That is the trick I need, and I can't find in manuals. Thank you.

 

The likelihood that LabVIEW ever will support unions before version 3000 is close to 0, so don't hold your breath for that. :faccia_raggiante_con_occhi_sorridenti:

Pufff... I restarted breathing... 😀

 

Thanks, Rolfs, that's all I need.

I often use Unions in microcontroller and PLC programming, because they are small programs and I can control well the correct use of them. As I told, I'm coming from oooold programming skills, so a (bad?) habit is difficult to abandon.

Thanks to the community for the help

 

0 Kudos
Message 12 of 13
(762 Views)

@AndyPF wrote:

To set the correct order of the elements in a cluster you right click on the cluster border and select "Reorder Controls in Cluster".

That is the trick I need, and I can't find in manuals. Thank you.


In the very first answer, I already said you can define the cluster order!. I would not call it a trick and it is well documented.

 

While cluster elements can be arranged at will by default, if you set the cluster to "right-click ... autosizing ... arrange vertically/horizontally", the elements will be arranged in cluster order and it is very easy to tell what the order is and verify that it is what you think. (... unless some elements are set to not be visible).

 

altenbach_0-1719844280134.png

 

Message 13 of 13
(737 Views)