07-07-2021 04:13 AM
So I have the following:
1) A text file, which is I have attached as "CH1234.log", which is a delimited spreadsheet with the /t delimiter.
2) I would like to pull the latest information and display on the HMI. The text file updates every 1 minute.
Question: What is the most simple approach? Right now I am just playing around with this setup, where I am getting all the rows and the first row. Unfortunately there is no such "last row". So basically, should I just read all the rows, store it into an subarray and hence get the latest row in that array and display?
07-07-2021 04:45 AM
Hi Christian,
@ChristianLiinHansen wrote:
1) A text file, which is I have attached as "CH1234.log", which is a delimited spreadsheet with the /t delimiter.
2) I would like to pull the latest information and display on the HMI. The text file updates every 1 minute.
You can read the full file, and parse the whole content into an array and get the last row of that 2D array. Once a minute is not that big problem…
You can improve the VI like this:
07-07-2021 05:01 AM
@GerdW wrote:
Hi Christian,
You can improve the VI like this:
- Store the current length of the file in a shift register. When the file gets updated (hopefully by appending new data at the end) you can determine the new filelength and only read the new data by using SetFilePosition before ReadFile…
- You may use ReadTextFile to read an array of lines/rows from your text file. Now you get a 1D array of "rows": index the last one. (You may also use the SetFilePosition approach to read only the latest line…)
Hi GerdW. Thanks for your answer, but how do I use Read Text File to read the last array?
07-07-2021 05:36 AM
Hi Christian,
@ChristianLiinHansen wrote:
how do I use Read Text File to read the last array?
You use Read(Text)File to read the last (newly appended) bytes/chars as a string.
You can use SpreadsheetStringToArray to convert a string into an array afterwards. (ReadDelimitedFile does the very same as you can see in its block diagram!)
07-07-2021 05:44 AM
@GerdW wrote:
You use Read(Text)File to read the last (newly appended) bytes/chars as a string.
You can use SpreadsheetStringToArray to convert a string into an array afterwards. (ReadDelimitedFile does the very same as you can see in its block diagram!)
But...:) How will the block diagram looks like, in order to to use Read Text File to read the last bytes/chars as a string?
Because the idea of reading the whole thing with array size and subtract to read the 2nd last line, looks pretty messy right now:
07-07-2021 06:36 AM
So I managed to think about a way to access the last line.
07-07-2021 07:01 AM - edited 07-07-2021 07:02 AM
Hi Christian,
@ChristianLiinHansen wrote:
Because the idea of reading the whole thing with array size and subtract to read the 2nd last line, looks pretty messy right now:
Use MatrixSize instead auf ArraySize followed by two IndexArray nodes: you get #rows and #columns directly!
Now you want to read the "2nd last row", before you always wanted to read "the last row": that's a difference! Maybe you should learn to write good questions before starting to code? 😄
@ChristianLiinHansen wrote:
How will the block diagram looks like, in order to to use Read Text File to read the last bytes/chars as a string?
Simple:
07-07-2021 07:04 AM
Well, looking in the logfile, there will always be appended a CR + LF, so why should I then want the last line? 🙂 Anyway, thanks for the help 🙂
07-07-2021 05:10 PM
Your file contains lines with metadata about actions, so you need to adjust the algorithm to find the last line with suitable data. You already got some good advice, so I won't repeat all that. There is definitely no need to read and parse more than a few hundred bytes.
What is writing this log? (Another LabVIEW program? Some third party program?). You might need to deal with access rights, e.g. if you try to read wile the other program has it open for writing. Make sure to open it read-only if you only want to read, etc.