02-22-2013 04:34 PM
I would like to output some data to a file with a custom header and data format. I have created a simple true/false case structure within a while loop where the data is aquired. If the while loop count is 0, I would like to output a custom header to a file using the "Write to a Text File Function". For this fuction I planned on leaving the file name blank so it will prompt the user with a file dialog. If not on the first iteration of the while loop I would like to send my data to the same file with the header using the "Write to Text File Function" again. I would like to use the output refnum from the "Write to Text File Function" and use it as input for the second function that writes the data. Any ideas? I am unsure how to share the refnum between the case structures so there is only one file dialog shown to the user. I have attached a sample vi.
02-22-2013 04:48 PM
You should store the file refnum in a shift register (right-click the border of the while loop, add shift register).
02-22-2013 06:19 PM
Just for a good reference, you should do some research on the State Machine architecture. I can't find my normal page now (on a different computer than normal right now), but here's a good example I found really quick.
https://decibel.ni.com/content/groups/northeast-oh-labview-users/blog/2009/09/08/state-machines
10-29-2014 10:07 AM
Hello!
A bit old thread but I'd like just a clarification for file refnums as well:
I have a state machine structure (basicly a case structure with different file operations in each case).
I open file in one case, and pass in into feedback loop outside the case structure and input it back to the case structure.
Is it so that because I exit the case structure, the file refnum becomes invalid? Or should it stay persistent until I explicitly use close file VI?
I am experiencing an error code = 1 when trying to close a file that I opened in other case.
If so, what are my options to preserve a valid file I/O refnum? shift register? global variable? local variable? some other?
I feel that my files are getting closed without any warning!
10-29-2014 03:21 PM
How are you runing your vi? With Run button?
Doesn't your vi have a while loop?
10-29-2014 03:55 PM
@Turzt wrote:
Hello!
A bit old thread but I'd like just a clarification for file refnums as well:
I have a state machine structure (basicly a case structure with different file operations in each case).
I open file in one case, and pass in into feedback loop outside the case structure and input it back to the case structure.
Is it so that because I exit the case structure, the file refnum becomes invalid? Or should it stay persistent until I explicitly use close file VI?
I am experiencing an error code = 1 when trying to close a file that I opened in other case.
If so, what are my options to preserve a valid file I/O refnum? shift register? global variable? local variable? some other?
I feel that my files are getting closed without any warning!
No VI hard to answer.
10-29-2014 05:35 PM
@Turzt wrote:
I have a state machine structure (basicly a case structure with different file operations in each case).
I open file in one case, and pass in into feedback loop outside the case structure and input it back to the case structure.
Why are you using a Feedback Node in a State Machine? Why not just use the shift register? In this case, that would make things a little cleaner.
My guess to your problem is that you are not passing the reference through all of your cases. But without code, it is impossible to actually debug.
10-30-2014 04:17 AM - edited 10-30-2014 04:18 AM
The reason for using feedback node for state machine state and other variables is that the state machine is in a subVI which is inside a main VI.
If I put neverending while loop into subVI, the subVI never exits and the main VI gets stuck waiting the subVI.
I made a test and put while loop and shift register outside the state machine. Now it works. So, I must not ever exit the structure that contains the "open file" VI.
So I guess I must open the file in the main level and just pass requests from the state machine subVI. I don't want to create file immediately at start, but I need
to wait user command to create file and start logging data into file, and then other user command to stop logging and close the file.
10-31-2014 01:27 PM
Could you please clarify your question?