LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to call an event from a subvi into main VI

Solved!
Go to solution

Hi every one

I have a problem in calling an event from a subvi in to a main
VI, Actually I searched many of posts in this subject but unfortunately I
couldn`t find what is my VI problem.

I create a simple VI which adds two numbers when I push a bottom
and exit the program when I push another bottom. After that I want to call this
VI as a sub VI in a Main VI but it doesn`t work.

I read these two documents from here and here.
They were very useful but yet I couldn`t find a good solution.

I really appreciate if anyone could introduce me how I can
handle this problem

Thanks

Morteza

 

Download All
0 Kudos
Message 1 of 8
(4,753 Views)

You need to use user events. I have attached a basic example. A subVI can generate a user event. You cannot simply register a value change event for the control on one front panel and fire the event from another VI. User events however give you this capability.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 8
(4,745 Views)

Mark,

Thanks for your answer

Actully I could handle  my VI with event registering but I confronted with some thing odd, when I push the "stop button" the output became 0 I cant underestand  why?

could you please let me know you idea?

Download All
0 Kudos
Message 3 of 8
(4,725 Views)

You should have the indicator on the top level of the subVI and not in the event structure.  If the value is not written to the indicator terminal during execution, then the subVI returns its default data, (zero) when the subVI ends.

0 Kudos
Message 4 of 8
(4,720 Views)

Ravens,
I tried to put controls and indicator out of event structure but whenever I push "stop button"  the value again becamed zero. and I don`t know why program doesn`t exit the subvi and doesn`t allow me to stop main VI, as Mark said maybe I couldn`t handle this program easily. as you know this is a simple program and I want to use several complex sub vi in my program so If I could`t underestand this vi properly I couldn`t work with other VI`s

 

 

Download All
0 Kudos
Message 5 of 8
(4,704 Views)

The reason why it becomes zero when you press stop is that you selected the control to get the default value in the event structure if it's not connected. You can fix it by connecting it like I did in the first picture.

 

To make it stop when you press the stop button you have to connect one stop button to the loop exit terminal. The reason for the sequence structure that I added is to make sure that the loop exits the first time you press the stop button, try removing it and you'll see what I mean.

 

You have one additional problem. You rely on the x+1 output of the sub vi as an input for the next iteration. However, that is not reliable since if you can the subvi from somewhere else it might mess up the count. You're better of using a shift register and passing in the last value to the subvi in each iteration.

Download All
Message 6 of 8
(4,700 Views)

thanks ,your hints were great.  it worked very well,  I couldn`t underestand why when we connect the output of subVI to the sequence it becomes ready to execute. 

would you please explain me more about this and also shift register, I really appreciate it

0 Kudos
Message 7 of 8
(4,689 Views)
Solution
Accepted by topic author Morti

What the sequence achieves is to make sure that whatever is in the sequence won't execute until the subvi reruns. The reason, is that an output of the subvi is connected to the sequence so the sequence cannot execute until that output is ready and the output is only ready when the subvi returns. This ensures that the stop control is read by the loop AFTER the sub vi returns.

 

If the sequence wasn't there, the loop might read the stop control which would still be false, pass it to the loop exit terminal and only then call the subvi and wait for a button press. Now if you press the stop button the sub vi will return and do another iteration. However, if the stop control is only read after the subvi returns, then if you set the stop control to true during the subvi wait, when the subvi returns it'll read the control and the loop won't do another iteration.

 

Which version of LabVIEW are you running? I need to know so that I can upload the code with shift register. I recommend you read this to understand the SR: http://www.ni.com/gettingstarted/labviewbasics/shiftregisters.htm

 

Also, I suggest you take Mark's advice and look into user events because that'll probably work better in the long run than what you're doing because it reduces code coupling between the VIs.

 

Matt

Message 8 of 8
(4,685 Views)