using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ASAM.HILAPI.Interfaces.Common.ValueContainer.General; using ASAM.HILAPI.Interfaces.MAPort; using ASAM.HILAPI.Implementation.Common.ValueContainer.General; using Vector.CANoe.ASAM.HILAPI; namespace HILAPI.Tester { interface ITestEnvironment { bool Wait(TimeSpan timeout); } public partial class MainForm : Form, ITestEnvironment { public MainForm() { InitializeComponent(); btnCancel.Enabled = false; bCancelled = false; hilAPI = new Vector.CANoe.ASAM.HILAPI.HILAPI(); } private void Run() { bCancelled = false; ///////////////////////// // Tool specific setup // ///////////////////////// // load FDX descriptions List descriptions = new List(); FDXDescription description = new FDXDescription(); //sn description.FileName = "..\\..\\..\\..\\SeatTestFDX.xml"; description.FileName = "SeatTestFDX.xml"; description.GroupProperties.Add(1, new FDXGroupProperty(1000, false)); description.GroupProperties.Add(2, new FDXGroupProperty(1000, true)); descriptions.Add(description); // initialize the HILAPI hilAPI.Initialize("10.49.55.24", 2809, descriptions); // start the HILAPI if ( hilAPI.Start(5000)) { lblRunning.Text = "Running"; // create an MAPort IMAPort maPort = hilAPI.CreateMAPort(); //////////////////////////////////// // Run the interchangeable script // //////////////////////////////////// RunSeatTest(maPort, this); lblRunning.Text = "Done!"; } else { lblRunning.Text = "Error!"; } // stop the HILAPI hilAPI.Stop(); } private bool RunSeatTest(IMAPort maPort, ITestEnvironment environment) { bool testDiagnostics = true; bool testLongitudinal = true; bool testHeight = true; IUintValue value; IUintValue value2; // 1.) change control variable to start diagnostic session value = (UintValue)maPort.Read("Test/SeatGetVariant"); ++value.Value; maPort.Write("Test/SeatGetVariant", value); environment.Wait(TimeSpan.FromMilliseconds(50)); // 2.) read ECU variant string variant = ReadStringValue(maPort, "Test/SeatVariant", TimeSpan.FromMilliseconds(500), environment); testDiagnostics = !string.IsNullOrEmpty(variant); if (!testDiagnostics) { return false; } // 3.) perform longitudinal test for both variants if ("Basis" == variant || "Comfort" == variant) { // forward maPort.Write("Test/SeatPosForward", new UintValue(1)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); if ( !WaitForValue(maPort, "Test/SeatLongitudinalUpperLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testLongitudinal = false; } // backward maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(1)); if ( !WaitForValue(maPort, "Test/SeatLongitudinalLowerLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testLongitudinal = false; } maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); // both value = (IUintValue)maPort.Read("Test/SeatLongitudinalPos"); maPort.Write("Test/SeatPosForward", new UintValue(1)); maPort.Write("Test/SeatPosBackward", new UintValue(1)); environment.Wait(TimeSpan.FromMilliseconds(2000)); value2 = (IUintValue)maPort.Read("Test/SeatLongitudinalPos"); if (value.Value != value2.Value) { testLongitudinal = false; } maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); } // 4.) perform height test for Comfort variant only if ("Comfort" == variant) { // up maPort.Write("Test/SeatPosUp", new UintValue(1)); maPort.Write("Test/SeatPosDown", new UintValue(0)); if ( !WaitForValue(maPort, "Test/SeatHeightUpperLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testHeight = false; } // down maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(1)); if ( !WaitForValue(maPort, "Test/SeatHeightLowerLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testHeight = false; } maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); // both value = (IUintValue)maPort.Read("Test/SeatHeightPos"); maPort.Write("Test/SeatPosUp", new UintValue(1)); maPort.Write("Test/SeatPosDown", new UintValue(1)); environment.Wait(TimeSpan.FromMilliseconds(2000)); value2 = (IUintValue)maPort.Read("Test/SeatHeightPos"); if (value.Value != value2.Value) { testHeight = false; } maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); } return testDiagnostics && testLongitudinal && testHeight; } bool WaitForValue(IMAPort maPort, string name, uint value, TimeSpan timeout, ITestEnvironment environment) { // read variable IUintValue currentValue = (IUintValue)maPort.Read(name); if (value == currentValue.Value) { return true; } // check timeout TimeSpan wait = TimeSpan.FromMilliseconds(10); if (timeout < wait) { return false; } // wait & read up to maximum timeout do { if (!environment.Wait(wait)) { break; } timeout -= wait; // read variable again currentValue = (IUintValue)maPort.Read(name); } while (value != currentValue.Value && timeout > TimeSpan.Zero); return value == currentValue.Value; } private string ReadStringValue(IMAPort maPort, string name, TimeSpan timeout, ITestEnvironment environment) { // read variable IStringValue variant = (IStringValue)maPort.Read(name); if (0 != variant.Value.Length) { // return if available return variant.Value; } // check timeout TimeSpan wait = TimeSpan.FromMilliseconds(10); if (timeout < wait) { return variant.Value; } // wait & read up to maximum timeout do { if (!environment.Wait(wait)) { break; } timeout -= wait; // read variable again variant = (IStringValue)maPort.Read(name); } while (0 == variant.Value.Length && timeout > TimeSpan.Zero); return variant.Value; } public bool Wait(TimeSpan timeout) { DateTime now = DateTime.Now; TimeSpan max = new TimeSpan(DateTime.MaxValue.Ticks - now.Ticks); DateTime then = now + (timeout > max ? max : timeout); do { Application.DoEvents(); } while (!bCancelled && DateTime.Now < then); return !bCancelled; } private void btnRun_Click(object sender, EventArgs e) { btnRun.Enabled = false; btnCancel.Enabled = true; Run(); btnCancel.Enabled = false; btnRun.Enabled = true; } private void btnCancel_Click(object sender, EventArgs e) { bCancelled = true; } private Vector.CANoe.ASAM.HILAPI.HILAPI hilAPI; private bool bCancelled; } }