LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Union typedef ... failed

Solved!
Go to solution

Hi,

 

I've seen in a post that is possible to create an union in LabVIEW[and without using a dll]

but I don't understand the method, is there someone who can help me and explain how to

handle this trick in the following example : 

 


typedef struct _ssscmddata_haltest
{
      u8 test_flags; // Reserved, must be zero 
      u8 test_start; // SSSVAL_HAL_TSTART_xxxx
      u16 target_output; // Target output in volts or milli-amps
      u16 time_ramp_tgt; // Time in tenths of a second to ramp to target voltage
      u16 time_hold; // Time in tenths of a second to hold target voltage
      u16 time_ramp_zero; // Time in tenths of a second to ramp to zero volts


      union {
                  float r; // Generic access 
                  float milli_amps; // HIPOT. Used only at end of test for pass/fail determination. 
                  float mega_ohms; // DCIR. Used only at end of test for pass/fail determination. 
                  float milli_ohms; // EBOND. Used only at end of test for pass/fail determination.
               }lo_limit;

      union {
                  float r; // Generic access 
                  float milli_amps; // HIPOT. Test fails immediately if this limit exceeded. 
                  float mega_ohms; // DCIR. Used only at end of test for pass/fail determination. 
                  float milli_ohms; // EBOND. Used only at end of test for pass/fail determination.
               }hi_limit; 

      u8 arc_detect; // 0 thru 9, 0=off, 1=most sensitive, ..., 9=least sensitive 
      u8 channel; // HAL: 0, HALSCAN: 0 thru 4, G2 0=front, 1=rear
} ssscmddata_haltest;

  

 

Thanks in advance :-]

0 Kudos
Message 1 of 7
(3,306 Views)
Solution
Accepted by topic author Nelval

I may be a bit thick here, but what is the purpose of the union in your example?  I recall from my line code days that a union was designed to handle multiple data types in ONE block of memory.  Since your unions are all floats, you could simple use float lo_limit/hi_limit and be done without the need to use a union.  

 

without having searched the forum for unions, the closest direct structure I can think of to do this would be an array of U8s, initialized to the specific number of bytes of your largest union 'variable'.   Then implement a LV2 style global to keep from allocating new memory.  What I am not sure of is the efficiency of the conversions into and out of said array.  I also do not know if you save a String with a specific number of characters if Labview will not reallocate memory anyway.

 

-p 

 

 


Paul
0 Kudos
Message 2 of 7
(3,291 Views)

The datatype in LabVIEW that most closely resembles a union would be variant. You would need to do the required conversion to your specific data type but a variant will allow you to assign it different data types. The other method would be to use flatten to string. When unflattened you get your data back.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 3 of 7
(3,280 Views)

@Mark Yedinak wrote:

The datatype in LabVIEW that most closely resembles a union would be variant. You would need to do the required conversion to your specific data type but a variant will allow you to assign it different data types. The other method would be to use flatten to string. When unflattened you get your data back.


Mark, the only thing with those two solutions is that they do not allocate a specific block of memory of fixed size and hold onto it.  The whole purpose of the union is to allow different datatypes to store data in a finite block of memory allocated by the largest type size.  The only way I know to come close to that is a fixed array and then convert data in and out of that, but then again, I am not sure that the memory allocation to do the conversions wouldn't be more costly.  

 

 


Paul
0 Kudos
Message 4 of 7
(3,251 Views)

@PJS wrote:

@Mark Yedinak wrote:

The datatype in LabVIEW that most closely resembles a union would be variant. You would need to do the required conversion to your specific data type but a variant will allow you to assign it different data types. The other method would be to use flatten to string. When unflattened you get your data back.


Mark, the only thing with those two solutions is that they do not allocate a specific block of memory of fixed size and hold onto it.  The whole purpose of the union is to allow different datatypes to store data in a finite block of memory allocated by the largest type size.  The only way I know to come close to that is a fixed array and then convert data in and out of that, but then again, I am not sure that the memory allocation to do the conversions wouldn't be more costly.  

 

 


While I agree with what you are saying the issue of memory management is somewhat mute. LabVIEW, unlike many other languages handles the memory management for you. In most cases there is no need to worry about it. As LabVIEW moves more towards embedded solutions more control over memory management may be necessary. However, if a new person to LabVIEW is simply looking to do things in the same way then they may not realize it isn't necessary to worry about it.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 5 of 7
(3,246 Views)

Yup you're right, I'm just thinking about the main purpose of a union: it's for one variable having the choice among several types.

I'm gonna try your method 😄 

0 Kudos
Message 6 of 7
(3,213 Views)

It works

Thank you so much Smiley Happy

0 Kudos
Message 7 of 7
(3,183 Views)