LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

write string to file

im reading values from a device that puts out a string, but i need it to save that string in a text file or excel spreadsheet. i cannibalized some example vi but cant get it to work. another thing i need to work on is getting it to save every six readings.  it doesnt matter if the program prompts the user every six readings, but im not sure how to do it. the data does not even have to be saved as a string... it is a string containing numbers, and all i care about are the numbers. im excited about labview but all this graphical programming is daunting and confusing... so any help will be appreciated!

 

thanks! 

0 Kudos
Message 1 of 12
(2,894 Views)
sorry i meant to post my work so far:
0 Kudos
Message 2 of 12
(2,893 Views)
The example you cannibalized used the low-level (old-style) GPIB functions. While you seem to still be using LabVIEW 7.1, it is recommended that all new code should use VISA. From what I can gather, you want this program to run continously? If so then you need to use a while loop. I'm not sure I understand the bit about saving every six readings. What is the response to the "C" command? You said you get a string containing numbers. Is this six numbers, or is it something else? If it's six numbers does this mean you want to save the response after every "C" command, or do you want to save it after you sent six "C" commands and got the responses each time. If that's the case, why six?
0 Kudos
Message 3 of 12
(2,887 Views)

hello. thanks for replying! sorry, i did not make my post clear enough:

 

my temperature reader is connected to six thermometers, but only has one input channel. therefore i use a seperate circuit to switch between the six thermometers, taking one reading at a time.

 

i start the program, and press "read" to obtain readings from a temperature reader. the temperature reader displays temperature constantly, but since my application is in steady-state, i just care about getting one reading, ie temperature reading at the instant I press "read". that is why, for collecting data, i do not think need a loop.

 

when i write "c" i am sending a "clear" command to my temperature reader, so that i can take another reading. i do not remember the exact response at the moment, though i could post it tomorrow. i am not very familiar with daq and interfacing, but i found that this seems to work for my device; that is the only reason i write "c".

 

so, i would like to be able to do the following:

 

read one value from my instrument (which will be displayed as a string). store that value in an array or something (maybe i could have it so the user presses a button, which tells the program to store whatever string is displayed into the array). then use my external switching circuit to switch to another thermometer. write "c" to clear, and then read again. store that value in the array or whatever. repeat until i've cycled through all six thermometers. once i'm done, i want to be able to save the set of six readings as an excel file (text file is also acceptable). 

 

my thought is that i was on the right track with the vi i posted, though i may be completely wrong. the program as of now prompts me to specify the filename and directory, but i cannot seem to get past that. this might be a problem with loop placement? i also am a bit stuck on how to get the program to store six readings in a file. 

 

thank you!

0 Kudos
Message 4 of 12
(2,876 Views)

Your problem with loop placement is that you don't have any kind of loop.  Your program runs once.

 

In the current state, you need to have your buttons pressed, then press the run button, then it will pop up with a dialog box to pick a file, write it out, close the file and end.  Then to get the next reading, you have to press the run button again.  (Please don't tell me you are using the Run Continuously button.)  At which time it will ask for the filename again.

 

Use a filepath control on the front panel.  With that, you can select the file once.  Wrap your  Clear and Read sections in a while loop.  You probably actually want both buttons to be Latched when Released rather than Switch when pressed, so that they only occur once for each button press.  Put a small Wait statement in that loop so it doesn't run full speed.

 

Put your open file ahead of the while loop and the close file after the while loop.

 

I would recommend you learn more about LabVIEW from here. How to Learn LV

0 Kudos
Message 5 of 12
(2,867 Views)

hello ravens fan. thanks for your reply!

 

in fact i was using the run continuously button (sorry!), which explains why i kept on getting prompted to select a file. i have played around with the loop quite a bit and have it somewhat working. following your suggestions, i was able to get it so that the program continuously reads from the temperature reader, and appends each reading to the same file. almost exactly what i want. i am in a hurry at the moment so i will not be able to collect my thoughts until later, but here are some issues i plan on tackling:

 

i just need it to record the reading the instant at which the user clicks the read button. currently, the program reads continuously every 50ms, and i'm not sure how to fix that.

 

i also need it to have a space in between readings in the text file, so that when i open it in excel it will be able to separate the data into cells. (is it possible at to have it just save to an excel file? i have not tried it yet but plan to.)

 

otherwise, it looks like things are falling iinto place. thanks for your help so far!

0 Kudos
Message 6 of 12
(2,843 Views)
here is what i have so far. it isn't great programming but it seems to sort of work.
0 Kudos
Message 7 of 12
(2,842 Views)

I added an Event Structure to your code so that you can easily handle the buttons on the front panel.

 

The "six readings" stuff arent clear to me. Do you trigger them manually or should they be taken at constant time interval?

Why do you write to a bin file? Since it's all text why don't you use the "Write to Text file" vi?

Now you write each measurement to the file, you could build your string with 6 readings and write them at once to the file. Don't need an array to do that, just concatenate the succecive results. There's still work to do regarding your string since it's not clear to me what you programmed in the first place 😞

 

0 Kudos
Message 8 of 12
(2,833 Views)

As I said, set your switches for Latch When Released action rather than Switch action.  This way they only are read one time in the true position.  They will automatically jump back to false until you press the button again.

 

There is a Write to Spreadsheet file function.  Note, this does NOT make an Excel file.  It creates a text file where data is separator with delimiters.  And rows of data are separated by end of line characters.  This works better for 2-D data.  In your case, you are sending a value at a time.  The question is what do you want the delimiter to be?  (It looks like you have a space separating data now).  Perhaps a comma or tab character.  Depending on what separator you use, and how you open it in Excel or import it into Excel determines whether the data shows up in separate columns or all mammed into one column.  At some point, do you want the data to stop going into one row and start a new row (essentially becoming 2-D).  If so, you will need to determine how many points per row you want and how to determine when to start a new row.

 

 

EDIT:  Alain,  good catch on the binary file.  I missed that.  But when writing strings, as long as preprend array or string size is turn off (which it is), I don't think it matters.

Message Edited by Ravens Fan on 07-15-2009 02:02 PM
0 Kudos
Message 9 of 12
(2,824 Views)

I think I understand about the six readings based on the earlier post. Basically, he wants to take six readings at a time, but he needs to move a mechanical switch to go to the next sensor. When he moves the switch he wants to take the next reading. Once he's got six, that's the whole lot, and it should be written to file.

 

Given the on-demand reading requirement I was also going to suggest using an event structure, assuming that the user doesn't have LabVIEW Base, which doesn't have event structures. As far as the code with the event structure:

  • The GPIB address seems to have gotten lost in translation. Even so, you don't need a shift register for this.
  • The timeout for the event structure is not necessary, and actually the Timeout case can be deleted. The timeout value simply forces LabVIEW to execute the Timeout case every 10 msec to do absolutely nothing. Leave the timeout value unwired.  
  • I would recommend using rectangular buttons with the latching action.

As for the data collection/saving: Assuming what you want is six readings per line, and then each line being a new set of readings (say at a new setpoint), I would use a shift register that's initialized with an array of 6 elements. Use another shift register to keep track of which sensor you're reading. Whenever you take a reading you replace the corresponding element in the array. Once you've got 6 you can write it out to file using the Write to Spreadsheet File.
0 Kudos
Message 10 of 12
(2,822 Views)