07-26-2018 04:08 PM
we have a VI with lots of sequence structures in a state machine(enclosed).
is it possible to replace these with event structures to make it more efficient?
Thanks
07-26-2018 06:12 PM
Define "More efficient". Do you want the program to run faster? Take up less RAM? Be easier to debug? Take up less disk space?
Event structures are usually created to handle button presses. I see exactly one button that's for exiting the program and that's not enough to warrant it.
You have a lot of "Wait (ms)" nodes in your code that don't appear to do anything meaningful. If you just want the code to run faster, I'd set the wait period on all of them to zero and then run the VI and see if it all works fine. A few devices out there can't handle repeated quick queries but nearly all of them should be fine if you query them the instant you get the previous response. If you start getting errors or the program spins too fast then maybe add them back in one by one with lower values than they had before. The communications wait between your VI and your device should be enough to ensure it doesn't take up all of your CPU time.
Removing all of the stacked structures and sequence structures would make your code far easier to read and maintain, but probably not improve your "efficiency" much compared to removing those delays.
07-26-2018 09:35 PM
Bob Schor
07-27-2018 03:47 AM
There's no logic in replacing a sequence structure with an event structure. That's like replacing your car for a tree, because it's more 'efficient'.
Event structures are great for replacing polling of user controls, but I don't see that happening in your code.
One of the problems is the use of a state machine, while there's (almost) no state machine functionality in the code, it's mostly a sequence.
90% of those sequences serve no purpose. Forcing the update of two locals with a sequence structure makes no sense. Simply update them both, and get writ or the sequence.
As mentioned, the rest of the sequence structures will be redundant when driver code is put in SubVIs. Preferably as methods of a class, so refs and such can be private data. That might be a bridge too far for some, I think it's actually easier...
If you want to evolve what you have, so it becomes interactive, an event structure will start to make sense. I'd strongly recommend doing some courses and then refactoring before extending functionality.
07-27-2018 07:06 AM
This is not written by me.. I am supposed to improve the program..
I recently took the CLD exam, and i was told that too much sequence structure and local variables are not recommended.. but there are lots of sequence structure and local variables..
What do you guys think I can improve this program?
Thanks so much and have a nice day
07-27-2018 07:09 AM
thanks so much!
I am trying to improve this old VI for our digital processor..
07-27-2018 07:26 AM - edited 07-27-2018 07:33 AM
after the CLD you should know that by yourself.
but since you asked ..
think of code maintainability first before you go into (speed-)optimization.
so no stacked sequences!
remove code duplicity.
your VI looks very simple, this shouldn't be hard.
regards
EDIT: also apply state machine pattern better
07-27-2018 07:44 AM
Thanks...I did not do well in the CLD. I got all most all the points for style and documentation but did not get much for functionality.
you mean remove all stack sequence completely or replace it with something else?
if I remove the structure, I will leave these local variables and wait in the case structure? does it make sense?
07-27-2018 08:00 AM - edited 07-27-2018 08:03 AM
took 5 minutes on your VI to get you started
compare with your old one,
definitely decide what you want to do with all the waittimes,
i think the ones in the case structure are only interesting for the VISA write + read
use data-flow instead of sequences, which means, you should add terminals to your subvis
and you could think about reducing the locals ... for example could you write values after the case-structure has executed.
07-27-2018 10:02 AM
Well a quick glance at your code and I can say that most (not all) of the flat sequence structures in there are superfluous and could be removed without affecting the program at all.
The rest could probably be removed if you used the Error Cluster to force sequential execution like you are supposed to do.