Advertisement
alesi2000

ASAP2ODXExample

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