LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a bit field (multiple bits selectable) structure?

I'm develping a program to communicate with an external device, and it has a specific protocol. Often times, I need to make structs/typedefs of specific type (u8/u16/u32) that have values defined by multiple selection. For example, the protocol asks for the following:

 

The field is a bit field structure (multiple bits can be set simultaneously) defined as follows:

typedef u_16 COMM_TYPES;

#define A 0x8000

#define B 0x4000

#define C 0x2000

#define D 0x1000

 

 

How would you recommend implementing this? I've tried looping through a listbox in combination with a text ring that has the name and value pairs, but its cumbersome doing for each unique typedef (see image below)

 

bitfield attempt 1.png

0 Kudos
Message 1 of 4
(4,375 Views)

That's what I would do.  Other than I might use OR instead of sum, but that shouldn't make a difference.

 

The other possibility would be to take the array of index values,  to a 2^x where x the array of indices, then add that.  This assumes that every index in the array matches bit for bit with the enum values.  Nothing is missing, and array element 0 corresponds to bit 0.  In your case, your order is reversed, but you can reverse the array.  And all your values are in the highest nibble of the highest byte (hex ?000)  So you'd have to do some bit shifting or multiplication to get the actual values.

 

But I think this would make your code that much more fragile.  I would just do what you have shown.

0 Kudos
Message 2 of 4
(4,362 Views)
I think I would use a ring control with nonsequential values. No parsing, manipulation or parsing needed. What they select IS the value.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 3 of 4
(4,344 Views)

I thought he had a ring going on in there.

 

Now that I copied the VI snipped so I could see it, and play with it, I see that it does, but  the snippet is a lot more convoluted than it needs to be.  I can't even tell what the user experience with this VI is supposed to be.

0 Kudos
Message 4 of 4
(4,329 Views)