Advertisement
alesi2000

ASAP2WriterExample

Feb 28th, 2021 (edited)
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. using jnsoft.Helpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6.  
  7. namespace jnsoft.ASAP2.Examples
  8. {
  9.   /// <summary>
  10.   /// Sample console application.
  11.   ///
  12.   /// Demonstrates modifying the A2L object model
  13.   /// and rewriting it into a new A2L file.
  14.   ///
  15.   /// Usage: ASAP2WriterExample A2LFile.a2l
  16.   ///
  17.   /// - Delete the M"speed_by_formula" from the object model
  18.   /// - Adds the Function _MyFunction
  19.   /// - Adds the CompuMethod _MyCompuMethod
  20.   /// - Adds the Measurement _MyMeasurement
  21.   /// - Adds the Characteristic _MyCharacteristic
  22.   /// - Adds the new measurment and characteristic to the new function
  23.   /// - Rewrites the resulting A2L to a new filename.out.a2l
  24.   /// Does an csv output to stdout, watch result by
  25.   /// opening the test.csv with excel or something.
  26.   /// </summary>
  27.   class Program
  28.   {
  29.     static void Main(string[] args)
  30.     {
  31.       if (args.Length == 0)
  32.       { // no args -> present usage
  33.         var appName = Extensions.AppName;
  34.         Console.WriteLine($"{appName}({Extensions.AppVersion})");
  35.         Console.WriteLine("\t Parse the specified A2L file");
  36.         Console.WriteLine("\t modify and rewrite it.");
  37.         Console.WriteLine($"Usage: {appName} A2LFile.a2l");
  38.         return;
  39.       }
  40.  
  41.       var file = args[0];
  42.       if (!File.Exists(file))
  43.       { // file does not exist
  44.         Console.WriteLine($"File '{file}' does not exist!");
  45.         return;
  46.       }
  47.  
  48.       var prevColor = Console.ForegroundColor;
  49.       try
  50.       { // parse specified A2L file
  51.         var a2lParser = new A2LParser();
  52.         a2lParser.parse(file);
  53.  
  54.         var module = a2lParser.Project.getNode<A2LMODULE>(false);
  55.  
  56.         // delete a measurement from the object model
  57.         var measurement = a2lParser.Project.MeasDict["speed_by_formula"];
  58.         if (measurement != null)
  59.           Debug.Assert(A2LNODE.deleteFromModel(measurement));
  60.  
  61.         // build a function
  62.         A2LREFERENCE functionChar, functionMeas;
  63.         const string FunctionName = "_MyFunction";
  64.         var function = new A2LFUNCTION { Name = FunctionName, Description = "Created Function", };
  65.         function.addChild(functionMeas = new A2LOUT_MEASUREMENT { References = new List<string>() });
  66.         function.addChild(functionChar = new A2LDEF_CHARACTERISTIC { References = new List<string>() });
  67.         module.addChild(function);
  68.  
  69.         // build a measurement
  70.  
  71.         // build a referenced CompuMethod
  72.         const string CompuMethodName = "_MyCompuMethod";
  73.         var compuMethod = new A2LCOMPU_METHOD
  74.         {
  75.           Name = CompuMethodName,
  76.           Description = "Created 1:1 CompuMethod",
  77.           Unit = "km/h",
  78.           Format = "%8.3",
  79.           ConversionType = CONVERSION_TYPE.RAT_FUNC,
  80.           Coeffs = new Helpers.RationalCoeffs { a = 0, b = 1, c = 0, d = 0, e = 0, f = 1 }
  81.         };
  82.         module.addChild(compuMethod);
  83.  
  84.         const string MeasurementName = "_MyMeasurement";
  85.         measurement = new A2LMEASUREMENT
  86.         {
  87.           Name = MeasurementName,
  88.           Description = "Created Measurement",
  89.           DataType = DATA_TYPE.UBYTE,
  90.           LowerLimit = 0,
  91.           UpperLimit = byte.MaxValue,
  92.           Conversion = CompuMethodName,
  93.           Address = 0x400,
  94.         };
  95.         module.addChild(measurement);
  96.  
  97.         // add measurement to function
  98.         functionMeas.References.Add(MeasurementName);
  99.  
  100.         // build a characteristic (map)
  101.  
  102.         // build a referenced record layout for a MAP containing 2 standard axis
  103.         const string LayoutName = "_MyRecordLayout";
  104.         var layout = new A2LRECORD_LAYOUT
  105.         {
  106.           Name = LayoutName,
  107.           FncValues = new A2LRECORD_LAYOUT.FncValuesLayoutDesc(1, DATA_TYPE.UBYTE, INDEX_MODE.COLUMN_DIR, ADDR_TYPE.DIRECT),
  108.         };
  109.         layout.AxisPts[0] = new A2LRECORD_LAYOUT.AxisPtsLayoutDesc("AXIS_PTS_X", 2, DATA_TYPE.UBYTE, INDEX_ORDER.INDEX_INCR, ADDR_TYPE.DIRECT, 0);
  110.         layout.AxisPts[1] = new A2LRECORD_LAYOUT.AxisPtsLayoutDesc("AXIS_PTS_Y", 3, DATA_TYPE.UBYTE, INDEX_ORDER.INDEX_INCR, ADDR_TYPE.DIRECT, 0);
  111.         module.addChild(layout);
  112.  
  113.         const string CharacteristicName = "_MyCharacteristicMap";
  114.         var characteristic = new A2LCHARACTERISTIC
  115.         {
  116.           Name = CharacteristicName,
  117.           Description = "Created 10x5 Characteristic Map",
  118.           CharType = CHAR_TYPE.MAP,
  119.           Conversion = CompuMethodName,
  120.           LowerLimit = 0,
  121.           LowerLimitEx = 0,
  122.           UpperLimit = byte.MaxValue,
  123.           UpperLimitEx = byte.MaxValue,
  124.           RecordLayout = LayoutName,
  125.           Address = 0x410,
  126.         };
  127.  
  128.         // build referenced axis descriptions for a 10x5 MAP
  129.         for (int i = 0; i < 2; ++i)
  130.           characteristic.addChild(new A2LAXIS_DESCR
  131.           {
  132.             AxisType = AXIS_TYPE.STD_AXIS,
  133.             LowerLimit = 0,
  134.             UpperLimit = byte.MaxValue,
  135.             Conversion = CompuMethodName,
  136.             InputQuantity = MeasurementName,  // use the created measurement as axis input
  137.             MaxAxisPoints = i == 0 ? 10 : 5,
  138.           });
  139.         module.addChild(characteristic);
  140.  
  141.         // add charcteristic to function
  142.         functionChar.References.Add(CharacteristicName);
  143.  
  144.         // store the modified A2L file
  145.         a2lParser.write(Path.GetFileNameWithoutExtension(file) + ".out" + Path.GetExtension(file));
  146.       }
  147.       catch (Exception ex)
  148.       { // should not occur...
  149.         Console.ForegroundColor = ConsoleColor.Red;
  150.         Console.WriteLine("File '{0}' seems not to be an A2L file, caught exception:\n{1}\nStacktrace\n{2}"
  151.           , file, ex.Message, ex.StackTrace
  152.           );
  153.       }
  154.       finally
  155.       { // reset starting color
  156.         Console.ForegroundColor = prevColor;
  157.       }
  158.     }
  159.   }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement