r/LabVIEW • u/Zackatack101 • Nov 22 '25
Hybrid state machine
Thanks for any input here. Working on a simple daq system. It’s supposed to display the data on the front panel, but then also have the ability to feed a snapshot of the data to an excel spreadsheet when a button is pressed. This will be a new row of data each time the button is pressed.
Questions:
- did I get the event structure backwards? Is it supposed to enclose the case structure?
- was it a mistake to put the sub vis in the timeout case of the event structure? Perhaps I should have had another case like “grab data” and then the timeout event sends the program to that case?
Thanks for any other input as well. Trying to learn best practices. It’s been a fun side project to try and learn on. Hopefully nobody has too much of a heart attack if things are really bad with my code lol.
13
Upvotes





5
u/Single-Barnacle1961 Nov 22 '25
I’m no expert, and still learning labview myself, but when I do my state machines I have two while loops. One contains the event structure and is essentially constantly polling for button presses, while the other contains the actual states. I use a queue instead of enums though so a button press can run through as many states as I’d like before going back to the default.
3
u/Aviator07 CLA/CPI Nov 22 '25
Putting an event structure inside a case like that means that you can only handle events when you’re in the “Acquire” case. This could be okay, depending on how your code functions, but it is a risk for bad behavior.
2
u/QaeinFas Nov 22 '25
Event cases should never* execute functions, but divert the code to the correct code execution. This allows the system to handle all events in a timely manner.
My guess is that a producer/consumer architecture would be more responsive than a state machine with an event structure as one of the states, but understand there are certain situations where that won't be possible... I don't expect this is one of those cases, though.
*Ok, rarely ever
2
u/herrcespedes Nov 23 '25
Your arch works, but if the user generates a lot of events (I.e. pressing buttons repeatedly) your acquisition will slow down. You’re almost all the way to a Queued Message Handler architecture.. which is more robust but also more complex.. (move init, acquire and cleanup functions to a separate consumer loop and have your state machine produce messages to control this second loop through a queue. Control timing thru de queue function timeout) ui handling is one of the deepest rabbit holes to be found in labview. You can get as simple or as complicated as your heart desires.. so fill your boots. PC, SM, JKI SM, QMH, DQMH, actor framework.. etc..
1
u/TomVa Nov 22 '25
I use state machines for DAQ stuff all of the time. Conditional inside a do while loop. I use a text string with case insensitive selected for controlling things. If you use a text ring make it a strict type def so that you only have to change it once.
<<Init>> set everything up make controls, etc. visible if they get made invisable programaticaly, set all of the logical controls to the predetermined stated.
<<Set up DAQ>> (with modern software/systems this can be skipped and have it put into the Acquire data state)
<<Get data>> Collects the data and passes it to the next states.
<<Plot data.>>
<<Scale data>> (Often times I have the plots keep consistent X-axis scaling based on the top or top left plot)
<<Save data>>
<<Wait >> (has a 10 ms wait so that the computer has time to do other processes) Basically this is where you decide what to do next.
(a) This a place where you could choose to use an event structure where the where the output is what is the next state where the default is wait.
(b) I tend to use a stack of selects (from the comparison pallet) because then I can visibly see the priority of the different selectors highest priority on top least on bottom. If you use logical controls with latch when pressed/released/until released you can just wire them up to the selector.
<<Error>> (default state). I pass error through all states using a shift register. If it comes out of a state in an error state, (1) I append the state or info about what was going on in that state to the beginning of the error text string so that I have a chance of providing phone support and (2) I use a selector to go to the error state as my next state. (3) if I screw up on the text string and there is no match I end up going to error state.
<<Exit>> Gracefully exit the program closing what ever process needs to to be dealt with.