LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Ensuring a tdms is not being used by another code section

Greetings everyone, this is a somewhat complicated problem to which, sadly, I cannot attach code since it's both propietary and rather large, so I will try my best at explaining it:

 

I have a total of 4 while loops: a main one that does most of what the user actually sees, one that does general mantaining and simple data generation for the user to see (which is not important so let's ignore it for now) and finally, a producer and consumer. Basically, the producer gives me data the consumer then splits into several sections, the two I'm struggling in are buffers. A data cycle is stored as buffer, so when the main buffer is filled, it is stored to a "backlog" and emptied so it is filled with the new data. Up to this point I have no problem, however now I want said buffers to be stored to a main log when a certain event triggers, and that's where I see the problem.

 

I will need to take the data of both current and past buffers, and write it to the log in the main loop, however, if the consumer loop has the .tdms open it will return an error, so I would like suggestions on how to make sure the consumer loop is not using the tdms buffers before attempting to open them, ideally without adding too much coding but any suggestion helps.

0 Kudos
Message 1 of 5
(2,905 Views)

I really don't "get" what you are trying to do, but have a suggestion, anyway.  Build a very small test routine, with four loops, and set up your Producer/Consumer to generate random numbers at, say, 100 points/second.  Set up the TDMS creation and writing as you are in your proprietary routine, and see if your small model reproduces the problems you've been seeing.  If you can reproduce the problem with a small demo routine, you can post it and we can pick it apart ...

 

Bob Schor

0 Kudos
Message 2 of 5
(2,880 Views)

@Daikataro wrote:

I will need to take the data of both current and past buffers, and write it to the log in the main loop


No.  Your file should only be handled in one location.  Instead, have your main loop send the data to the logging loop and let it handle the saving of the data.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 5
(2,856 Views)

I agree with the others saying it should probably be designed differently.  But that being said the TDMS API is crazy robust, and I've done some testing where I tried breaking it but I couldn't.  I had something like 5 writers in parallel, and a couple of readers, and the way the buffers worked things always seemed to do what they should without causing odd race conditions.  I wouldn't want to trust this undocumented feature, but it seems that under the hood, there is locks to make sure things get read and written correctly.

0 Kudos
Message 4 of 5
(2,848 Views)

Seems like I got lucky at breaking it then, since before solving the problem, I got myself a myriad of errors, which most likely arose from the way buffers are managed. Since I'm developing for LV 2013 the "delete data from tdms" function is not available, so I overwrite the old buffer with the new one. Algorythm goes as follows:

 

-Fill buffer 0 while cycle stays the same

-When cycle changes, open buffer 0, extract contents, write to buffer 1, then overwrite buffer 0 with a blank file

 

I solved it using a semaphor in the main section of the code and the consumer loop, which is acquired by the write to buffer, so main code will always have to wait before attempting to use the buffers. I know they're not exactly agile so, is there a better solution for this? Would a small model of the code help with alternatives that speed up code? Worth mentioning that currently, I am not having any issues with speed, but might have them in the future, so just for good measure.

0 Kudos
Message 5 of 5
(2,817 Views)