Jump to content

Automatic conversion of enum to string


viSci

Recommended Posts

I was wondering why LabVIEW does not automatically convert enum's to strings on subvi inputs. The information is available on the wire so why not just offer this as a convenience instead of having

to send it in as a variant and doing the conversion manually inside the subvi. I think it would promote the idea of using typedef'd enum's as diagram tags instead of having to use strings which cannot be

're-applied' when the typedef changes.

Link to comment

I'm not understanding why you don't use the typedef enum in the SubVI as well. Why pass the value as a string? The more strong typing you have in your code the fewer bugs. Other than that it seems like a harmless idea.

It's sort of like how you can wire any kind of data into a variant. It's pretty handy, but it can also lead to bugs. I would prefer to be forced to use the "To Variant" function so that my own worst enemy (me) would have a harder time making wiring mistakes which don't break the wires.

I also don't understand why NI doesn't use any typedef enums with it's property nodes, like enabled/disabled/grayed-out. At the very least that property node ("Disabled") could create a ring when you pop up on it, instead of just an integer.

I assume you already know that the Format Into String function is an easy way to get the enum's current string value, though it would be nicer to for enums to have their own special conversion function.

post-1764-1218137639.png?width=400

Scan From String does a pretty good job in the other direction too.

Link to comment

I should have explained my application...

I would like to create a generic CVT (Current Value Table) for cRIO applications. Instead of strings, I would like to use typedef'd enums

as the tag key on my diagram. The generic interface for a CVT read or write would have a variant input for the enum key and a DBL,STR, or BOOL

as the data input. I need to convert the enum to a string and use it as the key of a variant attribute which is used to strore all the CVT data. I thought

it would be nice if the enum could be automatically converted to a string at the vi interface (I am sure NI could do it more efficiently than I can).

I am currently preprocessing the enum typedef to extract all the strings and then use the enum cast to a I32 to index

out the string. Overall, on the cRIO, it turns out that working with variant attributes is quite costly in performance so I may have to rethink the whole approach.

Link to comment

In general, LV avoids type coercions. We do allow many numeric type coercions, but there's a lot of folks that wish we didn't do those automatically. In general, the more automatic type coercions that you allow, the more accidental miswirings are possible. Allowing enum wires to attach to string terminals would be a severe break with the strict type system of LabVIEW. Given a) how rare this behavior is desired and b) how easy it is (see previous comment from jdunham) to get the conversion when you want it, there's no way that we'd put such a conversion into LV.

The coercion dots are a constant balance of ease of programming vs correctness of programming. I don't think this conversion is one that would serve that balance.

Link to comment

Ok, the problem with 'scan from string' is that it cannot be used when trying to transfer an enum into a subvi. The only way to do that is with a variant. So how about adding some goodies to the scan function to scan a variant and we could have it do all sorts of cool lvdata parsing including obtaining the enum string.

Link to comment

QUOTE (sachsm @ Aug 7 2008, 05:56 PM)

Ok, the problem with 'scan from string' is that it cannot be used when trying to transfer an enum into a subvi. The only way to do that is with a variant. So how about adding some goodies to the scan function to scan a variant and we could have it do all sorts of cool lvdata parsing including obtaining the enum string.

That's already available in the OpenG lvdata package. The VI is called Get Strings from Enum__ogtk.vi. It takes a variant as input and gives you both the list of strings, and the current value's string.

Link to comment

QUOTE (sachsm @ Aug 7 2008, 04:56 PM)

Ok, the problem with 'scan from string' is that it cannot be used when trying to transfer an enum into a subvi. The only way to do that is with a variant. So how about adding some goodies to the scan function to scan a variant and we could have it do all sorts of cool lvdata parsing including obtaining the enum string.

When I have needed this, I just convert to string before passing to subVI. You can make a smaller-sized subVI wrapper around the Scan From String if you need to conserve block diagram real estate.

I made a request for exactly this a while back, but I have come to agree with Aristos. I just would prefer a smaller sized icon, similar to those for converting between numeric types.

Link to comment
  • 3 months later...

QUOTE (sachsm @ Dec 12 2008, 06:20 PM)

Here's a version based on the info in LabVIEW on the typedescriptors for LabVIEW 8.x, if it is way faster let me know (I have a feeling it should be faster).

post-2399-1229196504.png?width=400

EDIT: Done some benchmarking and looking into the OpenG code, the code functions the same.

The benchmarking shows that in multiple runs (>5000) OpenG is faster, my code is faster approximatly at 1000 samples. On smaller sample size the difference aren't statistically reliable, maybe you can tweak it for your embedded system.

EDITEDIT:

First tweak, removing the 'Split Array' and using a start offset of 6 makes my version approx. 30% faster than OpenG (with less functionality)

Second tweak, setting to I32 after the index array tops off another 10 %.

Tweakead:

post-2399-1229197747.png?width=400

QUOTE (Clio75 @ Dec 13 2008, 07:37 PM)

I do the property node, Like the picture attached.

But that requires scripting and you will need a control.

Ton

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.