Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- namespace jnsoft.ASAP2.Examples
- {
- /// <summary>
- /// Sample console application.
- ///
- /// Demonstrates modifying the A2L object model
- /// and rewriting it into a new A2L file.
- ///
- /// Usage: ASAP2WriterExample A2LFile.a2l
- ///
- /// - Delete the M"speed_by_formula" from the object model
- /// - Adds the Function _MyFunction
- /// - Adds the CompuMethod _MyCompuMethod
- /// - Adds the Measurement _MyMeasurement
- /// - Adds the Characteristic _MyCharacteristic
- /// - Adds the new measurment and characteristic to the new function
- /// - Rewrites the resulting A2L to a new filename.out.a2l
- /// Does an csv output to stdout, watch result by
- /// opening the test.csv with excel or something.
- /// </summary>
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length == 0)
- { // no args -> present usage
- var appName = Extensions.AppName;
- Console.WriteLine($"{appName}({Extensions.AppVersion})");
- Console.WriteLine("\t Parse the specified A2L file");
- Console.WriteLine("\t modify and rewrite it.");
- Console.WriteLine($"Usage: {appName} A2LFile.a2l");
- return;
- }
- var file = args[0];
- if (!File.Exists(file))
- { // file does not exist
- Console.WriteLine($"File '{file}' does not exist!");
- return;
- }
- var prevColor = Console.ForegroundColor;
- try
- { // parse specified A2L file
- var a2lParser = new A2LParser();
- a2lParser.parse(file);
- var module = a2lParser.Project.getNode<A2LMODULE>(false);
- // delete a measurement from the object model
- var measurement = a2lParser.Project.MeasDict["speed_by_formula"];
- if (measurement != null)
- Debug.Assert(A2LNODE.deleteFromModel(measurement));
- // build a function
- A2LREFERENCE functionChar, functionMeas;
- const string FunctionName = "_MyFunction";
- var function = new A2LFUNCTION { Name = FunctionName, Description = "Created Function", };
- function.addChild(functionMeas = new A2LOUT_MEASUREMENT { References = new List<string>() });
- function.addChild(functionChar = new A2LDEF_CHARACTERISTIC { References = new List<string>() });
- module.addChild(function);
- // build a measurement
- // build a referenced CompuMethod
- const string CompuMethodName = "_MyCompuMethod";
- var compuMethod = new A2LCOMPU_METHOD
- {
- Name = CompuMethodName,
- Description = "Created 1:1 CompuMethod",
- Unit = "km/h",
- Format = "%8.3",
- ConversionType = CONVERSION_TYPE.RAT_FUNC,
- Coeffs = new Helpers.RationalCoeffs { a = 0, b = 1, c = 0, d = 0, e = 0, f = 1 }
- };
- module.addChild(compuMethod);
- const string MeasurementName = "_MyMeasurement";
- measurement = new A2LMEASUREMENT
- {
- Name = MeasurementName,
- Description = "Created Measurement",
- DataType = DATA_TYPE.UBYTE,
- LowerLimit = 0,
- UpperLimit = byte.MaxValue,
- Conversion = CompuMethodName,
- Address = 0x400,
- };
- module.addChild(measurement);
- // add measurement to function
- functionMeas.References.Add(MeasurementName);
- // build a characteristic (map)
- // build a referenced record layout for a MAP containing 2 standard axis
- const string LayoutName = "_MyRecordLayout";
- var layout = new A2LRECORD_LAYOUT
- {
- Name = LayoutName,
- FncValues = new A2LRECORD_LAYOUT.FncValuesLayoutDesc(1, DATA_TYPE.UBYTE, INDEX_MODE.COLUMN_DIR, ADDR_TYPE.DIRECT),
- };
- layout.AxisPts[0] = new A2LRECORD_LAYOUT.AxisPtsLayoutDesc("AXIS_PTS_X", 2, DATA_TYPE.UBYTE, INDEX_ORDER.INDEX_INCR, ADDR_TYPE.DIRECT, 0);
- layout.AxisPts[1] = new A2LRECORD_LAYOUT.AxisPtsLayoutDesc("AXIS_PTS_Y", 3, DATA_TYPE.UBYTE, INDEX_ORDER.INDEX_INCR, ADDR_TYPE.DIRECT, 0);
- module.addChild(layout);
- const string CharacteristicName = "_MyCharacteristicMap";
- var characteristic = new A2LCHARACTERISTIC
- {
- Name = CharacteristicName,
- Description = "Created 10x5 Characteristic Map",
- CharType = CHAR_TYPE.MAP,
- Conversion = CompuMethodName,
- LowerLimit = 0,
- LowerLimitEx = 0,
- UpperLimit = byte.MaxValue,
- UpperLimitEx = byte.MaxValue,
- RecordLayout = LayoutName,
- Address = 0x410,
- };
- // build referenced axis descriptions for a 10x5 MAP
- for (int i = 0; i < 2; ++i)
- characteristic.addChild(new A2LAXIS_DESCR
- {
- AxisType = AXIS_TYPE.STD_AXIS,
- LowerLimit = 0,
- UpperLimit = byte.MaxValue,
- Conversion = CompuMethodName,
- InputQuantity = MeasurementName, // use the created measurement as axis input
- MaxAxisPoints = i == 0 ? 10 : 5,
- });
- module.addChild(characteristic);
- // add charcteristic to function
- functionChar.References.Add(CharacteristicName);
- // store the modified A2L file
- a2lParser.write(Path.GetFileNameWithoutExtension(file) + ".out" + Path.GetExtension(file));
- }
- catch (Exception ex)
- { // should not occur...
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("File '{0}' seems not to be an A2L file, caught exception:\n{1}\nStacktrace\n{2}"
- , file, ex.Message, ex.StackTrace
- );
- }
- finally
- { // reset starting color
- Console.ForegroundColor = prevColor;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement