LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

String to Enum in RT

I would like to write a generic VI to convert a String to an Enum.

For example, Enum BaudRate:


0 = "1200"
1 = "2400"
2 = "4800"
.....

 

A String value of the enum (e.g. "4800") is read from a configuration file and needs to be converted to an Enum. There are however several Enum types in my application, so I need a polymorphic VI that can convert a String to any defined Enum type.

 

I already know that there are some special considerations when accessing the Strings[] of an Enum in RT, please see link: http://zone.ni.com/devzone/cda/epd/p/id/2147. The linked article contains some example code, that converts an Enum to a String.

 

String to Enum is commonly used in other languages (e.g. .NET) but is possibly missing from LabVIEW.
Is it possible in LabVIEW (RT)? I am not entirely familiar with type descriptors and I am also not sure whether a VI can be polymorphic to any Enum type?

 

Thanks
Mark

Message Edited by sparkymark567 on 08-05-2009 08:25 AM
0 Kudos
Message 1 of 4
(3,261 Views)

sparkymark567 wrote:

I would like to write a generic VI to convert a String to an Enum.

For example, Enum BaudRate:


0 = "1200"
1 = "2400"
2 = "4800"
.....

 

A String value of the enum (e.g. "4800") is read from a configuration file and needs to be converted to an Enum. There are however several Enum types in my application, so I need a polymorphic VI that can convert a String to any defined Enum type.

 

I already know that there are some special considerations when accessing the Strings[] of an Enum in RT, please see link: http://zone.ni.com/devzone/cda/epd/p/id/2147. The linked article contains some example code, that converts an Enum to a String.

 

... is possibly missing from LabVIEW.
... not sure whether a VI can be polymorphic to any Enum type?

 

Thanks
Mark

Message Edited by sparkymark567 on 08-05-2009 08:25 AM

I belive the answer is rather complicated. LV is strongly typed (correct anything that I asy wrong in the following!) in that it wants to know about hte type of data on the wire. If you turn on the help window and use the wire tool to float over an enum type wire you will see tha the the enum definition is IN THE WIRE.

 

Prior to LV 8.2 and LVOOP there was no way around not telling LV ahead of time what was going to be in the wire (makes sense since LV handles the memory detials for us). LVOOP introduced dynamic dispatching which is like polymorhic VI but rather than the data type being selected at compile time when editing, the proper data type is selected at run time using dynamic dispatching.

 

Prior to yesterday LVOOP was not supported on RT. If I am on top of what they are doing down at NI Week LVOOP in RT was being announced.

 

So yes it can be done but it requires LV 2009 LVOOP.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 2 of 4
(3,250 Views)

By far the easiest way to do this is using Scan Value or Scan from String in the string palette.  Pass in your enumeration as the data type (default value) and set the format string to %s.

 

If for some reason that won't work for you, you can also use the GetNumericInfo VI found in vi.lib\utility\VariantDataType\VariantType.lvlib which will return the strings associated with any enumeration you pass to it.  You can then search through that array of strings to get the enumeration value.

Message 3 of 4
(3,249 Views)

This is exactly what I need, thanks.

Also, the oposite VI: "format into string" is useful to convert an enum value to a String 

 

It seems that the original linked article is rather pointless? or have I missed something?

 

 

0 Kudos
Message 4 of 4
(3,228 Views)