NI Home > Community > NI Discussion Forums

LabVIEW

Showing results for 
Search instead for 
Do you mean 
Reply
Member
Rodnebb
Posts: 84
0 Kudos

How to detect first time a vi is run?

Hi,
I have a vi that need to open a com port, unless it's already open in which case it should just read the com port.
Consequently I would need either a way of detecting if this is the first time a vi runs or a way of checking (very quickly) if a com port is open. Since the first functionality is very universally useful I'd very much like a suggestion here in any case. Wiebe mentioned such functionality in one of his post, a vi that returns this information.
 
I have tried to use a while loop that runs once to detect if its the first run, for some reason I decided this worked yesterday but today it doesn't. (Any explanation on that anyone?)
 
I am programming a cFP-2120.
Active Participant
JeffOverton
Posts: 536

Re: How to detect first time a vi is run?

Under Synchronization there's a function called "First Call?"
=============
XP SP2, LV 8.2
CLAD
Knight of NI
Knight of NI
tst
Posts: 10,924

Re: How to detect first time a vi is run?



Rodnebb wrote:

I decided this worked yesterday but today it doesn't. (Any explanation on that anyone?)

The USR keeps its value as long as the VI is in memory, even if the entire hierarchy stops running, so if you ran your code once and then ran it again without unloading the VI, it will return T even on the first call.

___________________
Try to take over the world!
Member
Rodnebb
Posts: 84
0 Kudos

Re: How to detect first time a vi is run?

thanks
Member
BuddyHaun
Posts: 49

Re: How to detect first time a vi is run?

Here is the path to a vi that I use to check to see if any ports are open you can then use the refnum you need from the array if the port is open. It is also useful if you need to make sure all ports are closed, just run the array into a for loop with a VISA Close.

 

vi.lib\Utility\visa.llb\Open Sessions.vi

 

Buddy Haun

CLA, Certified Trainer

Buddy Haun
CLA, Certified Trainer, Alliance Member, LabVIEW Champion
Active Participant
Jarrod_S.
Posts: 1,420

Re: How to detect first time a vi is run?

[ Edited ]
The First Call function is definitely what you want. But FYI, if you wanted to get the same functionality and program it yourself (for whatever reason), you can actually do so if you have LV8.5 with the newly updated Feedback Nodes that can exist outside loops as follows:




Message Edited by Jarrod S. on 02-03-2008 12:12 AM
Jarrod S.
National Instruments
Member
BuddyHaun
Posts: 49

Re: How to detect first time a vi is run?

The only problem I have with the "detect first time" method is that it is very easy to leave a COM port in an unknown state especially if you have improperly terminated a program and have not exited the LabVIEW system you get a "hung" port. LabVIEW has the port reserved as open but the program no longer has the session. I am attaching a program that should provide the functionality Rodnebb is looking for. I am also attaching another quick vi that I have found very useful and that one is for resetting all open COM ports.

 

 

Buddy Haun
CLA, Certified Trainer, Alliance Member, LabVIEW Champion
Active Participant
Neil.Pate
Posts: 768
0 Kudos

Re: How to detect first time a vi is run?

I didn't realise you could change the direction of the feedback arrow until I saw your example, thanks!
nrp
CLA
Member
Rodnebb
Posts: 84
0 Kudos

Re: How to detect first time a vi is run?

Thanks Haun,
but... wouldn't a better name for resetcommports be CloseComPorts.vi, while OpenComPorts should be ResetComPorts?
Util_OpenComPorts will work on already open ports?
 
Also, the functionality of Open Sessions.vi is to find all open ports, or all existing ports?
Member
BuddyHaun
Posts: 49
0 Kudos

Re: How to detect first time a vi is run?

Use whichever names make it easiest for you to remember the functionality. I just grabbed these from some old projects where I used different naming conventions. The Util_OpenCommPorts compares the VISA Resource In to ports already open and opens the port if it is not already open. The VI just passes out the VISA reference if the port is already open. The functionality of the  Open Sessions.vi is to find all Open Sessions, not all Configured Ports, you can test this easily by running the vi itself with ports open or closed, if you have no open ports you get an empty array no matter how many ports are configured on your system.
Buddy Haun
CLA, Certified Trainer, Alliance Member, LabVIEW Champion