NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieving Enum from TestStand in C#

Solved!
Go to solution

Hi All,

 

We have defined an Enum in TestStand, named "StatusCodes",  which holds a status for an item of test hardware.  The Enum has the "Flags Enumeration" and "Strict Enumeration" settings made.

JAPHE_1-1717061860720.png

 

 

We have a variable in StationGlobals which is of this type.  There are numerous other variables in StationGlobals pertaining to this piece of test hardware, but StatusCodes is the only Enum.  We have no problem retrieving all the other variables in C# but cannot access the Enum variable StatusCodes.  To retrieve a numerical array via a SequenceContext, for example, we may do the following ...

 

double[] myArray;
myArray = tsSequenceContext.StationGlobals.GetPropertyObject("", 0).GetValVariant("PCDCI_ESPDeviceResponse", 0);

 

... which works fine.  Or to access a Boolean property of a container, by using ...

 

bool myBool;

myBool = tsSequenceContext.StationGlobals.GetPropertyObject("TestHardware", 0).GetPropertyObject("OutputState", 0).GetValBoolean("Set", 0);

 

which also works fine.  So the question is how can we retrieve the value held in the variable which is a StatusCodes Enum?  Despite our endeavours using various  "GetVal" methods we are thwarted by a runtime error stating the given method "Expected Number, found StatusCodes".

 

We'd appreciate some guidance on this.  Thanks.

 

John

 

 

 

 

0 Kudos
Message 1 of 5
(463 Views)

In the attachment, you will find examples of using getters and setters with enums in TestStand. I used the Statement step with TestStand expressions but you can do the same in C#.

 

Example getters:

Locals.GetValXResults.Code = ThisContext.AsPropertyObject.GetValNumber("Locals.StatusCode", PropOption_CoerceFromEnum),
Locals.GetValXResults.Name = ThisContext.AsPropertyObject.GetValString("Locals.StatusCode", PropOption_CoerceFromEnum)

 

Example setters:

ThisContext.AsPropertyObject.SetValNumber("Locals.StatusCode", PropOption_Coerce, Locals.SetValXParameters.Code),
ThisContext.AsPropertyObject.SetValString("Locals.StatusCode", PropOption_Coerce, Locals.SetValXParameters.Name)

 

My ThisContext would be your tsSequenceContext. I used Locals instead of StationGlobals but it doesn't matter. I also utilized the lookup strings instead of calling nested methods as you showed.

Michał Bieńkowski
CLA, CTA, CPI

  1. Did someone devote their time to help solve your problem? Appreciate it and give kudos.
  2. Problem solved? Accept as a solution so that others can find it faster in the future.
  3. Contribute to the development of TestStand by voting on the TestStand Idea Exchange.
Message 2 of 5
(415 Views)

Hi Michal,

That worked with the exception of one issue.  When the value of the Enum type has more than one bit set the string representation is an empty string.  Having read the description of the PropOption_CoerceFromEnum it seems to be the correct option.

 

Many thanks for you input and you time.  If you have any idea of how to resolve this last teaser, it would be much appreciated.

 

John

0 Kudos
Message 3 of 5
(399 Views)
Solution
Accepted by JAPHE

Working with flag enumerators is not that different. It's just bitwise semantics. If you do, for example, Locals.StatusCode = (Enums.CUSTOM_StatusCodes.CMD_PARITY_ERROR | Enums.CUSTOM_StatusCodes.CMD_FRAMING_ERROR) then you will see {CMD_PARITY_ERROR | CMD_FRAMING_ERROR} in the variables. Try PropOption_Coerce instead of PropOption_CoerceFromEnum.

 

See another example in the attachment. 

 

Please use the ACCEPT AS SOLUTION button if I have covered everything.

Michał Bieńkowski
CLA, CTA, CPI

  1. Did someone devote their time to help solve your problem? Appreciate it and give kudos.
  2. Problem solved? Accept as a solution so that others can find it faster in the future.
  3. Contribute to the development of TestStand by voting on the TestStand Idea Exchange.
Message 4 of 5
(388 Views)

Hi Bienieck,

 

That worked great - problem solved.  The PropOptions can be a bit confusing.

 

Thanks again for you help.

 

John

0 Kudos
Message 5 of 5
(371 Views)