From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to programatically get enum strings in RT code?

Hi all,

 

In desktop programs, I can programatically get a list of enum strings by creating a (possibly-hidden) FP control, linking it to a property node, and reading from the Strings[] property.

 

Unfortunately, property nodes aren't supported on RT targets. What other options are there?

 

 

Thanks in advance!

Certified LabVIEW Developer
0 Kudos
Message 1 of 7
(3,750 Views)

Does GetNumericInfo.vi work on RT?

0 Kudos
Message 2 of 7
(3,745 Views)

Thanks for the lead; I'll look into GetNumericInfo.vi

Certified LabVIEW Developer
0 Kudos
Message 3 of 7
(3,732 Views)

One trick that has worked since at least LabVIEW 3 is to use the String/Number Conversion->Format Value function. Format specifier "%s" will translate an enum in a string. The same format specifier also works with the Format into String node, but here the format specifier is even optional since the string conversion is the default.

Enum String conversion.png

Rolf Kalbermatter
My Blog
Message 4 of 7
(3,673 Views)

See attached VIs for two different ways to do it.

"If you weren't supposed to push it, it wouldn't be a button."
Download All
0 Kudos
Message 5 of 7
(3,647 Views)

Forgot to include a subVI in previous post.  Here it is.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 6 of 7
(3,644 Views)

I'm assuming you have one value of an Enum (from, say, a Control, or possibly from an "Enum constant" that you have available) and want to create an array of strings holding all possible values of the Enum.  There are two really nice features that LabVIEW has to allow you to easily accomplish this goal.

 

First, the Format into String function will accept an Enum and will return a String representing it's "value", i.e. if you create an Enum for a State Machine with values "Initialize", "Acquire", "Process", and "Exit" (corresponding to numbers 0, 1, 2, 3), then putting the Initialize Enum into Format into String returns the string "Initialize".

 

The other nice feature is that the Increment function (on the Numerics page) "wraps around" on Enums, so that incrementing "Exit" (in the previous example) gives you "Initialiize"

 

So consider the following:  Create a For Loop with a Shift Register and a tunnel beneath it.  Wire "Initialize" into both the Shift Register and the Tunnel.  Inside the For Loop, take the value on the Shift Register, get its String representation with Format into String (and bring it out through an Indexing tunnel), increment it and connect the incremented value to the output Shift Register.  Wire this incremented value, along with the input tunnel holding the initial value, into an "Is Equal?" comparator that you wire to the While's Stop control.

 

This will neatly step through every value of the enum exactly once (as it exits when the "wrap-around" occurs), giving you an array corresponding to the values of the Enum.  If you start the process with the first Enum value (using a Constant, for example), the String Array will exactly correspond to your Enum (that is, the first element will be the name of the first Enum value), but even if you put an arbitrary member of the Enum as the input, you will still get an array of all of the Enum values.

 

Bob Schor

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