using System;
using System.Windows.Forms;
using ASAM.XIL.Implementation.TestbenchFactory.Testbench;
using ASAM.XIL.Interfaces.Testbench.MAPort;
namespace HILAPI.Tester
{
interface ITestEnvironment
{
bool Wait(TimeSpan timeout);
void Output(string text);
}
public partial class MainForm : Form, ITestEnvironment
{
private IMAPort mMaPort;
private bool mCancelled;
///
/// Initializes a new instance of the class.
///
public MainForm()
{
InitializeComponent();
btnCancel.Enabled = false;
mCancelled = false;
InitializeXilApi();
}
///
/// Waits for the specified timeout.
///
/// The timeout.
///
public bool Wait(TimeSpan timeout)
{
DateTime now = DateTime.Now;
var max = new TimeSpan(DateTime.MaxValue.Ticks - now.Ticks);
DateTime then = now + (timeout > max ? max : timeout);
do
{
Application.DoEvents();
} while (!mCancelled && DateTime.Now < then);
return !mCancelled;
}
///
/// Outputs the specified text.
///
/// The text.
public void Output(string text)
{
mTxtLog.Text += text + Environment.NewLine;
mTxtLog.SelectionStart = 0;
mTxtLog.SelectionLength = 0;
}
///
/// Handles the Click event of the btnRun control.
///
/// The source of the event.
/// The instance containing the event data.
private void OnBtnRunClick(object sender, EventArgs e)
{
btnRun.Enabled = false;
btnCancel.Enabled = true;
Run();
btnCancel.Enabled = false;
btnRun.Enabled = true;
}
///
/// Handles the Click event of the btnCancel control.
///
/// The source of the event.
/// The instance containing the event data.
private void OnBtnCancelClick(object sender, EventArgs e)
{
mCancelled = true;
}
///
/// Initializes the XIL API.
///
private void InitializeXilApi()
{
try
{
// Create the XIL API factory by specifying the folder containing vendor specific configs
var factory = new TestbenchFactory("..//..//Config");
// Instantiate the Vector CANoe testbench
var testbench = factory.CreateVendorSpecificTestbench("Vector", "CANoe", "8.2");
// Create a Model Access Port from the corresponding XML configuration
mMaPort = testbench.MAPortFactory.CreateMAPort("CANoeMAPort");
mMaPort.Configure(mMaPort.LoadConfiguration("..//..//Config//VectorMAPortConfig.xml"), true);
Output("XIL API initialized.");
}
catch (Exception ex)
{
Output("An error occurred while initializing the XIL API.");
Output("Please make sure the vendor specific configuration file \"Vector.imf\" contains the correct path to your CANoe installation.");
Output("See the CANoe online help or the HIL API application note shipped with CANoe for details.");
Output("");
Output("Original error message:");
Output(ex.Message);
btnRun.Enabled = false;
}
}
///
/// Runs the tests.
///
private void Run()
{
mCancelled = false;
mTxtLog.Text = "";
// Start the XIL API
Output("Starting the XIL API...");
// Try to connect ton CANoe
try
{
mMaPort.StartSimulation();
}
catch
{
Output("Failed to start the XIL API.");
Output("Please make sure CANoe is running on the configured IP-address and FDX-port.");
return;
}
// Execute the tests
Output("Running...");
Tester.RunSeatTest(mMaPort, this);
Output("Done!");
// Stop the XIL API
mMaPort.StopSimulation();
}
}
}