02-14-2011 01:49 PM
Can anyone tell me why I can't use the "Set Datalog Position" back to overwrite existing records in a circular buffer?
Thanks
Matt
Solved! Go to Solution.
02-14-2011 10:07 PM
That would be problematic if your records don't have uniform sizes. For instance, if you set the position back to a previous record of size 10 bytes and overwrote it with a record of the same type, but size 16 bytes, then you would have accidentally overwritten the first 6 bytes of the next record.
What version of LabVIEW are you using? More recent versions have more powerful functions for reading and writing binary files that have made the old-style datalog files less useful. These functions do support setting a position in the file that would allow you to use the file like a circular buffer. However, here you would still need to be very careful that your records are always a uniform size.
02-15-2011 07:41 AM
I'm using Labview 2010. My buffer size should stay the same size. As one value comes in, one should get removed.
02-15-2011 09:29 AM
Matty,
I'm curious why you're using a log file for this kind of buffer as opposed to an array. You may have a reason for this but it seems like uneccessary overhead if you don't. I have included an example of a fixed circular array buffer.
If you're doing this with a log file for whatever reason, the principle would be the same. You need to maintain a count of how many records have been written and reset the file position and the count when the count is equal to the maximum size for your buffer. To output the record at the "end" of the buffer, you output the line that is about to be overwritten, then overwrite it.
02-15-2011 09:38 AM
In the case that the program ever stops executing, I need to save the circular buffer. Say, someone unplugs the PC. The circular buffer should contain the last saved value plus all others in the buffer. The value will be stored with a time stamp so I can use this to determine first to last entered.
Regards
Matt
02-15-2011 09:59 AM
Ok. I hope this get's you started then:
1. If the "count" is below the maximum for the buffer, read and output the current record, then overwrite it.
2. If the count is equal to the the max, reset the count and read and output the record at the begginning, then overwrite it.
Does that make sense?
02-15-2011 10:07 AM
Unzip my program and run the "File Buffer.vi". I am not worried about deleting after reading. I actually don't care if data in the circular buffer gets overwritten without reading. I just want a file buffer of the most recent data collected. I tried to do exactly what you are saying with the "set datalog position" but it is not working. I am getting unpredicted results.
Thanks
Matt
02-15-2011 11:55 AM - edited 02-15-2011 11:56 AM
I see part of the issue now. The "write data log" vi you use after setting the position sets the position to the end of the file always:
The "set position vi" also mentions this in its help doc though the wording is questionable:
This at least explains why your file keeps growing and does not act like a buffer as you want. Looking through the datalog vis, I can't see a way to work around this issue. You may have to start from scratch using the more basic file vis which don't automatically set the file position.
02-15-2011 12:00 PM
Do you have any suggestions? Or maybe someone else who has done this before.
Thanks
Matt
02-15-2011 12:10 PM - edited 02-15-2011 12:14 PM
You could use the regular text file VIs unbundling the records before you write them in, each on their own line. You'd then have to write a parser for your app to turn them back into records again. I think your positioning scheme would work with regular text files because the "write to text file" vi does not try to set the position and relies completely on "set file position" before it is run. You would also want to wire to file position as opposed to offset.