From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

TDMS file on disk doesn't get updated with AutoSave set to true when appending data to channels

Solved!
Go to solution

Hello friends,

I am developing a WPF application that needs to log data to TDMS files. I append data to channels in the TDMS file periodically. The TDMS file's AutoSave property is set to true upon creating it (although I'm pretty sure true is the default value of this property).

 

With the AutoSave property set to true, I would expect to see the file's size grow in windows explorer as my application runs and appends more data to it however, this is not what happens. In fact, the TDMS log files don't get updated until the application is gracefully shutdown. If I don't gracefully shutdown my application (clicking exit button in upper right hand corner) and I stop the debugger from visual studio, the TDMS log files remain at 0KB.

 

Why aren't my TDMS files getting saved to disk as my application runs, even though AutoSave is set to true?

 

Below is my class responsible for writing data to TDMS files. The WPF application calls one of the AppendData() overloads periodically. The CreateNewSegmentInLogFile() function is called once at application startup.

 

internal class TdmsFacade : ITdmsFacade
    {
        private string _latestChannelGroupName;
        private readonly TdmsFile _logFile;

        internal TdmsFacade(ITdmsData data, string filePath)
        {
            // Initialize various file options for accessing tdms files.
            var tdmsRwFileOptions = new TdmsFileOptions
            {
                Access = TdmsFileAccess.ReadWrite,
                FileFormat = TdmsFileFormat.Version20,
                BufferingDisabled = true
            };

            // Assign initial properties to log file & create it.
            _logFile = new TdmsFile(filePath, tdmsRwFileOptions)
            {
                Author = data.Author,
                Description = data.RootDescription,
                Title = data.Title,
                AutoSave = true
            };
        }

        public void AppendData(double[] samples, TdmsChannelEnum channelToLogTo)
        {
            // Get the channel group.
            var channelGroup = _logFile.GetChannelGroups()[_latestChannelGroupName];

            // Get the channels.
            var channels = channelGroup.GetChannels();

            // Append data to the channels.
            switch (channelToLogTo)
            {
                case TdmsChannelEnum.ChannelZero:
                    channels[0].AppendData(samples);
                    break;
                case TdmsChannelEnum.ChannelOne:
                    channels[1].AppendData(samples);
                    break;
                case TdmsChannelEnum.ChannelTwo:
                    channels[2].AppendData(samples);
                    break;
                case TdmsChannelEnum.ChannelThree:
                    channels[3].AppendData(samples);
                    break;
                default:
                    throw new InvalidOperationException("An invalid channel chosen to log to. A channel will be considered invalid if it is not utilized by the DAQ.");
            }
        }

        public void AppendData(double sample, TdmsChannelEnum channelToLogTo)
        {
            // Get the channel group.
            var channelGroup = _logFile.GetChannelGroups()[_latestChannelGroupName];

            // Get the channels.
            var channels = channelGroup.GetChannels();

            // Append data to the channels.
            switch (channelToLogTo)
            {
                case TdmsChannelEnum.ChannelZero:
                    channels[0].AppendData(sample);
                    break;
                case TdmsChannelEnum.ChannelOne:
                    channels[1].AppendData(sample);
                    break;
                case TdmsChannelEnum.ChannelTwo:
                    channels[2].AppendData(sample);
                    break;
                case TdmsChannelEnum.ChannelThree:
                    channels[3].AppendData(sample);
                    break;
                default:
                    throw new InvalidOperationException("An invalid channel chosen to log to. A channel will be considered invalid if it is not utilized by the DAQ.");
            }
        }

        public void CreateNewSegmentInLogFile(ITdmsData tdmsData)
        {
            // Add a new channel group.
            var now = new DateTimeProvider().GetDateAndTime();
            _latestChannelGroupName = now.ToString().Replace('/', '_').Replace(':', '_').Replace(' ', '_');
            var channelGroup = _logFile.AddChannelGroup(_latestChannelGroupName);
            foreach (var property in tdmsData.ChannelGroupProperties)
            {
                if (property.DataType.Equals(TdmsPropertyDataType.DateTime))
                {
                    property.Value = now;
                }

                channelGroup.AddProperty
                (
                    property.Name,
                    property.DataType,
                    property.Value
                );
            }

            // Add channels to the new channel group.
            foreach (var channelInfo in tdmsData.ChannelInformation)
            {
                var channel = channelGroup.AddChannel(channelInfo.ChannelName, channelInfo.DataType, channelInfo.ChannelDescription, channelInfo.UnitString);

                // Add the properties.
                foreach (var property in channelInfo.ChannelProperties)
                {
                    if (property.DataType.Equals(TdmsPropertyDataType.DateTime))
                    {
                        property.Value = new DateTimeProvider().GetDateAndTime();
                    }

                    channel.AddProperty(property.Name, property.DataType, property.Value);
                }
            }
        }
    }
0 Kudos
Message 1 of 7
(2,631 Views)

Can you provide version numbers for any relevant software/driver/api versions you are using?

0 Kudos
Message 2 of 7
(2,590 Views)

Could you also humor me by setting BufferingDisabled to false and noting any changes in behavior?

0 Kudos
Message 3 of 7
(2,580 Views)
Can you provide version numbers for any relevant software/driver/api versions you are using?NationalInstruments.Tdms.dll Version=15.0.45.49153

The version of the NationalInstruments.Tdms.dll I am referencing is 15.0.45.49153.

Could you also humor me by setting BufferingDisabled to false and noting any changes in behavior?

Sure, setting BufferingDisabled did not change the behavior. The .tdms file still doesn't get written to until the application is closed.

0 Kudos
Message 4 of 7
(2,563 Views)
Solution
Accepted by topic author b!tmaster

That is unfortunate, I was playing around with this setting recently and found that setting BufferingDisabled = false unexpectedly caused AutoSave to work properly.

 

It seems that this may be a bug and there is an open corrective action request (CAR) for it: 643616

 

I recommend you check the readme of future releases to see if that number is listed among the bug fixes.

0 Kudos
Message 5 of 7
(2,556 Views)

brettski,

 

Thanks for the info. I will keep my eyes peeled for any information.

0 Kudos
Message 6 of 7
(2,551 Views)

I have confirmed that setting BufferDisabled to false allows the AutoSave feature to work properly, strange!

0 Kudos
Message 7 of 7
(2,437 Views)