LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I write data to a file for only a specified amount of time?

What I'm trying to do (with my included file "10lbcalibration4.vi") is have it record data for only a specified amount of time. It needs to wait 20seconds as shown on a countdown timer, and then record data for 5 seconds before stopping. Currently it is set up to run for 25 seconds (20 seconds of wait + 5 seconds of recording). The program runs, but it does not write to file properly. Could anyone show me what I'm doing wrong? Thanks.
0 Kudos
Message 1 of 10
(3,415 Views)
First of all you're going to have to eliminate the race condition with the two local variables. They're not necessary at all and could be part of your problem. Wire the output of the one outside your while loop to the wire look itself and into the subtract function. Take the output of the timer inside the loop and wire it directly to the other input of the subtract function. Also, without running it and being sure, I don't understand why you're using the Remainder and Quotient function. The output of the subtract function is in milliseconds and to convert to seconds, all you need do is use the normal divide function.
Message 2 of 10
(3,415 Views)
I don't understand what you mean by "eliminate the race condition" - what is a race condition and where is it in my program?
0 Kudos
Message 3 of 10
(3,415 Views)
Race conditions can occur when you use local variables and you're not careful about dataflow. In your case, it's inderterminate when the two locals I mentioned are written too and read from. You don't know what happens first. Is the local first written to with the results of the millisecond timer or is the local variable first read from. LabVIEW will attempt to do things in parallel when it can and what you've done is put the read and write in parallel and there's no guarantee that the order you want is the order you're going to get. I modified your VI to not use locals. What I got what a program that would acquired data for 25 seconds and for the last 5 seconds, it would write to a file. another thing I noticed was that the file writ
e that you do when the while loop is done, is set to overwrite and not append.
0 Kudos
Message 4 of 10
(3,415 Views)
I changed the write function to append instead of overwrite. That fixed the problem of having my data erased, but now it displays my data on top and the headers on the bottom. How do I switch them?
0 Kudos
Message 5 of 10
(3,415 Views)
Nevermind, I fixed it. One other thing: It is important that the user can save the file (or choose the name and location of the new file) before actually running the program. The way it is set up, it will only let you write to an existing file, not create a new one. Is there any way to do this?
0 Kudos
Message 6 of 10
(3,415 Views)
Change the order in which you write. Remember - LabVIEW is a dataflow language. If you wire the new file path output of the Write Characters to File to the File Path input of your Write to Spreadsheet File function, you force the order. Your file path control should be the input to the Write Characters function. For appearances sake, it would be nice to put the header stuff on the left side of the diagram but it is the dataflow that will force one thing to happen before another.
0 Kudos
Message 7 of 10
(3,415 Views)
Easy. Use the File Dialog function on the File I/O>Advanced File Functions palette. Both of the file write functions in you VI will call this if the file path is empty.
0 Kudos
Message 8 of 10
(3,415 Views)
How would I hook that up to a file path control (otherwise, the save file dialog comes up when you start the program - the user needs to be able to specify the name and location of the saved file before the application starts running.)
0 Kudos
Message 9 of 10
(3,415 Views)
If you ever want to build your app into an executable, I would recomend replacing the file path control with the dialog and design it so that is the first thing that happens. By default a built app will run when opened. You can change that but it's usually a hassle for the user to open a program and then have to click on some run arrow. If you don't want to go that route, just look at the file path control. There's a little icon to the right that looks like an open file. Click on that and your get the regular Windows file browse window. You can right click on that icon and select Browse options to specify a prompt, selection mode, etc.
0 Kudos
Message 10 of 10
(3,415 Views)