alesi2000

ASAP2WriterExample

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