alesi2000

ASAP2MDFWriterExample

Jun 14th, 2014 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using jnsoft.ASAP2;
  2. using jnsoft.Helpers;
  3.  
  4. namespace jnsoft.MDF.Examples.Writer;
  5.  
  6. /// <summary>
  7. /// MDF Writer example.
  8. ///
  9. /// - Loading an A2L file
  10. /// - Recording sample data for all Measurements contained in the specified A2L file
  11. /// - Recording a 4k raw data sample block.
  12. ///
  13. /// Usage: ASAP2MDFWriterExample A2LFile.a2l
  14. /// </summary>
  15. class Program
  16. {
  17.   static int Main(string[] args)
  18.   {
  19.     if (args.Length == 0)
  20.     { // no args -> present usage
  21.       var appName = Extensions.AppName;
  22.       Console.WriteLine($"{appName}({Extensions.AppVersion})");
  23.       Console.WriteLine("\t Recording a sample MDF recording file containing:");
  24.       Console.WriteLine("\t - All measurements from the specified A2L file");
  25.       Console.WriteLine("\t - A 4k raw data sample block");
  26.       Console.WriteLine($"Usage: {appName} A2LFile.a2l");
  27.       return -1;
  28.     }
  29.  
  30.     try
  31.     {
  32.       // create parser
  33.       using var a2lParser = new A2LParser();
  34.       // parse specified A2L file
  35.       a2lParser.parse(args[0]);
  36.  
  37.       // get all measurements from the A2L file
  38.       var measurements = a2lParser.Project.getNode<A2LMODULE>(false).getNodeList<A2LMEASUREMENT>(false);
  39.  
  40.       // expand measurement arrays
  41.       var measurementList = A2LMEASUREMENT.buildDAQMeasurements(measurements);
  42.  
  43.       // Create the MDF writer
  44.       using var writer = new MDFWriter("ASAP2UnitTest", "jnsoft", "test", "subject", "header comment");
  45.       // add all measurements to record in the MDF file
  46.       writer.addMeasurements(0, measurementList);
  47.  
  48.       // add a raw data block
  49.       var rawData = new byte[0x1000];
  50.       var rawDataHandle = writer.addRawData("Raw data", (uint)rawData.Length, "RAW_DATA_BLOCK", "Raw data block");
  51.  
  52.       // Record start of annotation
  53.       writer.addAnnotation(0, "Start of recording");
  54.  
  55.       // Create sufficient sample record data
  56.       var data = new byte[writer[0].getRecordSize() - 8];
  57.  
  58.       // Write 10 sample records
  59.       for (int i = 0; i < 10; ++i)
  60.       {
  61.         // add measurement samples
  62.         writer.addDataEntry(0, i + 1, data);
  63.  
  64.         // add raw data sample
  65.         writer.addDataEntry(rawDataHandle, i + 1, rawData);
  66.       }
  67.  
  68.       // Record end of annotation
  69.       writer.addAnnotation(10, "End of recording");
  70.  
  71.       // Save the MDF file
  72.       System.Diagnostics.Debug.Assert(writer.save($"test.{(writer.Type == MDFType.V4 ? "mf4" : "mdf")}"));
  73.       return 0;
  74.     }
  75.     catch (Exception ex)
  76.     {
  77.       Console.WriteLine($"Something failed\n{ex.Message}");
  78.       return -2;
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment