alesi2000

ASAP2MergeDataExample

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