Vector-常用CAN工具 – CANoe入门到精通_04

汽车安全 1年前 (2022) admin
483 0 0


 前面已经介绍了Network Node节点的创建和配置,我想大家如果仔细研究下这块基本没什么问题,但是针对相应的CAPL编程该如何去做呢?今天这篇文章就是我们专门介绍在Network Node节点中常用的一些操作函数和使用技巧。

Vector-常用CAN工具 - CANoe入门到精通_04

编辑

五、 Network Node相关CAPL编程

1、on message

        on message作为一个功能异常强大的CAPL专有的的姑且叫它函数吧,它伴随很多人从初入测试行业到熟练与开发battle三小时不会累,可想而知的强大。首先我们来介绍它的第一种用法。

on message 0x101{  this.BRS =   this.FDF =  this.TYPE =   this.dlc =   this.byte(0) =     .    .    .  this.byte(n) =    }

        从上面我们能看到使用起来是相当的简单,而且Vector CAPL Browser自带联想功能,是你能够快捷、方便的进行编程,在on message 0x101中,其中on message你可以把它当成一个函数,0x101作为一个触发开关,一旦总线上出现0x101报文,就会进入到{}的内部(并且实时更新),就会执行内部的CAPL代码,我们可以在里面编写任意我们需要的功能。这里大家会注意到有一个特殊的单词this那他是什么意思呢?

        this在这里其实相当于一个message类型,这样说的话大家应该就很好理解,它就是总线上实时的一个message,内部包含了大量的信息,这里我就不再一一列举,在上面的代码中我列举了几个常用的参数,基本能够满足我们大部分时候的使用,实际这些参数都可以在CAN报文中找到,大家如果有兴趣也可以学习一下CAN报文的结构,是跟这里一一对应。下面我也列举一下。

       Vector-常用CAN工具 - CANoe入门到精通_04编辑

 Vector-常用CAN工具 - CANoe入门到精通_04编辑

 这里顺便再提下Rx和Tx:

Rx

Message was received (DIR == Rx)

Tx

Message was transmitted (DIR == Tx)

TXREQUEST

Transmission request was set for message (DIR == TXREQUEST)

下面我们说另外一种用法,就是将CAN1的报文全部转发到CAN2上,源码如下:(简单归类为通道识别,只要是某一CAN通道就会触发)

//The gateway should transmit all messages between Bus 1 and Bus 2 in both directions:on message CAN1.*{   message CAN2.* msg;   if(this.dir != rx) return; //important!   msg = this; output(msg);}
on message CAN2.*{ message CAN1.* msg; if(this.dir != rx) return; //important! msg = this; output(msg);}

第三种使用方法就是任意触发:

//将所有接收到得报文发送出去on message * {   output(this);};

        上面基本囊括了on message的大部分使用场景,至于内部填充和想要做什么大家可以自由发挥想象,实现你能想到的所有功能吧。

2、数据类型介绍

Scalar Data Types


  • Integers in form of
    byte (unsigned, 1 Byte)
    word (unsigned, 2 Byte)
    dword (unsigned, 4 Byte)
    int (signed, 2 Byte)
    long (signed, 4 Byte)
    int64(signed, 8 Byte)
    qword(unsigned, 8 Byte)


  • Individual character
    char (1 Byte)


  • Floating point numbers
    float (8 Byte)
    double (8 Byte)


常见变量定义之struct:

//struct 常见定义//onestruct Data {int type;long l;char name[50];};
//twostruct Point { int x; int y; }; struct Point myPoint;
struct { int first; int second; } pair;
//threevariables{ struct { int first; int second; } pair;}
on start{ pair.first = 1; pair.second = 2;}
//fourstruct Point { int x; int y; }; struct Point myPoint; struct Point allPoints[50];

常见变量定义之enum:

//Enumeration types are defined in CAPL in exactly the same way as in C:
enum Colors { Red, Green, Blue };
//enum State { State_Off = -1, State_On = 1 };

//If necessary, an enumeration type is implicitly converted to an integer type
int i; enum State state;state = State_Off;i = state + 1; // i == 0
//Enumeration types can be used especially in switch-case-statement:
switch (state) { case State_On: write("On"); break; case State_Off: write("Off"); break; default: write("Undefined"); break;}
/*For a conversion from an integer type to an enumeration type or between two different enumeration types a cast is necessary:*/
enum State state; enum Colors color;state = (enum State) 1; // state == State_Oncolor = (enum Color) state; // color == Green

        数组类型的我们就不在这里介绍了,都是跟C语言一样的,如果有需要度娘随便一搜都是一大把,我就不在这卖弄了。

这一块太多了,如果后续催更的人比较多的话,我就继续写吧!!!

原文始发于微信公众号(车载网络测试):Vector-常用CAN工具 – CANoe入门到精通_04

版权声明:admin 发表于 2022年12月11日 下午5:08。
转载请注明:Vector-常用CAN工具 – CANoe入门到精通_04 | CTF导航

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...