alesi2000

ASAP2ParserExample

Apr 13th, 2012 (edited)
4,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.70 KB | None | 0 0
  1. using jnsoft.Helpers;
  2.  
  3. namespace jnsoft.ASAP2.Examples;
  4.  
  5. /// <summary>
  6. /// Sample console application.
  7. ///
  8. /// Demonstrating a simple csv output of
  9. /// some ASAP2 nodes from the specified A2L file content.
  10. ///
  11. /// Usage: ASAP2ParserExample A2LFile.a2l > test.csv
  12. /// Does an csv output to stdout, watch result by
  13. /// opening the test.csv with excel or something.
  14. /// </summary>
  15. class Program
  16. {
  17.   static void Main(string[] args)
  18.   {
  19.     if (args.Length == 0)
  20.     { // no args -> present usage
  21.       var appName = Extensions.AppName;
  22.       Console.WriteLine($"{appName}({Extensions.AppVersion})");
  23.       Console.WriteLine("\t Parse the specified A2L file");
  24.       Console.WriteLine("\t and do some example output of A2L contents");
  25.       Console.WriteLine($"Usage: {appName} A2LFile.a2l");
  26.       return;
  27.     }
  28.  
  29.     if (!File.Exists(args[0]))
  30.     { // file does not exist
  31.       Console.WriteLine($"File '{args[0]}' does not exist!");
  32.       return;
  33.     }
  34.  
  35.     var prevColor = Console.ForegroundColor;
  36.     try
  37.     { // parse specified A2L file
  38.       // create parser and register parser events
  39.       using var a2lParser = new A2LParser();
  40.       a2lParser.ParserMessage += onParserMessage;
  41.       a2lParser.ProgressChanged += onProgressChanged;
  42.       if (!a2lParser.parse(args[0]))
  43.       { // parsing failed
  44.         Console.WriteLine($"File '{args[0]}' seems not to be an A2L file");
  45.         return;
  46.       }
  47.  
  48.       // Output the system constants
  49.       Console.ForegroundColor = ConsoleColor.Cyan;
  50.       var modpar = a2lParser.Project.getNode<A2LMODULE>(false).getNode<A2LMOD_PAR>(false);
  51.       if (modpar?.SystemConstants.Count > 0)
  52.       { // system contstans available
  53.         Console.WriteLine("\n\nSYSTEM_CONSTANTs...");
  54.         Console.WriteLine("SYSTEM_CONSTANT;Formula;Value");
  55.  
  56.         foreach(var kvp in modpar.SystemConstants)
  57.         { // iterate through defined constants
  58.           a2lParser.Project.FormulaDict.TryGetValue(kvp.Key, out var formula);
  59.           Console.WriteLine("{0};{1};{2}"
  60.             , kvp.Key, kvp.Value
  61.             , formula?.toPhysical(0).ToString() ?? "not defined");
  62.         }
  63.       }
  64.  
  65.       // Output the measurements
  66.       Console.WriteLine("\n\nMEASUREMENTs...");
  67.       Console.WriteLine("Measurment;DataType;Address;Min;Max;Description");
  68.       foreach (var measurement in a2lParser.Project.enumChildNodes<A2LMEASUREMENT>())
  69.         Console.WriteLine("{0};{1};{2};{3};{4};{5}"
  70.           , measurement.Name, measurement.DataType
  71.           , $"0x{measurement.Address.ToString("X8")}"
  72.           , measurement.LowerLimit, measurement.UpperLimit, measurement.Description);
  73.  
  74.       // Output Characteristics from project
  75.       Console.WriteLine("\n\nCHARACTERISTICs...");
  76.       Console.WriteLine("Characteristic;Type;Address;Min;Max;Description");
  77.       foreach (var characteristic in a2lParser.Project.enumChildNodes<A2LCHARACTERISTIC>())
  78.         Console.WriteLine("{0};{1};{2};{3};{4};{5}"
  79.           , characteristic.Name, characteristic.CharType
  80.           , $"0x{characteristic.Address.ToString("X8")}"
  81.           , characteristic.LowerLimit, characteristic.UpperLimit, characteristic.Description);
  82.  
  83.       // Output CompuMethods from dictionary
  84.       // use the pre calculated dictionary
  85.       Console.WriteLine("\n\nCOMPUMETHODs...");
  86.       Console.WriteLine("CompuMethod;Type;Description");
  87.       foreach (var compuMethod in a2lParser.Project.CompDict.Values)
  88.         Console.WriteLine("{0};{1};{2}", compuMethod.Name, compuMethod.ConversionType, compuMethod.Description);
  89.     }
  90.     catch (Exception ex)
  91.     { // should not occur...
  92.       Console.ForegroundColor = ConsoleColor.Red;
  93.       Console.WriteLine($"File '{args[0]}' seems not to be an A2L file, caught exception:\n{ex.Message}\nStacktrace\n{ex.StackTrace}");
  94.     }
  95.     finally
  96.     { // reset starting color
  97.       Console.ForegroundColor = prevColor;
  98.     }
  99.   }
  100.   /// <summary>
  101.   /// Handles parser progress events.
  102.   /// </summary>
  103.   /// <param name="sender"></param>
  104.   /// <param name="e"></param>
  105.   static void onProgressChanged(object sender, A2LParserProgressEventArgs e)
  106.   {
  107.     Console.ForegroundColor = ConsoleColor.Blue;
  108.     Console.WriteLine($"{e.LinesProcessed * 100 / e.LinesMax}% processed.");
  109.   }
  110.   /// <summary>
  111.   /// Handles parser message events.
  112.   /// </summary>
  113.   /// <param name="sender"></param>
  114.   /// <param name="e"></param>
  115.   static void onParserMessage(object sender, ParserEventArgs e)
  116.   {
  117.     Console.ForegroundColor = e.Type == MessageType.Error
  118.       ? ConsoleColor.Red
  119.       : e.Type == MessageType.Warning
  120.         ? ConsoleColor.Yellow
  121.         : ConsoleColor.White;
  122.     Console.WriteLine($"{e.Type}:\t{e.Message}({(e.Source as A2LNODE)?.Name ?? string.Empty})");
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment