CANoe_调用C#控件的方法_DEMO方法演示

17 篇文章 0 订阅
1、DEMO存放位置
D:\Users\Public\Documents\Vector\CANoe\Sample Configurations 11.0.96\CAN\MoreExamples\ActiveX_DotNET_Panels

每个人的电脑因为有区别存放位置不一样

2、控件制作--使用C#控件可以直接制作

 3、控件代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSharpPanels
{
    public class CSharpControl : System.Windows.Forms.UserControl
    {
        // Declare types
        private CANoe.Application         App;
        private CANoe.Measurement         Measurement;
        private CANoe.Variable            svMotorSwitch = null;
        private CANoe.Variable            svTurnSignal = null;
        private CANoe.Variable            svEngineSpeedEntry = null;
        private System.Windows.Forms.Timer tmrSignals;
                
        private System.Windows.Forms.GroupBox fraMotor;
        private System.Windows.Forms.GroupBox fraLight;
        private System.Windows.Forms.Label lblDriving;
        private System.Windows.Forms.Label lblTurn;
        private System.Windows.Forms.Button btnLightSwitch;
        private System.Windows.Forms.Button btnTurnSignal;
        private System.Windows.Forms.GroupBox fraEntry;
        private System.Windows.Forms.TrackBar sliSpeedEntry;
        private System.Windows.Forms.Button btnMotorSwitch;
        private System.Windows.Forms.Label lblCaption;
        
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        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>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if( components != null )
                    components.Dispose();

                if (tmrSignals != null)
                    tmrSignals.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          this.components = new System.ComponentModel.Container();
          this.fraMotor = new System.Windows.Forms.GroupBox();
          this.btnMotorSwitch = new System.Windows.Forms.Button();
          this.fraLight = new System.Windows.Forms.GroupBox();
          this.btnTurnSignal = new System.Windows.Forms.Button();
          this.btnLightSwitch = new System.Windows.Forms.Button();
          this.lblTurn = new System.Windows.Forms.Label();
          this.lblDriving = new System.Windows.Forms.Label();
          this.fraEntry = new System.Windows.Forms.GroupBox();
          this.sliSpeedEntry = new System.Windows.Forms.TrackBar();
          this.lblCaption = new System.Windows.Forms.Label();
          this.fraMotor.SuspendLayout();
          this.fraLight.SuspendLayout();
          this.fraEntry.SuspendLayout();
          ((System.ComponentModel.ISupportInitialize)(this.sliSpeedEntry)).BeginInit();
          this.SuspendLayout();
          // 
          // fraMotor
          // 
          this.fraMotor.Controls.Add(this.btnMotorSwitch);
          this.fraMotor.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.fraMotor.Location = new System.Drawing.Point(8, 32);
          this.fraMotor.Name = "fraMotor";
          this.fraMotor.Size = new System.Drawing.Size(104, 72);
          this.fraMotor.TabIndex = 0;
          this.fraMotor.TabStop = false;
          this.fraMotor.Text = "Motor Switch";
          // 
          // btnMotorSwitch
          // 
          this.btnMotorSwitch.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.btnMotorSwitch.Location = new System.Drawing.Point(24, 32);
          this.btnMotorSwitch.Name = "btnMotorSwitch";
          this.btnMotorSwitch.Size = new System.Drawing.Size(56, 25);
          this.btnMotorSwitch.TabIndex = 0;
          this.btnMotorSwitch.Text = "Off";
          this.btnMotorSwitch.Click += new System.EventHandler(this.btnMotorSwitch_Click);
          // 
          // fraLight
          // 
          this.fraLight.Controls.Add(this.btnTurnSignal);
          this.fraLight.Controls.Add(this.btnLightSwitch);
          this.fraLight.Controls.Add(this.lblTurn);
          this.fraLight.Controls.Add(this.lblDriving);
          this.fraLight.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.fraLight.Location = new System.Drawing.Point(120, 32);
          this.fraLight.Name = "fraLight";
          this.fraLight.Size = new System.Drawing.Size(136, 72);
          this.fraLight.TabIndex = 1;
          this.fraLight.TabStop = false;
          this.fraLight.Text = "Light Switch";
          // 
          // btnTurnSignal
          // 
          this.btnTurnSignal.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.btnTurnSignal.Location = new System.Drawing.Point(78, 38);
          this.btnTurnSignal.Name = "btnTurnSignal";
          this.btnTurnSignal.Size = new System.Drawing.Size(40, 25);
          this.btnTurnSignal.TabIndex = 3;
          this.btnTurnSignal.Text = "Off";
          this.btnTurnSignal.Click += new System.EventHandler(this.btnTurnSignal_Click);
          // 
          // btnLightSwitch
          // 
          this.btnLightSwitch.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.btnLightSwitch.Location = new System.Drawing.Point(14, 38);
          this.btnLightSwitch.Name = "btnLightSwitch";
          this.btnLightSwitch.Size = new System.Drawing.Size(40, 25);
          this.btnLightSwitch.TabIndex = 2;
          this.btnLightSwitch.Text = "Off";
          this.btnLightSwitch.Click += new System.EventHandler(this.btnLightSwitch_Click);
          // 
          // lblTurn
          // 
          this.lblTurn.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.lblTurn.Location = new System.Drawing.Point(66, 20);
          this.lblTurn.Name = "lblTurn";
          this.lblTurn.Size = new System.Drawing.Size(66, 16);
          this.lblTurn.TabIndex = 1;
          this.lblTurn.Text = "Turn signal";
          // 
          // lblDriving
          // 
          this.lblDriving.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.lblDriving.Location = new System.Drawing.Point(14, 20);
          this.lblDriving.Name = "lblDriving";
          this.lblDriving.Size = new System.Drawing.Size(48, 16);
          this.lblDriving.TabIndex = 0;
          this.lblDriving.Text = "Driving";
          // 
          // fraEntry
          // 
          this.fraEntry.Controls.Add(this.sliSpeedEntry);
          this.fraEntry.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.fraEntry.Location = new System.Drawing.Point(8, 112);
          this.fraEntry.Name = "fraEntry";
          this.fraEntry.Size = new System.Drawing.Size(248, 72);
          this.fraEntry.TabIndex = 2;
          this.fraEntry.TabStop = false;
          this.fraEntry.Text = "Motor Entry";
          // 
          // sliSpeedEntry
          // 
          this.sliSpeedEntry.Location = new System.Drawing.Point(16, 24);
          this.sliSpeedEntry.Maximum = 3500;
          this.sliSpeedEntry.Name = "sliSpeedEntry";
          this.sliSpeedEntry.Size = new System.Drawing.Size(216, 45);
          this.sliSpeedEntry.TabIndex = 0;
          this.sliSpeedEntry.TickFrequency = 350;
          this.sliSpeedEntry.Scroll += new System.EventHandler(this.sliSpeedEntry_Scroll);
          // 
          // lblCaption
          // 
          this.lblCaption.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          this.lblCaption.Location = new System.Drawing.Point(8, 8);
          this.lblCaption.Name = "lblCaption";
          this.lblCaption.Size = new System.Drawing.Size(256, 16);
          this.lblCaption.TabIndex = 3;
          this.lblCaption.Text = "C# Control";
          this.lblCaption.TextAlign = System.Drawing.ContentAlignment.TopCenter;
          // 
          // CSharpControl
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.Controls.Add(this.lblCaption);
          this.Controls.Add(this.fraEntry);
          this.Controls.Add(this.fraLight);
          this.Controls.Add(this.fraMotor);
          this.Name = "CSharpControl";
          this.Size = new System.Drawing.Size(264, 192);
          this.fraMotor.ResumeLayout(false);
          this.fraLight.ResumeLayout(false);
          this.fraEntry.ResumeLayout(false);
          this.fraEntry.PerformLayout();
          ((System.ComponentModel.ISupportInitialize)(this.sliSpeedEntry)).EndInit();
          this.ResumeLayout(false);

        }
        #endregion

        private void btnMotorSwitch_Click(object sender, System.EventArgs e)
        {
            if (Measurement.Running)
            {
                if (svMotorSwitch != null)
                {
                    if (btnMotorSwitch.Text == "Off")
                    {
                        svMotorSwitch.Value = 1;
                        btnMotorSwitch.Text = "On";
                    }
                    else
                    {
                        svMotorSwitch.Value = 0;
                        btnMotorSwitch.Text = "Off";
                    }
                }
            }
        }

        private void svMotorSwitch_OnChange(object Value)
        {
            int tmp = (int)Value;
            if (tmp == 0)
            {
                btnMotorSwitch.Text = "Off";
            }
            else
            {
                btnMotorSwitch.Text = "On";
            }
        }

        private void btnLightSwitch_Click(object sender, System.EventArgs e)
        {
            if (Measurement.Running)
            {
                CANoe.Bus bus = null;
                try
                {
                    bus = (CANoe.Bus)App.get_Bus("CAN");
                    if (bus != null)
                    {
                        CANoe.Signal signal = (CANoe.Signal)bus.GetSignal(1, "LightState", "OnOff");
                        if (signal != null)
                        {
                            if (btnLightSwitch.Text == "Off")
                            {
                                signal.Value = 1;
                                btnLightSwitch.Text = "On";
                            }
                            else
                            {
                                signal.Value = 0;
                                btnLightSwitch.Text = "Off";
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
   
        private void tmrSignals_Tick(object sender, System.EventArgs e)
        {
            // get all the signal values and actualize controls
            if (Measurement.Running)
            {
                CANoe.Bus bus = null;
                try
                {
                    bus = (CANoe.Bus)App.get_Bus("CAN");
                    if (bus != null)
                    {
                        CANoe.Signal signal = (CANoe.Signal)bus.GetSignal(1, "LightState", "OnOff");
                        if (signal != null)
                        {
                            if (signal.Value == 0)
                                btnLightSwitch.Text = "Off";
                            else
                                btnLightSwitch.Text = "On";
                        }
                    }
                }
                catch
                {
                }
            }
        }

        private void btnTurnSignal_Click(object sender, System.EventArgs e)
        {
            if (Measurement.Running)
            {
                if (btnTurnSignal.Text == "Off") 
                {
                    if (svTurnSignal != null)
                    {
                        svTurnSignal.Value = 1;
                        btnTurnSignal.Text = "On";
                    }
                }
                else
                {
                    if (svTurnSignal != null)
                    {
                        svTurnSignal.Value = 0;
                        btnTurnSignal.Text = "Off";
                    }
                }
            }
        }

        private void svTurnSignal_OnChange(object Value)
        {
            int tmp = (int)Value;
            if (tmp == 0)
            {
                btnTurnSignal.Text = "Off";
            }
            else
            {
                btnTurnSignal.Text = "On";
            }
        }

        private void sliSpeedEntry_Scroll(object sender, System.EventArgs e)
        {
            if(Measurement.Running)
            {
                if (svEngineSpeedEntry != null)
                {
                    double tmp = sliSpeedEntry.Value;
                    svEngineSpeedEntry.Value = tmp;
                }
            }
        }

        private void svEngineSpeedEntry_OnChange(object Value)
        {
            sliSpeedEntry.Value = (int)((double)Value);
        }
    }
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace CSharpPanels
{
    public class CSharpDisplay : System.Windows.Forms.UserControl
    {
        // Declare types
        private CANoe.Application App;
      private CANoe.Measurement Measurement;
      internal GroupBox fraEngineSpeed;
      internal GroupBox fraMotor;
      internal GroupBox fraLight;
      internal Label lblEngine;
      internal Label lblRunning;
      internal Label lblDriving;
      internal Label lblTurn;
      internal Label lblDrivingState;
      internal Label lblTurnState;
      private System.Windows.Forms.Timer tmrSignals;
      internal Label lblCaption;
        private System.ComponentModel.IContainer components;

        public CSharpDisplay()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // Initialize types
            App         = new CANoe.Application();
            Measurement = (CANoe.Measurement)App.Measurement;
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.fraEngineSpeed = new System.Windows.Forms.GroupBox();
            this.lblEngine = new System.Windows.Forms.Label();
            this.fraMotor = new System.Windows.Forms.GroupBox();
            this.lblRunning = new System.Windows.Forms.Label();
            this.fraLight = new System.Windows.Forms.GroupBox();
            this.lblTurnState = new System.Windows.Forms.Label();
            this.lblDrivingState = new System.Windows.Forms.Label();
            this.lblTurn = new System.Windows.Forms.Label();
            this.lblDriving = new System.Windows.Forms.Label();
            this.tmrSignals = new System.Windows.Forms.Timer(this.components);
            this.lblCaption = new System.Windows.Forms.Label();
            this.fraEngineSpeed.SuspendLayout();
            this.fraMotor.SuspendLayout();
            this.fraLight.SuspendLayout();
            this.SuspendLayout();
            // 
            // fraEngineSpeed
            // 
            this.fraEngineSpeed.Controls.Add(this.lblEngine);
            this.fraEngineSpeed.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.fraEngineSpeed.Location = new System.Drawing.Point(6, 26);
            this.fraEngineSpeed.Name = "fraEngineSpeed";
            this.fraEngineSpeed.Size = new System.Drawing.Size(111, 39);
            this.fraEngineSpeed.TabIndex = 0;
            this.fraEngineSpeed.TabStop = false;
            this.fraEngineSpeed.Text = "Engine Speed";
            // 
            // lblEngine
            // 
            this.lblEngine.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblEngine.Location = new System.Drawing.Point(6, 14);
            this.lblEngine.Name = "lblEngine";
            this.lblEngine.Size = new System.Drawing.Size(101, 19);
            this.lblEngine.TabIndex = 0;
            this.lblEngine.Text = "0";
            this.lblEngine.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // fraMotor
            // 
            this.fraMotor.Controls.Add(this.lblRunning);
            this.fraMotor.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.fraMotor.Location = new System.Drawing.Point(121, 26);
            this.fraMotor.Name = "fraMotor";
            this.fraMotor.Size = new System.Drawing.Size(99, 39);
            this.fraMotor.TabIndex = 1;
            this.fraMotor.TabStop = false;
            this.fraMotor.Text = "Motor State";
            // 
            // lblRunning
            // 
            this.lblRunning.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblRunning.Location = new System.Drawing.Point(6, 14);
            this.lblRunning.Name = "lblRunning";
            this.lblRunning.Size = new System.Drawing.Size(89, 19);
            this.lblRunning.TabIndex = 0;
            this.lblRunning.Text = "Not Running";
            this.lblRunning.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // fraLight
            // 
            this.fraLight.Controls.Add(this.lblTurnState);
            this.fraLight.Controls.Add(this.lblDrivingState);
            this.fraLight.Controls.Add(this.lblTurn);
            this.fraLight.Controls.Add(this.lblDriving);
            this.fraLight.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.fraLight.Location = new System.Drawing.Point(6, 70);
            this.fraLight.Name = "fraLight";
            this.fraLight.Size = new System.Drawing.Size(214, 39);
            this.fraLight.TabIndex = 2;
            this.fraLight.TabStop = false;
            this.fraLight.Text = "Driving Light";
            // 
            // lblTurnState
            // 
            this.lblTurnState.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblTurnState.Location = new System.Drawing.Point(184, 17);
            this.lblTurnState.Name = "lblTurnState";
            this.lblTurnState.Size = new System.Drawing.Size(26, 15);
            this.lblTurnState.TabIndex = 3;
            this.lblTurnState.Text = "Off";
            this.lblTurnState.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // lblDrivingState
            // 
            this.lblDrivingState.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblDrivingState.Location = new System.Drawing.Point(80, 17);
            this.lblDrivingState.Name = "lblDrivingState";
            this.lblDrivingState.Size = new System.Drawing.Size(26, 15);
            this.lblDrivingState.TabIndex = 2;
            this.lblDrivingState.Text = "Off";
            this.lblDrivingState.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // lblTurn
            // 
            this.lblTurn.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblTurn.Location = new System.Drawing.Point(110, 17);
            this.lblTurn.Name = "lblTurn";
            this.lblTurn.Size = new System.Drawing.Size(76, 19);
            this.lblTurn.TabIndex = 1;
            this.lblTurn.Text = "Turn signal:";
            // 
            // lblDriving
            // 
            this.lblDriving.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblDriving.Location = new System.Drawing.Point(12, 17);
            this.lblDriving.Name = "lblDriving";
            this.lblDriving.Size = new System.Drawing.Size(63, 20);
            this.lblDriving.TabIndex = 0;
            this.lblDriving.Text = "Driving:";
            // 
            // tmrSignals
            // 
            this.tmrSignals.Enabled = true;
            this.tmrSignals.Tick += new System.EventHandler(this.tmrSignals_Tick);
            // 
            // lblCaption
            // 
            this.lblCaption.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold);
            this.lblCaption.Location = new System.Drawing.Point(6, 6);
            this.lblCaption.Name = "lblCaption";
            this.lblCaption.Size = new System.Drawing.Size(214, 17);
            this.lblCaption.TabIndex = 3;
            this.lblCaption.Text = "C# Display";
            this.lblCaption.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // CSharpDisplay
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lblCaption);
            this.Controls.Add(this.fraLight);
            this.Controls.Add(this.fraMotor);
            this.Controls.Add(this.fraEngineSpeed);
            this.Name = "CSharpDisplay";
            this.Size = new System.Drawing.Size(226, 117);
            this.fraEngineSpeed.ResumeLayout(false);
            this.fraMotor.ResumeLayout(false);
            this.fraLight.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void tmrSignals_Tick(object sender, System.EventArgs e)
        {
            // get all the signal values and actualize controls
            if(Measurement.Running)
            {
                CANoe.Configuration configuration     = null;
                CANoe.SimulationSetup simulationSetup = null; 
                CANoe.Nodes nodes                     = null;
                CANoe.Node node                       = null;
                CANoe.Signals motorInputs             = null;
                CANoe.Signals motorControlInputs      = null;
                try
                {
                    configuration   = (CANoe.Configuration)App.Configuration;
                    simulationSetup = (CANoe.SimulationSetup)configuration.SimulationSetup;
                    nodes           = (CANoe.Nodes)simulationSetup.Nodes;
                    node            = (CANoe.Node)nodes["Motor"];
                    motorInputs     = (CANoe.Signals)node.Inputs;
                
                    if (motorInputs["OnOff"].Value == 0)
                    {
                        lblRunning.Text = "Not Running";
                    }
                    else
                    {
                        lblRunning.Text = "Running";
                    }

                    lblEngine.Text = motorInputs["EngineSpeed"].Value.ToString();
        
                    motorInputs = null;
                
                    node = (CANoe.Node)nodes["MotorControl"];
                    motorControlInputs = (CANoe.Signals)node.Inputs;
        
                    if(motorControlInputs["OnOff"].Value == 0)
                    {
                        lblDrivingState.Text = "Off";
                    }
                    else
                    {
                        lblDrivingState.Text = "On";
                    }

                    if(motorControlInputs["TurnSignal"].Value == 0)
                    {
                        lblTurnState.Text = "Off";
                    }
                    else
                    {
                        lblTurnState.Text = "On";
                    }
                }
                catch
                {
                }
                finally
                {
                    motorControlInputs = null;
                    node = null;
                    nodes = null;
                    simulationSetup = null;
                    configuration = null;
                }
            }
        }
    }
}
4、运行效果展示

 

 5、总结

现在啊,汽车电子这块儿技术发展得太快了,对测试和验证这些系统的需求也越来越多。CANoe呢,就像一个超级英雄的工具,专门用来模拟和测试汽车里的网络通信。我们想了个招儿,就是做几个自己定义的小工具,这样跟CANoe打交道就更直观了,能实时看着汽车信号怎么样,还能动手调调。

小工具是咋设计的:

我们做了两个小帮手,一个叫CSharpControl,负责接收你给的信号指令;另一个叫CSharpDisplay,就像个小喇叭,告诉你信号现在是啥状态。这样一分工,它们的职责就很清楚了,以后维护起来也方便,想加点新功能也容易。

信号是怎么处理的:

我们靠CANoe的超能力,实时拿到信号的状态,然后有个定时器(就像个小闹钟,我们叫它tmrSignals),不停地检查信号有没有变,一变就更新到界面上。这样你就能随时知道系统啥情况了,用起来更顺手。

事件驱动是咋回事:

我们还用了个挺聪明的方法,就是事件驱动。比如说你点个按钮,或者信号变了,就会触发一些程序去处理这些事情。这样写代码就更灵活了,以后想加新功能也不费劲。

资源管理这块儿也很重要:

我们得确保用的资源,比如CANoe里的对象和定时器,用完了都得好好收起来,别占着地方。所以我们在控件不用的时候,重写了个Dispose方法,让它们都能被正确回收,这样就不会浪费内存了。

界面长啥样:

界面设计嘛,我们想着得让用户用着舒服。所以用了一些分组框,把控件都归类放好了,看起来就整齐多了。每个区域都有名字,一看就知道是干啥的。

以后还能怎么发展:

现在的设计已经挺好了,但未来还能更上一层楼。比如说,想加更多控制信号和显示的功能,或者把CANoe的其他超能力,比如记录和分析数据,也集成进来。

最后说说感受:

这个项目让我们学会了怎么用C#和CANoe API做个很厉害的用户控件,跟汽车电子系统玩得转。设计得好,不仅现在开发起来快,以后维护、升级也方便。以后啊,我们还要继续挖掘新技能,让用户体验和系统性能都更上一层楼!

"an-ind-1-011_using_canoe_net_api.pdf" 是一个关于使用 Canoe Net API 的文档。Canoe 是一种网络分析工具,Canoe Net API 是 Canoe 提供的用于与其它系统进行集成和交互的应用程序接口。 这个文档主要介绍了如何使用 Canoe Net API 进行网络分析和数据处理。它包含了使用 Canoe Net API 的基本概念和操作步骤,以及示例代码和实际应用场景。通过阅读这个文档,开发人员可以了解如何使用 Canoe Net API 对网络数据进行收集、分析、过滤和可视化,并可以根据自己的需求进行定制和扩展。 使用 Canoe Net API,开发人员可以实现以下功能: 1. 收集网络数据:通过 Canoe Net API,可以获取网络传输层(如 TCP、UDP)上的数据包,包括源地址、目标地址、数据负载等信息。 2. 网络分析:可以利用 Canoe Net API 提供的功能,对网络数据进行分析和统计,例如计算网络流量、检测网络异常等。 3. 数据过滤:通过 Canoe Net API 可以对网络数据进行过滤,只保留感兴趣的数据包,以便后续处理和分析。 4. 可视化展示:Canoe Net API 还提供了可视化工具,可以将网络数据以图表、图形等形式展示,便于用户直观地了解网络情况。 总之,"an-ind-1-011_using_canoe_net_api.pdf" 是一份介绍 Canoe Net API 的文档,通过学习和应用其中的内容,开发人员可以使用 Canoe Net API 进行网络数据处理和分析,提升网络管理和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值