Advertisement
alesi2000

ASAP2MDFWriterExample

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