From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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.
0 Kudos
Message 1 of 10
(4,259 Views)
Under Synchronization there's a function called "First Call?"
Message 2 of 10
(4,255 Views)


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!
Message 3 of 10
(4,240 Views)
thanks
0 Kudos
Message 4 of 10
(4,195 Views)

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
Certified Trainer, Former Alliance Member, LabVIEW Champion
Message 5 of 10
(4,155 Views)
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
Message 6 of 10
(4,145 Views)

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
Certified Trainer, Former Alliance Member, LabVIEW Champion
Download All
Message 7 of 10
(4,133 Views)
I didn't realise you could change the direction of the feedback arrow until I saw your example, thanks!
0 Kudos
Message 8 of 10
(4,127 Views)
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?
0 Kudos
Message 9 of 10
(4,070 Views)
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
Certified Trainer, Former Alliance Member, LabVIEW Champion
0 Kudos
Message 10 of 10
(4,043 Views)