事件总线第一次点击_CANoe教程 | CAPL编程 - 事件驱动

311f0f528d8075e6b0d6bf3b325a9ddc.png

CAPL是一种面向过程、由事件驱动的类C语言。

事件驱动针对于顺序执行,其区别如下:

顺序执行:

顺序执行流程中,子例程或过程函数按照代码编写顺序逐句执行。

事件驱动:

CAPL程序由事件驱动,工程运行过程中发生指定的事件时才会运行相应的事件处理函数。[1]

366d5bfc0989e6c14ee80194486bb1d4.png
顺序执行VS事件驱动

在CAPL中添加事件处理函数: [2]

cfb7ce1bb48be58cd7da8bba5a01b7e8.png

重要的事件处理函数:

9f8ac49c27726c0c01bc1e5ff0848118.png

事件总览: [3]

69d5b552b93ffa3c18506e36229d211b.png

事件详解

事件起始关键字 on

on后加某种事件,工程运行时条件触发,则执行函数体内的语句。

关键字this

系统变量、环境变量或CAN报文事件中,可以用this关键字访问其指代的数据内容,如:

on envvar Switch {
// Declare a CAN message to be transmitteed
message Controller msg;

// Read out the value of the switch
// Assign to the signal Stop
msg.Stop = getvalue(this);
// Output the message on the bus
output(msg);
}

系统事件

系统事件主要用于处理CANoe测量系统的控制功能,主要有on start、on preStart、onstopMeasurement、on preStop、on key<newKey>以及on timer

8bfc92210e2ba56443e7a6272755cf66.png
系统事件

Example:

//on preStart procedure

on preStart
{
   write("Measurement started!");
   msg_Count = 0;
}

//on start procedure

on start
{
   write("start Node A");
   setTimer(cycTimer,20);
   CallAllOnEnvVar(); // CANoe only
}

//on preStop procedure

on preStop
{
   message ShutdownReq m;

   output(m);
   DeferStop(1000);
}

//on stopMeasurement procedure

on stopMeasurement
{
   write("Message 0x%x received: %d", msg.id, msg_Count);
}

CAN控制器事件

当CAN控制器或错误计数器状态变化时调用CAN控制器事件。

f621ba49ba960c117670c9a068ef04a0.png
CAN控制器事件

Example:

//on errorPassive procedure
on errorPassive {
   ...
   write("CAN Controller is in errorPassive state")
   write(" errorCountTX = %d", this.errorCountTX);
   write(" errorCountRX = %d", this.errorCountRX);
};

//on busOff procedure
on busOff
{
   int errRxCnt;
   int errTxCnt;
   int channel;
   double timestamp; // [seconds]

   timestamp = (double)timeNow() / (double)100000;
   channel = this.can;
   errRxCnt = this.errorCountRX;
   errTxCnt = this.errorCountTX;
   Write("Bus Off: time=%f channel=%d, errRxCnt=%d, errTxCnt=%d",
   timestamp, channel, errRxCnt, errTxCnt);

   resetCanEx(channel);
}

CAN报文/信号事件

CAN报文或信号变化时调用报文/信号事件。

4266b8a43c36f51e657bbf6f229e08a3.png
CAN报文/信号事件

报文事件:

d28ef8ab4de4dead843c0d2a7598ecd7.png

信号事件:

on signal LightSwitch::OnOff
{
  v1 = this.raw;
  v2 = $LightSwitch::OnOff.raw;
}

定时器事件

定义好定时器变量后,由SetTimer函数设置定时器间隔并启动定时器。当定时器运行到设定的时间间隔时触发定时器事件,并执行on timer函数体中的程序。

msTimer myTimer;
message 100 msg;
...
on key 'a' {
   setTimer(myTimer,20);
}
...
on timer myTimer {
   output(msg);
}

键盘事件

通过定义键盘事件,用户可以在工程运行时通过点击键盘触发预先定义的行为。这在实际开发和测试时非常常用。比如用户可以在任意时刻向总线发送特定的消息、改变信号或系统变量的值或是启动停止测量。

系统变量/环境变量事件

系统变量和环境变量事件分别是对系统变量和环境变量发生变化时的响应。

系统变量事件:

on sysvar IO::DI_0
{
$Gateway::IOValue = @this;
}

环境变量事件

on envvar Switch {
// Declare a CAN message to be transmitteed
message Controller msg;

// Read out the value of the switch
// Assign to the signal Stop
msg.Stop = getvalue(this);
// Output the message on the bus
output(msg);
}

更多详细介绍请参考:

05_CANoe_CAPL_Event​v.youku.com
150ffbd9d05ed4d19a8e86da7a06ea17.png

参考

  1. ^https://v.youku.com/v_show/id_XNDQxMDgwNjE5Mg==.html?spm=a2hzp.8244740.0.0
  2. ^https://assets.vector.com/cms/content/products/VectorCAST/Events/TechNights/CAPLQuickstart_Generic_2018_Final.pdf
  3. ^https://kb.vector.com/80/
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值