LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Text Ring to Enum without coercion

Hi All. Another teaser for you...

 

I am receiving a binary-string via UDP Read. This is being unflattened into a message (.ctl defined).

One of the fields in the message represents a state. This is a U8 value that, if correctly sent by the sender, is between 1 and 3 representing "On"/"Off"/"Unknown".

If I just represent this field in the message .ctl file with an enumeration, and the number is outside this range 1 to 3, it gets coerced to the closest enumerate (either "On" or "Unknown"). This is not good as now an erroneous value has got into my system. So I want to be able to test this field before I start trusting and relying on it as an enumeration value.

So it makes sense to treat it as a text ring or numeric value, test it, then cast it to an enumeration.

However, I would like to do this in many places, so I want to subVI this test/cast operation, for many enumeration types representing different state sets.

 

So I would like a way of finding out from an enum type, what numeric values are valid, test the given ring/numeric value against it, and flag an error if it is outside of the enums defined value limits. If not outside, return an enumeration of the value.

 

How can I do this in LabVIEW?

I have almost got there using the property-node of an enum type, but it only gives me a "Number of Items" value. I suppose I can assume values start at 0, but I'd prefer to be able to explicitly iterate through each enumeration value.

 

Any clues?

 

Thanks 

 

 

 

 

 

0 Kudos
Message 1 of 9
(4,564 Views)

@skol45uk wrote:

Hi All. Another teaser for you...

 

I would like a way of finding out from an enum type, what numeric values are valid, test the given ring/numeric value against it, and flag an error if it is outside of the enums defined value limits. If not outside, return an enumeration of the value.

 

How can I do this in LabVIEW?

I have almost got there using the property-node of an enum type, but it only gives me a "Number of Items" value. I suppose I can assume values start at 0, but I'd prefer to be able to explicitly iterate through each enumeration value.

 

Any clues?

 

Thanks    


You can assume values start at 0 for enums, because they do. so "Number of Items" will always work (check the LabVIEW help files). Use the KISS principle, don't go looking for something esoteric when a simple solution is staring at you.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 2 of 9
(4,557 Views)

Create your typedef enum with a "Error, Default" value.  Throw "Unexpected Value" error if the "Default" value come in


"Should be" isn't "Is" -Jay
0 Kudos
Message 3 of 9
(4,533 Views)

Creating a typedef enum with an "Error, Default" value sounds ideal!

But how do I do that?!

 

I am using LabVIEW 2012 SP1

0 Kudos
Message 4 of 9
(4,509 Views)

Open the typdef'd enum control (.CTL file)

 

Set the enum value to the desired default value.

 

Select Edit -> Make Current Values Default

 

Save your control.

 

Any controls or constants created with this .CTL will use the saved default value.

 

Note: If you create a constant from a sub-vi connector, the default value might not be the default of the typdef; a sub-VI can be saved using the same technique as the .CTL ( Make Current Values Default). The default value of the typdef in the sub-vi overrides the default of the .CTL file.

0 Kudos
Message 5 of 9
(4,495 Views)

This does not override the coercion behaviour when I unflatten a U8 into the enum. It still takes on the extreme enum-item value if it outside the enum range.

 

I could set the upper-most and lower-most enum-item to mean an error condition, but when I have valid enumeration value of 0, I can't set the lower-most item of the enumerate to -1.

 

I am starting to think that using the "Number Of Items" property is the only way.

 

The problem is controling the coercion behaviour of an enum....

 

 

0 Kudos
Message 6 of 9
(4,486 Views)

a few ways around that.

 

1) use 0 as the default (enums are integer types the "Default" default is 0)

2)use a Strict type def and set the default value of the type def (I believe this still does the trick however existing objects will not necessarilly have the default value updated)

3) put it in a class  class data members CAN override the type default since the class is essentially a new type

4) use scan from string and set the default on the SFS primitive


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 9
(4,464 Views)

1) use 0 as the default (enums are integer types the "Default" default is 0)

2)use a Strict type def and set the default value of the type def (I believe this still does the trick however existing objects will not necessarilly have the default value updated)

3) put it in a class  class data members CAN override the type default since the class is essentially a new type

4) use scan from string and set the default on the SFS primitive

 

-------

 

Tried

1) Don't get how setting a default will produce a enum that indicates "Error" for an out of range value (both too small and too large).

2) Don't get how setting a default will produce a enum that indicates "Error" for an out of range value (both too small and too large).

3) OOP with LabVIEW is not something I've learnt yet.

4) Not sure what you mean. I still do not understand how coercion can be avoided. But I will play with this approach a bit more.

 

Sorry for being thick.

I've attached my little test program which is replicating whats going on when I receive messages from UDP Read.

When I unflatten the binary-string, coercion takes place and I cannot prevent it with Default values.

 

If you can get this working with Defaults in the enum type I will be amazed. 

If not, can you provide an example of the use of SFS you are suggesting.

 

Thanks very much for sticking with my problem.

 

0 Kudos
Message 8 of 9
(4,450 Views)

Do your test first, using the enum's property "Number of Items" for your max "True" value, then connect to the enum only when True (and send a nasty little electric "reminder" to the user to follow the rules otherwise Smiley LOL ).

 

Cameron

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 9 of 9
(4,443 Views)