VI High 7: How to Program Events with the Event Structure in LabVIEW
This time around, we examine one of the most commonly asked LabVIEW programming questions: how do I program with events? We’ll explore why this is different than the traditional “polling scheme” and basics of the event structure. We’ll talk about this more in a future episode.
For more on learning LabVIEW, check out the Sixclear Lucid LabVIEW Fundamentals Training (formerly Sixclear LabVIEW Fundamentals) course offered by Sixclear: sixclear.com.
In the video, we mention that we must place the terminal of latch action Booleans in the event structure case corresponding to that event. Why is that? This is taken from the LabVIEW Help topic ” Using Events with Latched Boolean Controls”:
“When you trigger an event on a Boolean control configured with a latching mechanical action, the Boolean control does not reset to its default value until the block diagram reads the terminal on the Boolean control. You must read the terminal inside the event case for the mechanical action to work correctly. As a reminder, a note appears in the Edit Events dialog box when you configure a Value Change event on a latched Boolean control.”
Experience Level: Basic
(start transcription)
“I like an eventful life, so when Friday night rolls around, I program my own. You may have other weekend plans, so I’ll teach you how to do it in in five minutes. First, what are events? An asynchronous notification that something has occurred, the most common thing to have occurred is some action on our front panel. Let’s say when I click on this OK button, I want a dialog box to pop up saying: “You clicked the button”. First let’s do this without events. We’ll look at my block diagram. You see I’m wiring the OK button to the case selector terminal of this case structure enclosed by a while loop. And in the true case of the case structure, I have the one-button dialog function. It’s also a good idea to put some timing in here to dictate the frequency of loop execution: every 10 ms should be fine.
Going back to my front panel, let’s run this VI. When I click on the OK button, the dialog box pops up. Looks good. We call this type of design a polling scheme, because LabVIEW is continuously checking or polling this OK button to see if it’s been pressed. How often? 100 times per second. Let’s highlight execution this. Indeed, we see that over and over again, the OK button is polled and reads false, meaning that we go to the false case where there is nothing. Once the OK button has been pressed, we go to the true case and execute the dialog box function.
This works, but is there a better way? Yes. Events. Let’s replace this case structure with an event structure. I’ll remove the One Button Dialog function, delete the case structure, and put down an event structure. Get rid of this timing. Now I need to tell the event structure what to look for. Right-click on the border and go to Edit Events Handled by This Case. Up comes my Edit Events Dialog Box. We’ll start here in the middle with Event Sources. These are all the places in LabVIEW that could generate an event. We see events coming from The Application, or LabVIEW, this VI, a couple other options, and what we’re finally interested in, controls on the front panel. So I’ll click on my OK Button and now in the Events window I see all the events that could be generated from this OK button. I see events that can occur from the keyboard, moving my mouse or clicking my mouse on the OK button, dragging over it, activating the shortcut menu, or a value change. Meaning that whenever the OK button changes value, an event is generated, that’s what I want. I now see the OK Button: Value Change as a configured event. I’ll click OK.
Now, when that Value Change event occurs, whatever is inside of this event structure, and particularly this specific case of this event structure, will execute. Naturally I’ll put this dialog function in here. Finally, I’ll put my latch action Boolean terminal inside of here as well. It’s not necessary to do this for all data types, but it is for latch action Booleans. See the text description of this video for the reason. Now let’s go to the front panel and run this VI again and note that the behavior is the same. Let’s click on highlight execution and watch this run…..this is pretty boring. But it’s a good boredom because we’re not constantly having LabVIEW poll our OK button. Instead, our code sleeps and waits for an event to occur, and immediately wakes up when it does. Let’s watch. That’s just better.
Now I’ve kept this extremely simple, and many of you familiar with event structures will note that I should also create a separate event for this Stop button. That’s true, so we’ll do that now. I’ll go to my event structure, right-click, Add Event Case, and this time, choose the Stop button and a Vaue Change. Ok, and now place the Stop button terminal inside the event case. And wire it out to my while loop conditional terminal. I’ll take it off highlight execution and run this again, and as before, flawless execution. And I’ll next show you how to program multiple events with this same event structure. I’ll be back next week to do that. Hopefully. I have a pretty busy weekend ahead…”
(end transcription)