首页 > 编程语言 >CANoe_调用C#控件的方法_DEMO方法演示

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

时间:2024-10-10 22:18:44浏览次数:3  
标签:控件 CANoe C# System Forms new Drawing Size

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做个很厉害的用户控件,跟汽车电子系统玩得转。设计得好,不仅现在开发起来快,以后维护、升级也方便。以后啊,我们还要继续挖掘新技能,让用户体验和系统性能都更上一层楼!

标签:控件,CANoe,C#,System,Forms,new,Drawing,Size
From: https://blog.csdn.net/caoxuefei520/article/details/142833566

相关文章

  • 实验1 现代C++编程初体验
    1.实验任务1task1源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>//*****表明这是一个模板参数,可以接受任意类型的参数......
  • selenium:ActionChains类模拟鼠标和键盘操作(6)
    selenium包中提供了ActionChains类,主要用于鼠标和键盘的一些操作,比如鼠标移动,鼠标按键,或者是悬停和拖放等;模拟键盘按键输入,比如按住control+C键等。使用时先导入该类:fromselenium.webdriverimportActionChainsActionChains类的方法介绍 ActionChains类常用方法函......
  • “降维模糊C均值(PCA-FCM)”创新算法的聚类与可视化
    在这篇博客中,我们将探讨一个MATLAB代码示例,它展示了如何从Excel文件导入数据,进行模糊C均值(FCM)聚类,并通过2D和3D图形可视化聚类结果。让我们一步一步地深入这个过程!1.环境准备首先,我们需要清空工作环境,以确保没有旧变量干扰我们的结果。这可以通过以下几行代码实现:clear;cl......
  • vscode git 提交不进行commit 校验按钮开启
    第一种方式设置里面搜索gitverify,然后打钩然后git提交这里就有不校验的按钮了第二种方式直接setting.json里添加配置"git.allowNoVerifyCommit":true,......
  • 使用Qt Creator创建项目
    个人主页:C++忠实粉丝欢迎点赞......
  • PCIe配置篇(1)——如何进行配置操作(一)
    一、功能的唯一标识——BDF    首先我们简单回顾一下总线(Bus)、设备(Device)、功能(Function)这几个概念:功能(function):是PCI设备中独立的功能单元,最多可以有8个功能。设备(device):是物理设备,连接在PCI总线上,可能包含一个或多个功能。总线(bus):是设备和系统通信的通道,一个系统......
  • 求 LCA 方法总结
    求LCA方法总结前言求LCA是十分基础的东西,但是方法众多。此篇介绍OI中常用的求法。倍增求LCA蒟蒻最先学的求LCA方法就是倍增求LCA。预处理和查询时间复杂度均为单\(\log\)。优点为好理解,比较简单,且便于处理路径数据。树剖LCA重链剖分。优点是预处理是线性复杂度,......
  • 一个连续动作空间的SAC的例子
    """MySACcontinuousdemo"""importargparseimportcopyimportgymimportnumpyasnpimporttorchimporttorch.nn.functionalasFfromtorchimportnnfromtorch.distributionsimportNormaldefparse_args()->argpar......
  • 海明码(Hamming Code)的知识点
    这道题目考察的是海明码(HammingCode)的知识点。海明码是一种线性错误纠正码,由理查德·海明(RichardHamming)在1950年发明。它主要用于检测和纠正数据传输或存储过程中的单个错误位。海明码的基本原理:奇偶校验:海明码利用奇偶校验位来检测错误。在数据位之间插入校验位,使得每个校......
  • C语言最重要的知识点复习资料
    总体上必须清楚的: 1)结构化程序的三种基本结构:顺序结构 ,循环结构(三种循环语句),选择结构(if和switch) 2)读程序从main()开始,按从上往下顺序(main函数是程序的入口也是程序的终点,总体顺序结构,遇到循环做循环,遇到选择做选择,程序的其他函数不管其位置是在main函数......