LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Locking the spreadsheet file when LabVIEW is running.

Solved!
Go to solution

Hello LabVIEW community, I have been trying to establish a solution for the following problem: 

LabVIEW crashes when someone opens a spreadsheet that LabVIEW is writing into. How can I lock the spreadsheet from opening and if someone tries to open it, it opens as a read-only file? 

 

I have attached my simple VI snip and the actual VI for your reference. 

Happy96_1-1687481850191.png

 

Happy96_0-1687481797198.png

 

Also, I found a comment on this forum question but was not able to follow it as I am new to LabVIEW and could not recognize some of the vi used. 
Link: https://forums.ni.com/t5/LabVIEW/Write-to-data-file-with-timestamp/m-p/3794638/highlight/true#M10707...

 

Thank you for the support, Cheers. 

 

 

 

0 Kudos
Message 1 of 15
(1,413 Views)

Try replacing "Write delimited Spreadsheet.vi" with my one.

please set this as ANSWER or KUDO this post, if it help you.


0 Kudos
Message 2 of 15
(1,390 Views)

I do fix some bug and create repo for it in github.

If you would like to load a new version that fixed the bug, you can follow the github here.

https://github.com/Pjumpod/Write_n_lockFile 

please set this as ANSWER or KUDO this post, if it help you.


0 Kudos
Message 3 of 15
(1,384 Views)

Hello, appreciate your help, but I am working on LabVIEW 19 and I cannot open the file you shared. 

Could you please share V19 compatible file, please? 

0 Kudos
Message 4 of 15
(1,373 Views)

here you are for v19

please set this as ANSWER or KUDO this post, if it help you.


0 Kudos
Message 5 of 15
(1,361 Views)

Hi Jumpod,

 


@Jumpod wrote:

here you are for v19


Thanks for conversion!

 

See this snippet after some cleanup:

Your example is very specific for your personal needs, with lots of assumptions for the use case.

And it misses the "file lock" at all, all it does is "open, write, close the file" - basically the same as WriteSpreadsheetFile…

(There's a DenyAccess function to play with.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 15
(1,357 Views)

I believe that your "problem" is an "Operating System" problem -- you are writing to another Computer (reached via TCP) and apparently writing to a location that other Users can also reach and "overwrite".

 

The "safest" way I can think to do this is to set up a system where you write the data twice -- once on your own PC, in your own User Profile (where, I presume, other users of the network cannot overwrite it), and then periodically (or at the end of the data session) copying the "good copy" of the data to the TCP Master, potentially "protecting" it by making it "Read Only" if you do it all-at-once.

 

I would also urge you not to try to "improve" NI's Read/Write Delimited Spreadsheet VIs.  They work just fine as written.

 

Bob Schor

0 Kudos
Message 7 of 15
(1,333 Views)
Solution
Accepted by topic author Happy96

You are writing one row to the file (and occasionally a header row).

 

All you need is to use lowlevel file IO where you open the file once and keep it open for the duration of use, while allowing read-only access. This is also significantly more efficient.

 

(As soon as you use "write to delimited spreadsheet" each write operation opens and closes the file with each call and at any other time the file can be grabbed by any other application.)

Message 8 of 15
(1,326 Views)

Thanks, Altenbach. 

The solution you suggested does work. One change from what you mentioned:  I had to allow Write Only Access because with read-only access you cannot write to that file and my VI was breaking. 

One last follow-up question: Where would you recommend I add the "Close File. VI" in my code? 

 

The purpose of my code is to have a daily log of PTH for which it creates a new file at midnight every day and logs in that file until the next midnight. 

I believe having the "Close File VI" in such an application is important but I am not able to figure out how/where to add that as currently all my solution fails. 

 

Below are the snip of my edited code and I have also attached the VI. 

Happy96_0-1687545175613.pngHappy96_1-1687545201302.png

 

 

0 Kudos
Message 9 of 15
(1,306 Views)

I have been working to add the "Close File VI", but not getting success. This is the logic I applied on my last try. Not able to determine why it is not working. All I know is that the VI is not entering the 3rd Case. 

Here are the snips and the attached VI

Happy96_0-1687553572443.png

Happy96_1-1687553607270.png

 

Happy96_2-1687553632552.png

 



0 Kudos
Message 10 of 15
(1,291 Views)