alesi2000

ASAP2ODXExample

Jan 6th, 2018 (edited)
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using jnsoft.Helpers;
  2.  
  3. namespace jnsoft.Diagnose.ODX.Examples;
  4.  
  5. /// <summary>
  6. /// Sample console application.
  7. ///
  8. /// Demonstrates parsing, changing, (re)writing an ODX file and
  9. /// printing some information from the object model.
  10. ///
  11. /// Usage: ASAP2ODXExample ODXFile.odx
  12. /// </summary>
  13. class Program
  14. {
  15.   static void Main(string[] args)
  16.   {
  17.     if (args.Length == 0)
  18.     { // no args -> present usage
  19.       var appName = Extensions.AppName;
  20.       Console.WriteLine($"{appName}({Extensions.AppVersion})");
  21.       Console.WriteLine("\t Parse the specified ODX file");
  22.       Console.WriteLine("\t and do some example output of ODX contents");
  23.       Console.WriteLine($"Usage: {appName} ODXFile.odx");
  24.       return;
  25.     }
  26.  
  27.     if (!File.Exists(args[0]))
  28.     { // file does not exist
  29.       Console.WriteLine($"File '{args[0]}' does not exist!");
  30.       return;
  31.     }
  32.  
  33.     var prevColor = Console.ForegroundColor;
  34.     try
  35.     {
  36.       var odxFile = ODXFile.open(args[0]);
  37.  
  38.       var dlc = odxFile.ODX.DIAG_LAYER_CONTAINER;
  39.       if (null != dlc)
  40.       {
  41.         Console.ForegroundColor = ConsoleColor.Green;
  42.  
  43.         var DTCs = odxFile.ODX.getDTCs();
  44.  
  45.         Console.WriteLine($"ODX model version: {odxFile.ODX.MODEL_VERSION}, defined trouble codes: {DTCs.Count}");
  46.  
  47.         if (dlc.PROTOCOLS != null)
  48.           foreach (var protocol in dlc.PROTOCOLS)
  49.             Console.WriteLine($"Defined protocol: {protocol} ({protocol.TYPE}, Baudrate: {(int)protocol.Baudrate / 1000} kBit)");
  50.  
  51.         if (dlc.BASE_VARIANTS != null)
  52.           foreach (var bv in dlc.BASE_VARIANTS)
  53.             Console.WriteLine($"ECU Base variant: {bv}(Supported services: {bv.FunctClasses.Count})");
  54.  
  55.         if (dlc.ECU_VARIANTS != null)
  56.           foreach (var ecu in dlc.ECU_VARIANTS)
  57.             Console.WriteLine($"\tECU variant: {ecu} (form Base Variant: {ecu.Parent}, Defined services: {ecu.DiagServices.Count})");
  58.       }
  59.       else
  60.         Console.WriteLine("ODX parsed successfully, but no diag layer container defined...");
  61.  
  62.       // change something in the ODX model
  63.       odxFile.ODX.MODEL_VERSION = "2.0.2";
  64.  
  65.       // Save the file
  66.       odxFile.save(Path.ChangeExtension(odxFile.SourceFile, ".out.odx"));
  67.       Console.WriteLine($"Successfully saved the file as {Path.GetFileName(odxFile.SourceFile)}");
  68.     }
  69.     catch (Exception ex)
  70.     {
  71.       Console.ForegroundColor = ConsoleColor.Red;
  72.       Console.WriteLine("File '{0}' seems not to be an ODX file, caught exception:\n{1}\nStacktrace\n{2}"
  73.         , args[0], ex.Message, ex.StackTrace);
  74.     }
  75.     finally
  76.     {
  77.       Console.ForegroundColor = prevColor;
  78.     }
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment