From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Case structure with combined input values for the selector?

Hello,

 

is it possible to implement a case structure with cases that come from two different sources (for example enums)?

The application would be a state machine that has states that are triggered from outside (like start, pause/resume, stop) and other states that are triggered from inside the state machine.

 

I want to separate those inputs in terms of external and internal inputs.

I could, of course, put all cases (regardless if they are triggered internally or externally) into one enum, but that doesn't seem like a clean solution to me.

Please find attached an example.

Trennen von internen und externen Kommandos für die State Machines.PNG

0 Kudos
Message 1 of 10
(3,678 Views)

Depending on your requirements, there are two ways I handle this.

 

1. Assuming an actual state machine (you have a sequence you go through with the option of being told to change the sequence based on a message), you have a state where you read the queue and react accordingly.  So the internal state takes precidence.

 

2. Assuming this is actually a Queued Message Handler (you are told what to do by outside processes, but have the option to do something when there are no commands in the queue), then use the queue's timeout.  On timeout, do whatever state your internal registers say to do.

 

Regardless, you should only have 1 type defined enum that everybody uses for your state.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 10
(3,670 Views)

Thanks for the quick reply.

I basically use solution 2 already, but what I dislike about it (or more to the point about the single type defined enum) is that all possible commands are thrown into one pot.

 

Process 1: Start, Stop, Pause/Resume, Internal commands 1-3

Process 2: Start, Stop, Pause/Resume, Internal commands 2-6

...

 

So that gives me a very extensive enum with all possible values for all state machines that I use. I would like for the "master process" to only be able to give the 4 global commands (via queue) that are common for all my processes, i.e. start, stop, pause and resume.

 

I know, my software works as it is, but something about this point feels wrong and not well thought out...

0 Kudos
Message 3 of 10
(3,661 Views)

You could convert one enum into another via the enum text.   So an enum "Start" can be converted to the string "Start" which is then converted into a different enum "Start".  Use "Format into string" followed by "Scan from string".   Then you can have an enum that lacks the internal commands.  Make sure your full enum's default case is "Unkown command" or some such, to catch errors.

Message 4 of 10
(3,654 Views)

Hi joptimus,

 

you could use one enum for the "master queue" containing just those 4 general commands/states (maybe plus an additional "do nothing").

Your statemachine use their own enum, but those general commands are always the first items (in the same order as in your first enum). When there is a general command from your queue you can wire it to the statemachines state wire and all you get is a coercion dot…

 

Main point: you can use different enums per statemachine, but those global/general states should always be stored at the same item/index of the enums to allow easy coercion!

(I prefer an additional "void"/"do nothing" as first item to even ease handling of ReadQueue timeouts where you receive the default value in case of a timeout.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 10
(3,651 Views)

Thanks GerdW,

 

that seems like a good solution. But I still get some errors with this approach (no cases for some selector values, wrong value type). Did I overlook something?

 

 

0 Kudos
Message 6 of 10
(3,630 Views)

Hi joptimus,

 

unfortunately I cannot open your VI right now. Mind to down-convert it for LV2014?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 10
(3,625 Views)

Hi joptimus,

 

notes:

- use "Coerce To Type" (found here in the forum) to coerce one enum into the other

- don't mix enum datatypes in the second case structure! You use both types of enum in there!

- your enums don't follow my suggestion of using the very same items in the first indices: you cannot hae a case "Pause" when there is no item "Pause" in your statemachine enum (I thought I explained that part above…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 10
(3,618 Views)

Out of curiousity, why do you have a variant for the "Message" item instead of the enum?  Or if you wanted to really be generic, a string.  Then it is really simple to use the Scan From String to convert into your State Machine's enum.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 10 of 10
(3,612 Views)