From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

Write/Close text file problems

Solved!
Go to solution

There is probably a simple answer to this question, but I cant seem to figure it out.

 

Im using a compact RIO 9068 realtime controller

 

I have a very simple program reading data from a serial port and saving it to a text file.  Before logging starts, I call the open/create/replace file function, then I start reading the port, and  I run the "Write to Text File" Function everytime I recieve a certian number of bytes from the serial port.    Everything works great if I use the program as intended, I press the start button to start logging data, and then press the stop button to stop logging, which then runs the "close file" function .  I get one nice readble text file saved and all is well with the world.

 

This application is intended to log data for hours on end unattended, so I want it to be robust to a power failure.  If I pull power while the data is logging, I still get the file on the hard drive, but it is 0 bytes and there is no data in it.  It seems that If I dont run the "close file" VI function than nothing gets saved.  I am confused because I thought that the close file vi only relases the file from labview control, and doesn't actually write anything to the file.  Anyone have any Ideas on what is wrong?

 

I'd post pictures of the code, but its on a lab machine which isn't internet connected.

0 Kudos
Message 1 of 8
(4,218 Views)
Solution
Accepted by topic author M1080

Check out the Flush File function in the Advanced File Functions menu.  Here is the help file entry for it:

 

Writes all buffers of the file identified by refnum to disk and updates the directory entry of the file associated with refnum.

The file remains open, and refnum remains valid. Data written to a file often resides in a buffer until the buffer fills up or until you close the file. This function forces the operating system to write any buffer data to the file.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 2 of 8
(4,207 Views)

My first guess is that the cRIO is buffering the file in memory and not directly writing it out.  When you do a Close File, it writes it out.  Maybe it will occasionally write it out over time if the in memory buffere fills up.

 

There is a function called Flush File that forces a write to disk.  Try using that.  Maybe if you use that occasionally, it will ge the data out to disk and the amount of data you lose in a power failure is minimized.

 

If a power failure is a serious risk, then I'd consider a back up system.  Perhaps a UPS.  Have the UPS signal your program to close the file and shutdown cleanly if a power failure lasts too long.  I have also used a buffering power supply that takes and supplies 24 V.  If the power is lost, it is still able to supply 24 VDC for about another 1/2 minute (depending on load).  It also has a contact that signals a power failure.  This allowed me to have a motor drive safely store the current parameters in its memory and safely shut down the LV program monitoring it before the drive lost power.

0 Kudos
Message 3 of 8
(4,205 Views)

Thanks for the replies.  That fixed it  I knew it would be something simple.  In my usual applications im usually saving a lot of high rate data, so must be the buffer fills quickly and offloads to the drive fairly frequently.  That is probably why ive never had this problem in the past.

0 Kudos
Message 4 of 8
(4,173 Views)

Note that the storage on a cRIO is flash, so you can only write to each block a limited number of times (around 100,000). The flash controller includes a wear-leveling algorithm that attempts to distribute writes across the entire drive, and probably also waits until it has enough data to write an entire block at once. It's also possible that your data is getting written to the file, but that (again, to avoid constant updates) the directory information about the file size and location isn't getting updated until you close or flush the file. To maximize the life of your flash memory, you shouldn't use the flush file function too frequently. See http://www.ni.com/product-documentation/10126/en/

0 Kudos
Message 5 of 8
(4,161 Views)

I am wrting to a USB sold state Hard drive, so I should be OK.  But thanks for that info, I didn't realize that the internal CRIO memory was life limited like that.

0 Kudos
Message 6 of 8
(4,154 Views)

@M1080 wrote:

I am wrting to a USB sold state Hard drive, so I should be OK.  But thanks for that info, I didn't realize that the internal CRIO memory was life limited like that.


Solid-state hard drives are the same technology as the internal storage in a cRIO, so it has the same limitations, although it's much less of a concern with a large external drive since a larger drive distributes wear over a larger area and you can swap it out in the very unlikely event that it starts to fail.

 

I also have yet to see anyone complain on this forum that they wore out the internal drive in their cRIO, and I suspect that doing so would be an extremely unusual occurrence.

0 Kudos
Message 7 of 8
(4,142 Views)

I wonder how likely the problem would be.  If you force a write to the hard drive fairly often, but not too often, so that you are basically writing out a block of data each time, I'd think each block would be written out just once.  When the next "sector size" block is forced to be written, it should be going to a new block.  I don't think the drive would be rewriting to the enitre file each time.

 

However, if you forced a write to the drive after each and every byte, then I'd think you'd be writing to the same block hundreds to a thousands of times before if finally fills up and the drive moves onto the next block.

 

At least that is my understanding assuming that the file system within a solid state drive is similar to a regular hard drive.

0 Kudos
Message 8 of 8
(4,124 Views)