Advertisement
alesi2000

ASAP2MergeDataExample

Nov 9th, 2013 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.IO;
  4. using jnsoft.ASAP2;
  5. using jnsoft.ASAP2.Values;
  6.  
  7. /// <summary>
  8. /// Merge data example.
  9. ///
  10. /// - Loading an A2L and it's corresponding datafile (HEX or S19)
  11. /// - Merging an INCA DCM file (.dcm)
  12. /// - Save the changed data file
  13. ///
  14. /// Usage: ASAP2MergeDataExample A2LFile.a2l DataFileIn.hex|.s19 ImportDataFile.DCM
  15. /// </summary>
  16. class Program
  17. {
  18.   /// <summary>
  19.   /// Main function.
  20.   /// </summary>
  21.   /// <param name="args">The commandline arguments</param>
  22.   /// <returns>Console application exitcode (0 if successful)</returns>
  23.   static int Main(string[] args)
  24.   {
  25.     if (args.Length != 3)
  26.     { // no args -> present usage
  27.       Console.Write($"{Application.ProductName}({Application.ProductVersion}) - ");
  28.       Console.WriteLine("Merge data from a DCM file into a HEX/S19 file\n");
  29.       Console.WriteLine("Required input: An A2L, a corresponding HEX/S19 file and a datafile (DCM) to merge");
  30.       Console.WriteLine($"Usage: {Application.ProductName} A2LFile.a2l datafile.hex|.s19 datafile.dcm");
  31.       Console.WriteLine($"Example: {Application.ProductName} ecu.a2l ecu.hex import.dcm");
  32.       return -1;
  33.     }
  34.  
  35.     try
  36.     { // parse specified A2L file
  37.       using (var a2lParser = new A2LParser())
  38.       {
  39.         if (!a2lParser.parse(args[0]))
  40.           // not an a2l file
  41.           return -2;
  42.  
  43.         // Get default memory
  44.         var project = a2lParser.Project;
  45.         var module = project.getNode<A2LMODULE>(true);
  46.         var initialSegments = module.createInitialMemorySegments(false);
  47.  
  48.         // Parse the data file (HEX or S19)
  49.         var dataFile = DataFile.open(args[1], initialSegments);
  50.  
  51.         // Parse the DCM file
  52.         var dcmFile = DCMFile.open(args[2], module);
  53.         if (dcmFile == null)
  54.           // failed to open or read the DCM file
  55.           return -3;
  56.  
  57.         // Importing the DCM file data into the hex file data
  58.         var outFilename = Path.GetFileNameWithoutExtension(args[1]) + ".out" + Path.GetExtension(args[1]);
  59.         dataFile.import(dcmFile.Values.ToArray());
  60.  
  61.         // save the result
  62.         if (!dataFile.save(outFilename, false))
  63.           // failed to save the resulting hex file
  64.           return -4;
  65.  
  66.         Console.WriteLine("{0} merged data from {1} with {2}, written to {3}"
  67.           , Application.ProductName
  68.           , Path.GetFileName(args[2])
  69.           , Path.GetFileName(args[1])
  70.           , Path.GetFileName(outFilename)
  71.           );
  72.         return 0;
  73.       }
  74.     }
  75.     catch (Exception ex)
  76.     {
  77.       Console.WriteLine($"Something failed {ex.Message}");
  78.       return -5;
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement