Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.ASAP2;
- using jnsoft.Comm;
- using jnsoft.Comm.XCP;
- using System.Text;
- namespace MAUITestApp
- {
- /// <summary>
- /// MAUI Test application using the ASAP2Library.
- ///
- /// Connects to an ECU over XCPonUDP (192.168.0.2:21000)
- /// and reads measurements using DAQ.
- ///
- /// Test e.g. with the ECUSimulator started like
- /// ECUSimulator -debug udp:21000 ..\..\..\..\A2LExamples\ASAP2Example.a2l ..\..\..\..\A2LExamples\ASAP2Example.hex
- /// </summary>
- public partial class MainPage : ContentPage
- {
- const string ConnectTo = "192.168.0.2";
- const ushort Port = 21000;
- readonly A2LParser parser;
- readonly A2LPROJECT project;
- readonly A2LMODULE module;
- readonly XCPMaster xcpMaster;
- readonly IEnumerable<DAQMeasurement> measurements;
- long lastUpdate;
- public MainPage()
- {
- InitializeComponent();
- BindingContext = this;
- parser = new A2LParser();
- using var resStream = FileSystem.OpenAppPackageFileAsync("ASAP2Example.a2l").Result;
- parser.parse(resStream);
- project = parser.Project;
- module = project.getNode<A2LMODULE>(false);
- measurements = project.MeasDict.Values
- .Where(x => x.getArraySize() == 1) // restrict to single values
- .Select(x => new DAQMeasurement(x));
- var xcpMedia = module.getXCPInterfaces().First(x => x.Type == A2LType.XCP_ON_UDP_IP);
- xcpMedia.getSettings(out var protocol, out var Checksum, out var PAG, out var DAQ, out var PGM);
- xcpMaster = new XCPMaster(ConnectBehaviourType.Manual, XCPType.UDP, ConnectTo, Port, protocol, DAQ);
- // track received data
- xcpMaster.OnValuesReceived += (o, e) =>
- {
- var ticks = Environment.TickCount64;
- if (ticks - lastUpdate > 1000)
- {
- lastUpdate = ticks;
- var sb = new StringBuilder();
- foreach (var odtEntry in xcpMaster.DAQs.ODTEntries.SelectMany(x => x.Value))
- {
- var measurement = odtEntry.Measurement;
- xcpMaster.getDAQPhysValue(measurement, out var measValue);
- sb.AppendLine($"{measurement} = {measurement.toStringValue(measValue)} {odtEntry.Measurement.Unit}");
- }
- Values = sb.ToString();
- }
- };
- }
- protected override void OnDisappearing()
- {
- xcpMaster.Disconnect();
- base.OnDisappearing();
- }
- void TestBtn_Clicked(object sender, EventArgs e)
- {
- if (!xcpMaster.Connected)
- {
- State = "Starting test...";
- if (CmdResult.OK != xcpMaster.Connect(ConnectMode.Normal, out var response))
- {
- State += "\nFailed to connect...";
- return;
- }
- if ((response.Resource & ResourceType.DAQ) == 0)
- {
- State += "\nDAQ resource not available!";
- xcpMaster.Disconnect();
- return;
- }
- xcpMaster.epkCheck(module.RefModPar, out var epk);
- State += $"\nECU EPK = {epk}";
- State += $"\nStarted receiving {measurements.Count()} measurements.";
- xcpMaster.configureMeasurements(measurements.ToList());
- xcpMaster.startMeasurements(true);
- IsTestEnabled = !(IsStopEnabled = true);
- }
- }
- void StopBtn_Clicked(object sender, EventArgs e)
- {
- xcpMaster.stopMeasurements(true);
- xcpMaster.Disconnect();
- State = "\nTest stopped.";
- IsStopEnabled = !(IsTestEnabled = true);
- }
- #region properties
- string _state = string.Empty;
- public string State
- {
- get => _state;
- set
- {
- _state = value;
- OnPropertyChanged(nameof(State));
- }
- }
- string _values = string.Empty;
- public string Values
- {
- get => _values;
- set
- {
- _values = value;
- OnPropertyChanged(nameof(Values));
- }
- }
- bool _isTestEnbaled = true;
- public bool IsTestEnabled
- {
- get => _isTestEnbaled;
- set
- {
- _isTestEnbaled = value;
- OnPropertyChanged(nameof(IsTestEnabled));
- }
- }
- bool _isStopEnbaled;
- public bool IsStopEnabled
- {
- get => _isStopEnbaled;
- set
- {
- _isStopEnbaled = value;
- OnPropertyChanged(nameof(IsStopEnabled));
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement