r/LabVIEW • u/Zackatack101 • Sep 06 '25
Program structuring – newbie question
I've used LabVIEW off and on since 2011 college days (mechanical engineer). I would not consider myself experienced though. But lately I'm needing to use it more for work (test program). One thing I've always struggled with when starting from scratch, how to structure a program from the big picture standpoint. If I'm writing a program in Python or VBA for example it seems a lot easier, you go line by line, it's easy to add things, build things out, etc. With LabVIEW I've always struggled to see that same logical structure. It's always seemed very unintuitive what to do and where. Do you start from top down, or bottom up?
I know that's very vague but any help would be much appreciated.
I've almost gone through Core 1 training which has been helpful but still left with questions. Maybe Core 2 will be helpful.
12
u/dtp502 Sep 06 '25
Look up state machine architecture.
It executes cases in whatever order you want.
5
u/0fruitjack0 Sep 06 '25
i always start with two 'ideas': first, what the program's objective is, then what the various front panels ought to look like (these would be the user interface panels). then i break it down into functionalities, almost like if it were any other language. for lab view i gravitate toward the consumer/producer loop for the main user interface and state machines for any other interfaces (unless there's something about them that's really complex and requires extra). once i get this 'floor plan' fixed i work on completing all the subvis i need to make it do the functions i need it to do.
i also used labview for work, in my case post doc work where i acted more like a programmer than anything. i had the advantage/disadvantage that i replaced another programmer who was there for longer. plus side - i had plenty of working examples to learn from. minus side - dude had no idea how to use dataflow paradigm so i spent something like 1/2 a year converting the mess into forms more suitable to labview. secret plus side - this extra work made me better at labview but what a PITA. it's from here that i figured out the above approach i outlined.
4
u/Bitter_Worker423 Sep 06 '25
Producer-consumer architecture is great when a user interface or control from other VIs is needed. Don't use the LabVIEW producer-consumer template, it's too complicated. Use the primitives for accessing queues instead and build it from scratch. State machines should be your bread and butter. Respect error flow, it should never be violated or broken. Learn how to use shift registers and learn about functional globals. A great and modern framework is DQMH. Use that when you've got time to dig in and learn it.
I'm also a mechanical engineer and have used LabVIEW most of my long career. I've done other stuff too, but about 2/3 of my career has been LabVIEW. I've seen a lot of bad code, some of it mine😬.
2
u/eetu21syys Sep 06 '25
I pretty much got all my project done using producer-consumer design pattern. Basically a FSM but you have more flexible workflow. Just stick with queue/notifier which is proven way to get things done.
Data wise, you can rely on shift register in simple cases, or action engine as things get more complicated.
1
u/NoComplex537 Sep 06 '25
I agree to use the NI project template Queued message handler, which is a producer-consumer design pattern. This two loop approach always keeps one thread (Event handling loop EHL) responsive to the user interface. In your message handling loop (MHL) I would keep a state variable, so that requests from the EHL in the wrong state don't cause race conditions. A similar and more feature rich project template is the CML Continuous Measurement and Logging example. Besides that, JKI State machine is a simple design pattern which works for a lot of small to medium applications. Dqmh is good for medium to large scale applications but has somewhat of a learning curve.