Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.Helpers;
- using System;
- using System.Globalization;
- using System.IO;
- namespace jnsoft.ASAP2.Values.Examples
- {
- /// <summary>
- /// Demo console application.
- ///
- /// - Loading an A2L and it's corresponding datafile (.hex or .s19)
- /// - Do an EPK check on the datafile
- /// - Iterate over any characteristics and print and modify out values
- /// - save the changed data file
- ///
- /// Usage: ASAP2AdjustDataExample A2LFile.a2l DataFile.hex|.s19
- /// Prerequisites: A valid A2L file, and a corresponding data file (hex/s19)
- /// </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 Adjust datafile demo");
- Console.WriteLine($"Usage: {appName} A2LFile.a2l datafile.hex|datafile.s19");
- return;
- }
- var prevColor = Console.ForegroundColor;
- try
- {
- using (var a2lParser = new A2LParser())
- {
- // parse specified A2L file
- if (!a2lParser.parse(args[0]))
- // not an a2l file
- return;
- // do further checks
- var project = a2lParser.Project;
- var module = project.getNode<A2LMODULE>(true);
- var initialSegments = module.createInitialMemorySegments(false);
- // Load the data file
- var dataFile = DataFile.open(args[1], initialSegments);
- // Do the EPK check on the file
- var modPar = module.getNode<A2LMOD_PAR>(true);
- if (modPar != null && !string.IsNullOrEmpty(modPar.EPK) && !dataFile.checkEPK(modPar.EPKAddress, modPar.EPK))
- // epk check failed
- throw new ArgumentException("EPK check failed");
- // Print and modify characteristic's
- foreach (var recLayoutRef in module.enumChildNodes<A2LRECORD_LAYOUT_REF>())
- {
- // create the characteristic's value
- var value = CreateValue(dataFile, recLayoutRef);
- if (value == null)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Segment not found for {recLayoutRef.Name}");
- continue;
- }
- switch (recLayoutRef)
- {
- case A2LAXIS_PTS axisPts:
- // Just for documentation, usually AxisPts are referenced
- // by CURVE/MAP/CUBOIDs and are implicitly accessed and modified by them
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"AxisPts {axisPts.Name}=\n{value}");
- // Access values
- var vFncValues = (double[])value.Value;
- break;
- case A2LCHARACTERISTIC characteristic:
- switch (characteristic.CharType)
- {
- case CHAR_TYPE.VALUE:
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine(string.Format(CultureInfo.InvariantCulture
- , $"{{0}};{{1:f{value.DecimalCount}}};{{2}}"
- , characteristic.Name
- , (double)value.Value
- , value.Unit
- ));
- // Set a random value as Function value
- value.Value = CreateRandomValue(characteristic);
- continue;
- case CHAR_TYPE.ASCII:
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.WriteLine($"ASCII Value {characteristic.Name}={value.toSingleValue()}");
- continue;
- case CHAR_TYPE.VAL_BLK:
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"ValBlk {characteristic.Name}=\n{value}");
- // Set random values into ValBlks's function values
- vFncValues = (double[])value.Value;
- for (int x = 0; x < vFncValues.GetLength(0); ++x)
- vFncValues[x] = CreateRandomValue(characteristic);
- break;
- case CHAR_TYPE.CURVE:
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"Curve {characteristic.Name}=\n{value}");
- // Set random values into curve's function values
- var cFncValues = (double[])value.Value;
- for (int x = 0; x < cFncValues.GetLength(0); ++x)
- cFncValues[x] = CreateRandomValue(characteristic);
- // Move the X-Axis values and increment values by 1
- var axisIdx = 0;
- for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
- value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] + 1;
- break;
- case CHAR_TYPE.MAP:
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine($"Map {characteristic.Name}=\n{value}");
- // Set random values into map's function values
- var mFncValues = (double[,])value.Value;
- for (int y = 0; y < mFncValues.GetLength(1); ++y)
- for (int x = 0; x < mFncValues.GetLength(0); ++x)
- mFncValues[x, y] = CreateRandomValue(characteristic);
- // Move the X-Axis values and increment values by 1
- axisIdx = 0;
- for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
- value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] + 1;
- // Move the Y-Axis values and decrement values by 1
- axisIdx++;
- for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
- value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] - 1;
- break;
- }
- // Write the changed characteristic back into the datafile
- CharacteristicValue.setValue(dataFile, value);
- break;
- }
- }
- Console.WriteLine($"{Path.GetFileName(args[1])}={(dataFile.IsDirty ? "dirty" : "unchanged")}");
- if (dataFile.IsDirty)
- { // save the changed datafile
- var destFile = "test" + Path.GetExtension(args[1]);
- bool saved = dataFile.save(destFile, true);
- Console.WriteLine($"{(saved ? "successfully saved" : "failed to save")} to {destFile}");
- }
- }
- }
- catch (Exception ex) { Console.WriteLine($"Something failed {ex.Message}"); }
- finally { Console.ForegroundColor = prevColor; }
- }
- /// <summary>
- /// Creates a characteristic's physical value from the given dataFile.
- /// </summary>
- /// <param name="dataFile">The datafile to read from</param>
- /// <param name="characteristic"></param>
- /// <returns></returns>
- static ICharValue CreateValue(IDataFile dataFile, A2LRECORD_LAYOUT_REF characteristic)
- {
- return CharacteristicValue.createValue(dataFile, characteristic, ValueObjectFormat.Physical);
- }
- /// <summary>
- /// Creates random values for a specific characteristic (between lower and upper limit).
- /// </summary>
- /// <param name="characteristic"></param>
- static double CreateRandomValue(A2LCHARACTERISTIC characteristic)
- {
- return new Random().NextDouble()
- * Math.Abs(characteristic.UpperLimit - characteristic.LowerLimit)
- + characteristic.LowerLimit;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement