Advertisement
alesi2000

MAUITestApp

Dec 14th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using jnsoft.ASAP2;
  2. using jnsoft.Comm;
  3. using jnsoft.Comm.XCP;
  4. using System.Text;
  5.  
  6. namespace MAUITestApp
  7. {
  8.   /// <summary>
  9.   /// MAUI Test application using the ASAP2Library.
  10.   ///
  11.   /// Connects to an ECU over XCPonUDP (192.168.0.2:21000)
  12.   /// and reads measurements using DAQ.
  13.   ///
  14.   /// Test e.g. with the ECUSimulator started like
  15.   /// ECUSimulator -debug udp:21000 ..\..\..\..\A2LExamples\ASAP2Example.a2l ..\..\..\..\A2LExamples\ASAP2Example.hex
  16.   /// </summary>
  17.   public partial class MainPage : ContentPage
  18.   {
  19.     const string ConnectTo = "192.168.0.2";
  20.     const ushort Port = 21000;
  21.     readonly A2LParser parser;
  22.     readonly A2LPROJECT project;
  23.     readonly A2LMODULE module;
  24.     readonly XCPMaster xcpMaster;
  25.     readonly IEnumerable<DAQMeasurement> measurements;
  26.     long lastUpdate;
  27.  
  28.     public MainPage()
  29.     {
  30.       InitializeComponent();
  31.       BindingContext = this;
  32.  
  33.       parser = new A2LParser();
  34.       using var resStream = FileSystem.OpenAppPackageFileAsync("ASAP2Example.a2l").Result;
  35.       parser.parse(resStream);
  36.  
  37.       project = parser.Project;
  38.       module = project.getNode<A2LMODULE>(false);
  39.  
  40.       measurements = project.MeasDict.Values
  41.         .Where(x => x.getArraySize() == 1) // restrict to single values
  42.         .Select(x => new DAQMeasurement(x));
  43.        
  44.       var xcpMedia = module.getXCPInterfaces().First(x => x.Type == A2LType.XCP_ON_UDP_IP);
  45.       xcpMedia.getSettings(out var protocol, out var Checksum, out var PAG, out var DAQ, out var PGM);
  46.  
  47.       xcpMaster = new XCPMaster(ConnectBehaviourType.Manual, XCPType.UDP, ConnectTo, Port, protocol, DAQ);
  48.  
  49.       // track received data
  50.       xcpMaster.OnValuesReceived += (o, e) =>
  51.       {
  52.         var ticks = Environment.TickCount64;
  53.         if (ticks - lastUpdate > 1000)
  54.         {
  55.           lastUpdate = ticks;
  56.           var sb = new StringBuilder();
  57.           foreach (var odtEntry in xcpMaster.DAQs.ODTEntries.SelectMany(x => x.Value))
  58.           {
  59.             var measurement = odtEntry.Measurement;
  60.             xcpMaster.getDAQPhysValue(measurement, out var measValue);
  61.             sb.AppendLine($"{measurement} = {measurement.toStringValue(measValue)} {odtEntry.Measurement.Unit}");
  62.           }
  63.           Values = sb.ToString();
  64.         }
  65.       };
  66.     }
  67.  
  68.     protected override void OnDisappearing()
  69.     {
  70.       xcpMaster.Disconnect();
  71.       base.OnDisappearing();
  72.     }
  73.  
  74.     void TestBtn_Clicked(object sender, EventArgs e)
  75.     {
  76.       if (!xcpMaster.Connected)
  77.       {
  78.         State = "Starting test...";
  79.  
  80.         if (CmdResult.OK != xcpMaster.Connect(ConnectMode.Normal, out var response))
  81.         {
  82.           State += "\nFailed to connect...";
  83.           return;
  84.         }
  85.  
  86.         if ((response.Resource & ResourceType.DAQ) == 0)
  87.         {
  88.           State += "\nDAQ resource not available!";
  89.           xcpMaster.Disconnect();
  90.           return;
  91.         }
  92.  
  93.         xcpMaster.epkCheck(module.RefModPar, out var epk);
  94.         State += $"\nECU EPK = {epk}";
  95.         State += $"\nStarted receiving {measurements.Count()} measurements.";
  96.  
  97.         xcpMaster.configureMeasurements(measurements.ToList());
  98.         xcpMaster.startMeasurements(true);
  99.         IsTestEnabled = !(IsStopEnabled = true);
  100.       }
  101.     }
  102.  
  103.     void StopBtn_Clicked(object sender, EventArgs e)
  104.     {
  105.       xcpMaster.stopMeasurements(true);
  106.       xcpMaster.Disconnect();
  107.       State = "\nTest stopped.";
  108.       IsStopEnabled = !(IsTestEnabled = true);
  109.     }
  110.  
  111.     #region properties
  112.  
  113.     string _state = string.Empty;
  114.     public string State
  115.     {
  116.       get => _state;
  117.       set
  118.       {
  119.         _state = value;
  120.         OnPropertyChanged(nameof(State));
  121.       }
  122.     }
  123.  
  124.     string _values = string.Empty;
  125.     public string Values
  126.     {
  127.       get => _values;
  128.       set
  129.       {
  130.         _values = value;
  131.         OnPropertyChanged(nameof(Values));
  132.       }
  133.     }
  134.  
  135.     bool _isTestEnbaled = true;
  136.     public bool IsTestEnabled
  137.     {
  138.       get => _isTestEnbaled;
  139.       set
  140.       {
  141.         _isTestEnbaled = value;
  142.         OnPropertyChanged(nameof(IsTestEnabled));
  143.       }
  144.     }
  145.  
  146.     bool _isStopEnbaled;
  147.     public bool IsStopEnabled
  148.     {
  149.       get => _isStopEnbaled;
  150.       set
  151.       {
  152.         _isStopEnbaled = value;
  153.         OnPropertyChanged(nameof(IsStopEnabled));
  154.       }
  155.     }
  156.  
  157.     #endregion
  158.   }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement