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: 

Write to new file at midnight but record data constantly

Solved!
Go to solution

Hi, 

I'm writing a system monitoring program and I need it to monitor constantly and log a data point every ten minutes or so, and I would like the program to start writing to a new file each day at midnight.  I have everything set up, but after starting the program it just continuously appends to the same file rather than writing a new one.  This is because I have the part that initiates the spreadsheet file outside of the data collection and storage loop, but if I put it inside the loop then it either overwrites the file or appends my column headers to the file everytime it stores a data point. 

Is there a way to make what I'm doing work (forcing my program to create a new file because it's trying to append to a file whose name is based on the current date) or is there a way for labview to figure out that the date has changed so it needs a new file?

Thanks

0 Kudos
Message 1 of 37
(3,942 Views)

Hi Liz,

 

THINK DATAFLOW!

 

The file path is generated before the loop and so will stay the same in the loop!

 

You need to create the file path inside the loop and you need to apply some program logic to change the filename at midnight:

Example_BD.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 37
(3,909 Views)

Your "mildly complicated" program should probably be structured as a state machine or something similar. Such architectures are robust, easily maintained, and easily modified when features need to be added.

 

In your case you could have a "New File" state. It would be called when the program first starts and then every midnight. It would include the Write to Header as mentioned by GerdW. It would pass the file path via a shift register to the "Save to File" state, which gets called every ten minutes.

 

Other states might include Initialization of Data Acquisition, Acquire Data, Process Data, Alarms, Error Handling, Close Data Acquisition, and others depending on what the program actually does.

 

All recent versions of LV have a Design Pattern or template for state machines.

 

Lynn

 

0 Kudos
Message 3 of 37
(3,897 Views)

How can I tell it to call the program at mightnight?  Also I don't see where GerdW mentions Write to Header. Is that a function that LV has? If so I cannot locate it. 

0 Kudos
Message 4 of 37
(3,882 Views)

Hi Lizzy,

 

your VI runs as long as you don't click the stop button, so you don't need to call it at midnight…

 

"writing a header" is no LabVIEW function. I wrote "you need to apply…" and "you need to implement…"

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 37
(3,877 Views)

Liz,

 

GerdW and I sometimes read each other's minds and answer questions for each other. It is fun for us but sometimes confusing to those who start the threads. If I confused more than helped, I apologize.

 

Write to Header is not a specific VI. I used capitals to emphasize the function. It could even be a separate state if you need to write headers at times other than the beginning of a new file.

 

How do you "call" it at midnight? First, a slightly more detailed explanation of what we understand by a state machine is in order. A state machine is typically implemented in LV as a while loop containing a case structure.  One or more shift registers pass information from one iteration to the next. Each state is implemented as the code in one case of the case structure. If you create an enumerated control (usually called enum) and use that to name the states, then the state names appear in the selector label at the top of the case structure. This makes it very easy to tell what each state (case) does, assuming you have descriptive names for the states. It is also important to make the control a type def because any changes will automatically propagate to all controls and constants made from the typedef.

 

A state machine determines the next state by looking at all the information it has available at the time the code for that state has completed execution. In your case It would check the time to see if it was time for a new data point to be acquired, time to save to the file, time to create a new file, or just enter an idle state to wait. Other things it migh check would be alarms, errors, and the Stop command from the user. The "next" state is passed to the shift register and the left side of the shift register is wired to the case selector. The next state might be the same as the state currently executing. If you only want to acquire data once every minute and save it every ten minutes, you might go to the Idle state where you wait for 1/4 second before checking for the next state. If no errors occur and the user does not press the Stop button, the program would repeat the Idle state ~240 times before going to the Acquire Data state, the Process Data state, and back to Idle again. By only spending 1/4 second each time it enters the Idle state, you or the user never needs to wait more than 1/4 second to get a response to the Stop button. If you had a 1 minute wait (the assumed time between acquisitions), the program might wait a full minute after the user pressed the Stop button before anything appeared to happen. That is very frustrating for users.

 

Lynn

0 Kudos
Message 6 of 37
(3,862 Views)
Do you know what a shift register is? When the program starts, you just have to get the current date and store it. Before you do the actual write, check if current date is equal to the value in the shift register. If it's not, call the new file routine.
0 Kudos
Message 7 of 37
(3,858 Views)
Solution
Accepted by topic author LizRyland

Simple state machine with shift registers, elapsed timer, comparison..Modify as necessary

Example_VI_BD.png

new file every minute, data record every 2secs....see if you can understand the logic

Message 8 of 37
(3,829 Views)

Thanks so much. I was able to modify this to do what I needed to.

0 Kudos
Message 9 of 37
(3,775 Views)

I'm also having the same problem with writing to a file at midnight. However, I cannot open the VI you uploaded due to my LV version being older than version 13. Could you possibly explain how to modify my VI so that it writes to a new file at midnight?

 

Thanks,

 

Walsh

0 Kudos
Message 10 of 37
(3,703 Views)