12-17-2017 04:20 AM
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 🙂
Solved! Go to Solution.
12-17-2017 09:16 AM
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:
I hope this makes sense to you. You'll learn more if you write the code yourself ...
Bob Schor
12-18-2017 03:29 AM
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 😕
12-18-2017 12:05 PM
Another Chance to Learn (or to Re-Design). So you have the following things that you do sequentially --
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
12-27-2017 05:50 PM - edited 12-27-2017 05:53 PM
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