LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Write to text from functional global refnum

Solved!
Go to solution

I am attempting to make a status logging VI that retains a file refnum as a Functional Global (FG). The reason I want to make this an FG is for convenience of using it anywhere in my multi-thread program and not needing to wire the refnum through. For proof of concept, I am simply running my LogStatus2.VI by itself, first with an Open operation, and then again with a Write. The problem I am having is that I receive an error (Code 1) any time after the first execution. It feels as though the refnum is not storing as an FG in the way that I am thinking it should. Am I missing something fundamental or can a file reference not be used as a FGV?

 

Note: I intend to add some verification later for empty path, etc.

Download All
0 Kudos
Message 1 of 3
(1,668 Views)

If you are running this VI by itself, you will not get the results you want.  When the VI stops running (and nothing else is running) it is likely the references are being closed.  You need to create another VI that calls your Action Engine several times to test what you are trying to test.


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 2 of 3
(1,651 Views)
Solution
Accepted by topic author JFleck

Edit: Dangit, crossrulz beat me to it!

 

If you run the subVI from within another VI, it works fine. The issue is that LabVIEW will automatically close opened file references when the VI is closed. Your refnum is being stored correctly, but the file is being closed.

 

There are two ways to fix this. First is to use it as-is; when you call the VI from within another VI, it works just fine:

BertMcMahan_0-1598551856673.png

BertMcMahan_1-1598551926415.png

 

(I am guessing you're calling it multiple times manually, which doesn't work because the reference is closed automagically)

 

The second way, and the way I'd recommend, is to not store the refnum but to store the path. Each call would then open/write/close the path. This seems to be a status logger, so it's not going to be called hundreds of times a second, so the overhead with reopening the file is minimal. The reason to do this is so that the file gets reliably closed. Note in my example that I did *not* close the file, but that it was fine; that's because LabVIEW saw that I wasn't using the reference anymore and auto-closed it. In the dev environment this works, but in a normal application what happens if the user force-closes the application? Or what if it crashes? Or what if you have some edge case where the file doesn't quite get closed properly?

 

In general it's a better practice to not leave references open if you can help it. It's certainly no hard and fast rule, but in the case of a logger IMHO it would be better to save the *path*, not the *refnum*, to the FGV.

 

 

Message 3 of 3
(1,641 Views)