ihedvall / mdflib

Implementation of the ASAM MDF data file.
https://ihedvall.github.io/mdflib/
MIT License
57 stars 26 forks source link

About write MDFfile #14

Closed nealwang123 closed 1 year ago

nealwang123 commented 1 year ago

There is a requirement to record data in MDF files in the standardization record function for automotive components.Would it have been possible to support it earlier

ihedvall commented 1 year ago

There already exist some write support in the library so finalize it is a relative small work. However there are a large number of way the sample values may be stored. MDF3 support writing of values but in MDF4 there are a lot of features added. Adding support of all the features is several months work. I need some help regarding this by the end-user of the library.

Examples:

Well, I need help to define the input and the output of the MDF writer, simply some requirements.

An initial proposal from me is to implement one CAN logger writer interface and one simple writer interface that store compressed samples (DZ) blocks. Any ideas?

nealwang123 commented 1 year ago

I'm just starting to get into the mdf file format, and all I can think of is if we can define each module as a class first for easy extension later.

ihedvall commented 1 year ago

I will finalize the basic MDF4Writer and leave the bus logger to later. The MdfReader object (include/mdfreader.h) is relative easy to use.

  1. Select a MDF file and create a MDFReader object with file path.
  2. Open the file.
  3. Depending on how much information you want from the file, you may call one of the ReadXX() functions. Normally it's OK to call the ReadEverythingButData() function.
  4. Now you need to select which values you want out from the file. This is done by creating a sample observer and add the channel you want. The channel observers holds the channel values in primary memory,
  5. Next is to read in the samples into the sample observers. You do this by calling the ReadData(..) function. You need to select which measurement block (DG block) to read.
  6. You can now close the file and start do something with the channel values.

The MdfWriter is somewhat more difficult to use as the writer must write the MDF blocks at the end of the file. So the following sequence is recommended to follow.

  1. Create an MdfWriter object through the MdfFactory class. You need to select type of output file.
  2. Call the Init function and supply the file path. This function actually read in any existing MDF file in case you want to append a measurement.
  3. After Init(), you should call the Header() function and optional add any meta data i.e. information about the test and it's test object. You should also add a file history (FH) block.
  4. Now it's time to create a new measurement (DG) block and add channel groups (CG/SI) and channel configuration (CN/SI/CC). This requires some knowledge about MDF files.
  5. Optional you may set up a pre-trig time and init the measurement. This saves the previously added blocks and starts a thread with sample cache (max size is the pre-trig time).
  6. For each sample you need to set the channel values and the call the SaveSample() function for each channel group.
  7. The cache is in the primary memory and nothing is stored onto the disc at this point until you call the start measurement. The cache and all samples to follows are store onto the disc.
  8. Call the StopMeaasurement() which just purge out any remaing cache samples.
  9. Before calling FinalizeMeasurent(), you should add any remaining blocks for example attachments. The function generates a valid MDF files.
  10. You may now call InitMeasurement in case you want to create a new measurement.

I'm currently working with the workflow server (eventlog) for 1-2 more weeks. I start working with the writer after that. It's about 2 weeks work left plus 1 week to finalize the documentation.

ihedvall commented 1 year ago

I have added support for basic MDF writing through the MdfWriter class. The unit tests in te/testwrite.cpp show the usage.