LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert to enumeration

Yes, I realize it's a little ugly, but this is for software test anyway--I'm parsing and logging messages being sent to an external device via serial. Really, the type cast operator is fine, though I ran into a strange problem with it. I used it with an enum with values 0-5 (a command-ID enum), and it worked fine. But in the same vi, I did another one (a sub-ID enum) that went 0-14, and the output is always the highest enum value (corresponding to 14), regardless of input. I can do the other method from the same numerical source and enum definition, in parallel, and get the correct results. I'll see if I can figure it out when I have time, but it sure seems like it's just not working right. Meanwhile I'll use the alternate method and get some work done.
0 Kudos
Message 21 of 30
(2,216 Views)
The exact operation of a type cast might not be what you expect. You have to understand exactly how it works before you can use it correctly, so you should use the numeric conversion operations when possible. My first guess would be that you are trying to typecast a number which has a different representation (e.g. I32 to U16) and you're getting coercion to your maximum value.

___________________
Try to take over the world!
0 Kudos
Message 22 of 30
(2,201 Views)


@tst wrote:
The exact operation of a type cast might not be what you expect. You have to understand exactly how it works before you can use it correctly, so you should use the numeric conversion operations when possible. My first guess would be that you are trying to typecast a number which has a different representation (e.g. I32 to U16) and you're getting coercion to your maximum value.




Thanks for the input. I did consider something like that, but the values are small unsigned integers (0-5 and 0-14) and the type is U8, so I just moved on. I took time to look at it closer today. The first thing I noticed was that both of my conversions to enum weren't working right, not just one as I said before (one of them was always zero or the max value input, so it seemed to be working but in fact wasn't working for arbitrary values). Here's what I found:

If the value going into the Type Cast operator for conversion to enum is zero, the output is the first enum in the list, but if it's non-zero, the output is the last enum in the list. That sounds like a Lab View bug (unless you can give me a reason why that conversion is logical). If I convert the input to U16 first, everything works fine.
0 Kudos
Message 23 of 30
(2,194 Views)
Right after posting that last message, I checked the documentation. I see that U8 is shifted into the high-order byte of a conversion to U16. That's incredibly counter-intuitive. The integer conversions described imply that ints are assumed to be fixed point fractions, which is rarely the case. Also, it means you have to know the native size of the enumeration constant for the conversion to work--yuck.

(I tried to keep the board neat by editing my previous post, but it kept telling me I'd exceeded 10 minutes editing the message, even though I hadn't--no choice but a new message.)
0 Kudos
Message 24 of 30
(2,178 Views)


earlevel wrote:
I see that U8 is shifted into the high-order byte of a conversion to U16. That's incredibly counter-intuitive. The integer conversions described imply that ints are assumed to be fixed point fractions, which is rarely the case. Also, it means you have to know the native size of the enumeration constant for the conversion to work.

As I said, you have to understand how a type cast works in order to use it, since a type cast is not a conversion - it just takes whatever you have in memory and reinterpets it as the new data type. As such, it doesn't make any assumptions other than that you know what you're doing Smiley Happy . The main problem might be that the help is not clear enough about what it does and how dangerous it can be.

___________________
Try to take over the world!
0 Kudos
Message 25 of 30
(2,168 Views)
A typecast fills (or takes) the first bytes it encounters...

Ton
Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 26 of 30
(2,154 Views)

Type cast doesn't seem to work in LV 2012.

 

You can do this by bundling and then unbundling.

 

Here's an example.

Joel G
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 27 of 30
(1,561 Views)

@Joel_G wrote:

Type cast doesn't seem to work in LV 2012.

 

You can do this by bundling and then unbundling.

 

Here's an example.


Did you see the date? Smiley Tongue

-----

The best solution is the one you find it by yourself
0 Kudos
Message 28 of 30
(1,549 Views)

Yes!

 

Hence why I'm posting!

 

I found this thread while searching and found that the type casting didn't work, although this seems to document that it used to, so i thought it could do with an update. My colleague has also posted a nice, neater solution in reply to the example I referenced so it turns out there are a few ways to do this.

 

Joel G
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 29 of 30
(1,542 Views)

At this point of time I recollect my suggestion Smiley Wink

-----

The best solution is the one you find it by yourself
0 Kudos
Message 30 of 30
(1,534 Views)