05-30-2024 04:48 AM
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.
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
Solved! Go to Solution.
06-02-2024 07:48 AM
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.
06-03-2024 09:10 AM
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
06-03-2024 06:16 PM
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.
06-04-2024 03:19 AM
Hi Bienieck,
That worked great - problem solved. The PropOptions can be a bit confusing.
Thanks again for you help.
John