CAPL编程 - 事件驱动

1 事件概述

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

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

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

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

CAPL编程 - 事件驱动_第1张图片

顺序执行VS事件驱动

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

CAPL编程 - 事件驱动_第2张图片

重要的事件处理函数:

CAPL编程 - 事件驱动_第3张图片

事件总览: [3]

CAPL编程 - 事件驱动_第4张图片

2 事件详解

事件起始关键字 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

CAPL编程 - 事件驱动_第5张图片

系统事件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控制器事件。

CAPL编程 - 事件驱动_第6张图片

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报文或信号变化时调用报文/信号事件。

CAPL编程 - 事件驱动_第7张图片

CAN报文/信号事件

报文事件:

CAPL编程 - 事件驱动_第8张图片

信号事件:

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);
}

你可能感兴趣的:(CANoe,开发语言,CANoe,CAPL)