From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

Creating time stamped filenames inside a consumer loop

Solved!
Go to solution

Greetings,

I would like some help please in creating data files with time stamped file names from inside a consumer loop. The loop is receiving queued data from three sources, at different rates, in string format, which then has a time stamp appended.It is then sent to a txt file in json format if the record button on the front panel has been pressed. Currently, my code creates a new file every second as the file name changes with the PC clock. If my file name sub-VI is outside the loop, it will only ever read the time at startup of course. How can I create a filename once, at the time the record button is pressed?

I am sure this can be done, maybe with shift registers and a bit of Boolean, but I can't seem to crack it.
If there are any glaring errors with my loop architecture I would be happy for you to point them out too 🙂

0 Kudos
Message 1 of 5
(2,726 Views)
Solution
Accepted by topic author KarlosAmigos

Think about what you want to do, what is true only before you do it the first time, and what you might want to do only once, when everything is done.  Once you know what, you can worry about how.

 

So what you want to do is to start recording when the "Record" button is first pushed.  You want to keep recording into the same file until you exit the loop, at which point you want to close the file.

 

So what is True before you push Record?  Not obvious, but let's ask what becomes true right after you push it?  What do you do?  You open a File, which means you create a File Reference.  You want to keep writing to that file (until you exit the loop), so you might as well keep the File Reference around.  So before you push Record, you don't have a File Reference.  Can you detect this?  Of course -- look in the Comparison Palette at "Not a Number/Path/Refnum?".

 

So you carry the File Reference in a Shift Register, without initializing it (so it initially has the default value "Not a Refnum".  You can do your code several ways, but here's one:

  • Have a Case guarded by "Not a Number/Path/Refnum?". 
    • If this is false, you've already opened the File, so you need only to use the Refnum with a Text Write and write out your data. 
    • If it is true, however, the file hasn't (yet) been opened.  Check if Record is false -- great, do nothing.  If it is True, get the file name using the current time (I personally hate File names with the Time as part of the name, but To Each His/Her Own), open the file, and write the first Record, passing the (now-defined) Refnum into the Shift Register for all subsequent iterations of the loop.
  • When the loop exits, use the Refnum in the Shift Register to Close the file.

I hope this makes sense to you.  You'll learn more if you write the code yourself ...

 

Bob Schor

Message 2 of 5
(2,700 Views)

Thanks for your detailed reply Bob, but I still can't get this to work. To elaborate; my program has to keep running after I close the file, so I can't close the file outside of the consumer loop. I have a front panel that displays data from my producers, and at different sites (I am on a boat) I have to log data for that site. The file name therefore needs a different name with a unique site number and a time stamp. If I get rid of the record button, and just start the program and stop the program when on site, it works perfectly. In this case, I also have the open/create file and the close file outside of the consumer loop. I will keep working on it.....

I hate file names with time stamps too by the way, but it's what our data people demand 😕

0 Kudos
Message 3 of 5
(2,676 Views)

Another Chance to Learn (or to Re-Design).  So you have the following things that you do sequentially --

  1. Decide on a Site, which determines some aspects of the File Name and where the data originates.
  2. Start Recording from that Site.
  3. Stop Recording from that Site.
  4. Optionally choose another Site. and repeat Steps 1-3.
  5. Alternatively, end the Program.

Sounds to me like a State Machine, initialized with "Choose Site" (which might also have a button that says "Exit Program", visible only in this State).  Once you Choose Site, you go to the "Record From Site" State, which is largely what I described in my previous post.  The only difference is that "Stop" becomes "Stop Recording", and we don't exit, but we go back to the "Choose Site" State.  Pushing the Exit Program button takes us to the final "Clean Up and Exit" State.  How does that sound?

 

Incidentally, I notice a Tag Channel Wire in your Snippet (which I couldn't open).  I'm a big fan and heavy user of Channel Wires!

 

Bob Schor

Message 4 of 5
(2,656 Views)

Hi again Bob,

Back to coding now that Christmas is over, I hope you had a good one.
I had thought about changing the architecture to a state machine, but in the end I found a solution based on your first suggestion. I am now using a shift register for my file path, It is controlled by a vim I hadn't seen before called "Is Value Changed". It's a Malleable VI and fairly new I think. Anyway, it works like a charm. For the first change when the output goes true when I press the record button, I have a sub-VI with time stamp generator etc that creates the file name. For subsequent loop cycles, the case is false and I just pass the file name through.
Many thanks again for your guidance 🙂
Karl

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