1 目的

利用CANoe在两路CAN通道之间创建一个网关,通过CAPL实现CAN1、CAN2通道间的报文转发,并进行故障注入测试(通过改变某些信号的值)。

(本实例仅用于博主学习记录)

2 步骤

创建一个两路通道(CAN1、CAN2)的工程文件在CAN1新增一个Node节点,并命名为GW,并将node的Buses设置为CAN1+CAN2(如下图,此时CAN2也会同步出现一个GW节点)将dbc复制一份,并分别给CAN1、CAN2加载一份双击GW节点铅笔按钮,开始写CAPL脚本(如下),并将脚本文件保存至相应文件夹

/*@!Encoding:936*/

includes

{

}

variables

{

}

on message CAN1.* //将CAN1通道的报文透传(转发)给CAN2

{

message CAN2.* m;

if(this.DIR==RX) // if it is a received frame

{

if(this.CAN==1)

{

m=this;

output(m); // send it to the other channel

}

}

}

//on message CAN2.* //将CAN2通道的报文透传(转发)给CAN1,这里暂时不用

//{

// message CAN1.* m;

// if(this.DIR==RX) // if it is a received frame

// {

// if(this.CAN==2)

// {

// m=this;

// output(m); // send it to the other channel

// }

// }

//}

on message CAN1.0x506 //将CAN1 ID为506的报文,修改部分信号的值后再转发给CAN2

{

message CAN2.0x506 m;

// this is an example for a message that will be manipulated before it is sent on CAN2

if (this.DIR==RX) // if this message is received

{

if(this.CAN==1)

{

m=this;

// m.Byte(0)=0x31; // 第1个字节

// m.Byte(1)=0x31; // 第2个字节

// you can use signal-based access as well: m.SignalName = ....

output(m);

}

}

}

上位机输出CAN(DB9接口)接入CANoe CAN2通道,CANoe CAN3通道接入上位机原本接入的Control CAN(上位机<-->CANoe<-->Control CAN)运行工程文件

3 结果展示

(注:上图展示并未对506的信号值进行修改,所以CAN1、CAN2的报文传递的数据完全一样)

查看原文