LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How avoid using while loops into a event structure

Solved!
Go to solution

I'm quite new in LabVIEW, so I hope a constructive criticism. The VI will control and instrument using the serial port, and I receive and an echo as feedback. I have different inputs in my front panel (Amplitude, Operation mode, Frequency, Reset, Stop, and Start), and I'm using an event structure to handle any change in my controls. I use single or nested while and for loops inside the event structure. And, I've unticked the option to "lock panel until the case for this event completes" and used local variables, in order to use the same stop button in all the while loops.

However, I've read several posts and they suggest to not doing what I'm doing, so I would like to know if it is possible improving my code. Could you help me?

Download All
0 Kudos
Message 1 of 16
(4,140 Views)

Yo can do it by taking the state machine with events architecture and as the event is triggered, call another state and perform all your while loop actions or anything in that state and when it is done, again call back it to the state in which you have placed the event structure.

 

 Certified-LabVIEW-Developer_rgb.jpg

Best Regards,
Prince Agarwal,
Certified LabVIEW Developer
0 Kudos
Message 2 of 16
(4,123 Views)

Dranek wrote:

However, I've read several posts and they suggest to not doing what I'm doing, so I would like to know if it is possible improving my code. Could you help me?


Yes, you code is "inside-out". You've seen my post here, but there are quite a few others.

 

However, it is difficult to offer clear suggestions if all we see is a truncated code picture. We cannot tell what is in the other cases of case and event structures. We cannot see how the event is configured, etc. etc.

 

You also seems to over-use value property nodes and local variables where they are not needed. Does the serial port need to be configured from scratch every time the event executes?

 

(The front panel looks nice, but is not really needed for troubleshooting. As we always say: Don't judge a VI from its front panel :D)

 

Do you unlock the front panel during the event?The problem with slider type controls and slow events is the fact that many events of intermediary values accumulate in the event queue, so you make sure to limit that too. ( see also this idea, now implemented :D)

 

If you can, attach the actual VI. Thanks.

0 Kudos
Message 3 of 16
(4,080 Views)

Ok, thanks for your help. A small version of the program is attached. It is true that I'm using lots of property-value and local variables. Now, I'm aware that I have to change the VISA configuring port for VISA open port. I'm still learning how to program in LabVIEW, so I really appreciate your help.

PD. I disabled "VISA read" because I'm not always in the lab.

0 Kudos
Message 4 of 16
(4,057 Views)

Hi, Thanks. I've attached the VI. After watching online tutorials, using the state machine will replace the event structure right?. Is it really more effective?

0 Kudos
Message 5 of 16
(4,053 Views)

No, event structure is to process user actions (button pressed, etc)

State machine is to organize and control order of execution (initilize port, set frequency, wait for next command, close)

0 Kudos
Message 6 of 16
(4,037 Views)

@Dranek wrote:

using the state machine will replace the event structure right?


Not quite.  More like you will have a state in your state machine for checking for events.  You then go to whichever states you need to in order to do what you need.


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 7 of 16
(4,036 Views)

Your system is in one of many states at any given time, and depending on the state, various actions occur and update the various indicators (no need for local variable if you place them after the case structure containing the various states. States change based on the results of the current state and also based on front panel actions by the user.

 

You way overuse the stop primitive. All you need is to cause the state machine loop to end and the VI will stop automatically once there is no code left (I assume you don't use the "continous run" debugging tool). If you pull the emergency stop in the middle of the code (as you do in many places!), the VI just runs into a wall and there is no time left for proper cleanup (e.g. resetting outputs, closing connections, putting connected instruments into the correct state, etc.) 

 

Your serial port should be initialized exactly once before the loop starts.

 

You are dealing with raw bytes, so converting to I8 (everywhere!) is incorrect. Use U8. There are probably simpler solution to create the string using typecast or flatten operations.

 

Yes, please look at the state machine templates that ship with LabVIEW and also look at the shipping examples.

Message 8 of 16
(4,005 Views)

I cannot find examples using event structure and state machine. Could you help me with a simple example of how using them together?

0 Kudos
Message 9 of 16
(3,970 Views)

Here is a template of state machine with event architecture. You'll find it useful.

 

Kudos are welcome.

SME.png

 

 

Certified-LabVIEW-Developer_rgb.jpg

Best Regards,
Prince Agarwal,
Certified LabVIEW Developer
Message 10 of 16
(3,967 Views)