alesi2000

ASAP2AdjustDataExample

Apr 13th, 2012 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 KB | None | 0 0
  1. using jnsoft.Helpers;
  2. using System.Globalization;
  3.  
  4. namespace jnsoft.ASAP2.Values.Examples;
  5.  
  6. /// <summary>
  7. /// Demo console application.
  8. ///
  9. /// - Loading an A2L and it's corresponding datafile (.hex or .s19)
  10. /// - Do an EPK check on the datafile
  11. /// - Iterate over any characteristics and print and modify out values
  12. /// - save the changed data file
  13. ///
  14. /// Usage: ASAP2AdjustDataExample A2LFile.a2l DataFile.hex|.s19
  15. /// Prerequisites: A valid A2L file, and a corresponding data file (hex/s19)
  16. /// </summary>
  17. class Program
  18. {
  19.   static void Main(string[] args)
  20.   {
  21.     if (args.Length == 0)
  22.     { // no args -> present usage
  23.       var appName = Extensions.AppName;
  24.       Console.WriteLine($"{appName}({Extensions.AppVersion})");
  25.       Console.WriteLine("\t Adjust datafile demo");
  26.       Console.WriteLine($"Usage: {appName} A2LFile.a2l datafile.hex|datafile.s19");
  27.       return;
  28.     }
  29.     var prevColor = Console.ForegroundColor;
  30.     try
  31.     {
  32.       using var a2lParser = new A2LParser();
  33.       // parse specified A2L file
  34.       if (!a2lParser.parse(args[0]))
  35.         // not an a2l file
  36.         return;
  37.  
  38.       // do further checks
  39.       var project = a2lParser.Project;
  40.       var module = project.getNode<A2LMODULE>(true);
  41.       var initialSegments = module.createInitialMemorySegments(false);
  42.  
  43.       // Load the data file
  44.       var dataFile = DataFile.open(args[1], initialSegments);
  45.  
  46.       // Do the EPK check on the file
  47.       var modPar = module.getNode<A2LMOD_PAR>(true);
  48.       if (modPar != null
  49.         && !string.IsNullOrEmpty(modPar.EPK)
  50.         && !dataFile.checkEPK(modPar.EPKAddress, modPar.EPK))
  51.         // epk check failed
  52.         throw new ArgumentException("EPK check failed");
  53.  
  54.       // Print and modify characteristic's
  55.       foreach (var recLayoutRef in module.enumChildNodes<A2LRECORD_LAYOUT_REF>())
  56.       {
  57.         // create the characteristic's value
  58.         var value = CreateValue(dataFile, recLayoutRef);
  59.         if (value == null)
  60.         {
  61.           Console.ForegroundColor = ConsoleColor.Red;
  62.           Console.WriteLine($"Segment not found for {recLayoutRef.Name}");
  63.           continue;
  64.         }
  65.  
  66.         switch (recLayoutRef)
  67.         {
  68.           case A2LAXIS_PTS axisPts:
  69.             // Just for documentation, usually AxisPts are referenced
  70.             // by CURVE/MAP/CUBOIDs and are implicitly accessed and modified by them
  71.             Console.ForegroundColor = ConsoleColor.White;
  72.             Console.WriteLine($"AxisPts {axisPts.Name}=\n{value}");
  73.             // Access values
  74.             var vFncValues = (double[])value.Value;
  75.             break;
  76.  
  77.           case A2LCHARACTERISTIC characteristic:
  78.             switch (characteristic.CharType)
  79.             {
  80.               case CHAR_TYPE.VALUE:
  81.                 Console.ForegroundColor = ConsoleColor.Green;
  82.                 Console.WriteLine(string.Format(CultureInfo.InvariantCulture
  83.                   , $"{{0}};{{1:f{value.DecimalCount}}};{{2}}"
  84.                   , characteristic.Name
  85.                   , (double)value.Value
  86.                   , value.Unit));
  87.  
  88.                 // Set a random value as Function value
  89.                 value.Value = CreateRandomValue(characteristic);
  90.                 continue;
  91.  
  92.               case CHAR_TYPE.ASCII:
  93.                 Console.ForegroundColor = ConsoleColor.Gray;
  94.                 Console.WriteLine($"ASCII Value {characteristic.Name}={value.toSingleValue()}");
  95.                 continue;
  96.  
  97.               case CHAR_TYPE.VAL_BLK:
  98.                 Console.ForegroundColor = ConsoleColor.White;
  99.                 Console.WriteLine($"ValBlk {characteristic.Name}=\n{value}");
  100.  
  101.                 // Set random values into ValBlks's function values
  102.                 vFncValues = (double[])value.Value;
  103.                 for (int x = 0; x < vFncValues.GetLength(0); ++x)
  104.                   vFncValues[x] = CreateRandomValue(characteristic);
  105.                 break;
  106.  
  107.               case CHAR_TYPE.CURVE:
  108.                 Console.ForegroundColor = ConsoleColor.Yellow;
  109.                 Console.WriteLine($"Curve {characteristic.Name}=\n{value}");
  110.  
  111.                 // Set random values into curve's function values
  112.                 var cFncValues = (double[])value.Value;
  113.                 for (int x = 0; x < cFncValues.GetLength(0); ++x)
  114.                   cFncValues[x] = CreateRandomValue(characteristic);
  115.  
  116.                 // Move the X-Axis values and increment values by 1
  117.                 var axisIdx = 0;
  118.                 for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
  119.                   value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] + 1;
  120.                 break;
  121.  
  122.               case CHAR_TYPE.MAP:
  123.                 Console.ForegroundColor = ConsoleColor.Cyan;
  124.                 Console.WriteLine($"Map {characteristic.Name}=\n{value}");
  125.  
  126.                 // Set random values into map's function values
  127.                 var mFncValues = (double[,])value.Value;
  128.                 for (int y = 0; y < mFncValues.GetLength(1); ++y)
  129.                   for (int x = 0; x < mFncValues.GetLength(0); ++x)
  130.                     mFncValues[x, y] = CreateRandomValue(characteristic);
  131.  
  132.                 // Move the X-Axis values and increment values by 1
  133.                 axisIdx = 0;
  134.                 for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
  135.                   value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] + 1;
  136.  
  137.                 // Move the Y-Axis values and decrement values by 1
  138.                 axisIdx++;
  139.                 for (int i = 0; i < value.AxisValue[axisIdx].Length; ++i)
  140.                   value.AxisValue[axisIdx][i] = value.AxisValue[axisIdx][i] - 1;
  141.                 break;
  142.             }
  143.  
  144.             // Write the changed characteristic back into the datafile
  145.             CharacteristicValue.setValue(dataFile, value);
  146.             break;
  147.         }
  148.       }
  149.  
  150.       Console.WriteLine($"{Path.GetFileName(args[1])}={(dataFile.IsDirty ? "dirty" : "unchanged")}");
  151.       if (dataFile.IsDirty)
  152.       { // save the changed datafile
  153.         var destFile = "test" + Path.GetExtension(args[1]);
  154.         bool saved = dataFile.save(destFile, true);
  155.         Console.WriteLine($"{(saved ? "successfully saved" : "failed to save")} to {destFile}");
  156.       }
  157.     }
  158.     catch (Exception ex) { Console.WriteLine($"Something failed {ex.Message}"); }
  159.     finally { Console.ForegroundColor = prevColor; }
  160.   }
  161.  
  162.   /// <summary>
  163.   /// Creates a characteristic's physical value from the given dataFile.
  164.   /// </summary>
  165.   /// <param name="dataFile">The datafile to read from</param>
  166.   /// <param name="characteristic"></param>
  167.   /// <returns></returns>
  168.   static ICharValue CreateValue(IDataFile dataFile, A2LRECORD_LAYOUT_REF characteristic)
  169.   {
  170.     return CharacteristicValue.createValue(dataFile, characteristic, ValueObjectFormat.Physical);
  171.   }
  172.  
  173.   /// <summary>
  174.   /// Creates random values for a specific characteristic (between lower and upper limit).
  175.   /// </summary>
  176.   /// <param name="characteristic"></param>
  177.   static double CreateRandomValue(A2LCHARACTERISTIC characteristic)
  178.   {
  179.     return new Random().NextDouble()
  180.       * Math.Abs(characteristic.UpperLimit - characteristic.LowerLimit)
  181.       + characteristic.LowerLimit;
  182.   }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment