public CSharpControl() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // Initialize timer to get Signal changes tmrSignals = new System.Windows.Forms.Timer(components); if (tmrSignals != null) { tmrSignals.Enabled = true; tmrSignals.Tick += new System.EventHandler(tmrSignals_Tick); } // Initialize types App = new CANoe.Application(); Measurement = (CANoe.Measurement)App.Measurement; CANoe.System system = (CANoe.System)App.System; if (system != null) { CANoe.Namespaces namespaces = (CANoe.Namespaces)system.Namespaces; if (namespaces != null) { CANoe.Namespace nsEngine = (CANoe.Namespace)namespaces["Engine"]; if (nsEngine != null) { CANoe.Variables engineVars = (CANoe.Variables)nsEngine.Variables; if (engineVars != null) { svMotorSwitch = (CANoe.Variable)engineVars["MotorSwitch"]; svEngineSpeedEntry = (CANoe.Variable)engineVars["EngineSpeedEntry"]; if (svMotorSwitch != null) { svMotorSwitch.OnChange += new CANoe._IVariableEvents_OnChangeEventHandler(svMotorSwitch_OnChange); } if (svEngineSpeedEntry != null) { svEngineSpeedEntry.OnChange += new CANoe._IVariableEvents_OnChangeEventHandler(svEngineSpeedEntry_OnChange); } } } CANoe.Namespace nsLights = (CANoe.Namespace)namespaces["Lights"]; if (nsLights != null) { CANoe.Variables lightsVars = (CANoe.Variables)nsLights.Variables; if (lightsVars != null) { svTurnSignal = (CANoe.Variable)lightsVars["TurnSignal"]; if (svTurnSignal != null) { svTurnSignal.OnChange += new CANoe._IVariableEvents_OnChangeEventHandler(svTurnSignal_OnChange); } } } } } }
/// <summary> /// Occurs when a configuration was successfully opened. /// </summary> private void ConfigurationOpened() { try { // Assign system variables from namespaces. CANoe.System CANoeSystem = (CANoe.System)mCANoeApp.System; CANoe.Namespaces CANoeNamespaces = (CANoe.Namespaces)CANoeSystem.Namespaces; CANoe.Namespace CANoeNamespaceGeneral = (CANoe.Namespace)CANoeNamespaces["General"]; CANoe.Variables CANoeVariablesGeneral = (CANoe.Variables)CANoeNamespaceGeneral.Variables; mCANoeSysVar1 = (CANoe.Variable)CANoeVariablesGeneral["SysVar1"]; mCANoeSysVar2 = (CANoe.Variable)CANoeVariablesGeneral["SysVar2"]; // Assign signals. CANoe.Bus CANoeBus = (CANoe.Bus)mCANoeApp.get_Bus("CAN"); mCANoeEngineStatus = (CANoe.Signal)CANoeBus.GetSignal(1, "EngineData", "EngineStatus"); mCANoeEngineSpeed = (CANoe.Signal)CANoeBus.GetSignal(1, "EngineData", "EngineSpeed"); mCANoeEngineTemp = (CANoe.Signal)CANoeBus.GetSignal(1, "EngineData", "EngineTemp"); } catch (System.Exception) { MessageBox.Show("Possible cause: Wrong namespace names, bus name, system variable names or signal names in source code or configuration.", "Error while assigning system variables and signals", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Enables the start/stop measurement button. mGroupboxConfigSettings.Enabled = false; mGroupboxMeasurementControl.Enabled = true; mButtonStartStop.Focus(); if (mCANoeApp != null) { // Wire OnQuit event handler. mCANoeApp.OnQuit += new CANoe._IApplicationEvents_OnQuitEventHandler(CANoeQuit); } if (mCANoeMeasurement != null) { // Create on event handlers. mCANoeMeasurement.OnInit += new CANoe._IMeasurementEvents_OnInitEventHandler(MeasurementInitiated); mCANoeMeasurement.OnExit += new CANoe._IMeasurementEvents_OnExitEventHandler(MeasurementExited); } // Indicate that an instance of CANoe is running. mCANoeInstanceRunning = true; }
private void mBt_Connect_Click(object sender, EventArgs evargs) { mLV_Targets.Items.Clear(); mLV_Targets.Enabled = false; mBt_StartIdent.Enabled = false; mBt_ConfigureVariants.Enabled = false; mTB_Status.Text = "Connecting to CANoe and looking for configured diagnostics targets ..."; mTB_Status.BackColor = Color.LightGray; mSysVarResult = null; mSysVarTarget = null; try { CANoe.Application app = new CANoe.Application(); CANoe.Configuration config = (CANoe.Configuration)app.Configuration; CANoe.GeneralSetup generalSetup = (CANoe.GeneralSetup)config.GeneralSetup; CANoe.DiagnosticsSetup diagSetup = (CANoe.DiagnosticsSetup)generalSetup.DiagnosticsSetup; CANoe.DiagDescriptions descriptions = (CANoe.DiagDescriptions)diagSetup.DiagDescriptions; foreach (CANoe.Network netw in (CANoe.Networks)app.get_Networks(null)) { foreach (CANoe.Device dev in (CANoe.Devices)netw.Devices) { try { Diagnostic.Diagnostic diag = (Diagnostic.Diagnostic)dev.Diagnostic; if (null != diag) { ListViewItem item = new ListViewItem(); item.Checked = false; item.Text = dev.Name; item.SubItems.Add(netw.Name); item.SubItems.Add("(unknown)"); // default for configured variant, should be overwritten item.SubItems.Add("(not identified yet)"); // default for configured variant, should be overwritten foreach (CANoe.DiagDescription descr in descriptions) { if (descr.Qualifier == dev.Name) { item.SubItems[chConfiguredVariant.Index].Text = descr.Variant; break; } } mLV_Targets.Items.Add(item); } } catch { // device does not have a diagnostics interface, so ignore it } } } } catch (System.Exception ex) { mTB_Status.Text = "Error: " + ex.ToString(); mTB_Status.BackColor = Color.Red; return; } mTB_Status.Text = "" + mLV_Targets.Items.Count + " targets found."; if (mLV_Targets.Items.Count > 0) { mLV_Targets.Enabled = true; mTB_Status.BackColor = Color.LightGreen; } { CANoe.Application app = new CANoe.Application(); mBt_StartIdent.Enabled = !((CANoe.Measurement)app.Measurement).Running; } }
void StartIdentificationRun() { if (GetTargetString() == "") { mTB_Status.Text = "Select Targets first!"; mTB_Status.BackColor = Color.LightSalmon; return; } if (PathToSourceNode() == "") { mTB_Status.Text = "Cannot find CAPL program that performs identification!"; mTB_Status.BackColor = Color.Red; return; } AddStatusText("Configuring CANoe and trying to start the variant identification ..."); mTB_Status.BackColor = Color.LightGray; const string csNSDiagIdentifier = "DiagIdentifier"; const string csVarResult = "ResultOfIdentification"; const string csVarTarget = "Target"; const string csNodeName = "_TEMP_VarIdent_V1.0"; // Prepare connection to application CANoe.Application app = new CANoe.Application(); CANoe.Namespaces spaces = (CANoe.Namespaces)((CANoe.System)app.System).Namespaces; CANoe.Variables varsDiagIdentifier = null; // Check if namespace and/or variables have been defined already bool bCreateNamespace = true; bool bCreateSysVarResult = true; bool bCreateSysVarTarget = true; foreach (CANoe.Namespace ns in spaces) { if (ns.Name == csNSDiagIdentifier) { varsDiagIdentifier = (CANoe.Variables)ns.Variables; bCreateNamespace = false; foreach (CANoe.Variable var in varsDiagIdentifier) { if (var.Name == csVarResult) { mSysVarResult = var; bCreateSysVarResult = false; } else if (var.Name == csVarTarget) { mSysVarTarget = var; bCreateSysVarTarget = false; } } break; } } // Create namespace and variables, if necessary AddStatusText("Creating namespace and system variables"); if (bCreateNamespace) { varsDiagIdentifier = (CANoe.Variables)spaces.Add(csNSDiagIdentifier).Variables; } if (bCreateSysVarResult) { mSysVarResult = (CANoe.Variable)varsDiagIdentifier.AddWriteable(csVarResult, ""); } if (bCreateSysVarTarget) { mSysVarTarget = (CANoe.Variable)varsDiagIdentifier.AddWriteable(csVarTarget, ""); } if (mSysVarResult == null || mSysVarTarget == null) { AddStatusText("Cannot attach to system variables needed for control!"); mTB_Status.BackColor = Color.Red; return; } // Add the simulation node that communicates with this program an initiates the variant identification to the setup AddStatusText("Adding and configuring simulation node"); CANoe.Configuration config = (CANoe.Configuration)app.Configuration; CANoe.SimulationSetup simSetup = (CANoe.SimulationSetup)config.SimulationSetup; CANoe.Buses buses = (CANoe.Buses)simSetup.Buses; if (buses.Count < 1) { AddStatusText("There is no bus configure to attach the simulation node to!"); mTB_Status.BackColor = Color.Red; return; } CANoe.Bus bus = (CANoe.Bus)buses[1]; mTB_Status.Text += " to bus " + bus.Name; CANoe.Nodes nodes = (CANoe.Nodes)bus.Nodes; CANoe.Node node = (CANoe.Node)nodes.Add(csNodeName, null, null); node.FullName = PathToSourceNode(); // Start the measurement if (DialogResult.OK == MessageBox.Show("If you press OK, the CANoe measurement will be started and the variant identification is run!", "Attention: Measurement Start" , MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { CANoe.Measurement meas = (CANoe.Measurement)app.Measurement; AddStatusText("Starting CANoe measurement "); meas.Start(); while (!meas.Running) { mTB_Status.Text += "."; DoSleep(20); } DoVariantIdentification(); AddStatusText("Stopping CANoe measurement "); meas.Stop(); while (meas.Running) { mTB_Status.Text += "."; DoSleep(20); } } // Cleanup AddStatusText("Cleaning up ..."); // Remove the simulation node int i; AddStatusText("Removing simulation node"); nodes.Remove(node); // Remove the namespace if (bCreateNamespace) { for (i = 1; i <= spaces.Count; ++i) { if (spaces[i].Name == csNSDiagIdentifier) { AddStatusText("Removing namespace"); spaces.Remove(i); break; } } } }