【转载】CANoe 入门 Step by step系列(二)CAPL编程

时间:2023-03-09 05:58:42
【转载】CANoe 入门 Step by step系列(二)CAPL编程

来源:http://www.cnblogs.com/dongdonghuihui/archive/2012/09/26/2704619.html

CAPL就是Communication Application Programming Laguage的缩写,CAPL类似于C语言的语法,因此所有的语法请参考C语言教程,这里不在这里进行详述,关于C语言,毫无疑问的,首先推荐大家看谭浩强老师的书,经典中的经典,看完这本C语言应该没问题了。CAPL在CANoe中起到重要的作用,他将所有的部分联系起来(请看下图)现在我们给予上一节的基础上,来着重介绍CAPL编程。

【转载】CANoe 入门 Step by step系列(二)CAPL编程

CAPL语法是C语言的,又有一些C++的特性,this指针,事件等,对于事件的理解可以学习任意一种面对对象语言,首推C#.NET,可以参考我的博客学习,当然这个似乎比CAPL本身更加难,事件也更加多,但这并不妨碍对事件的理解,其中的事件类型如下图:

【转载】CANoe 入门 Step by step系列(二)CAPL编程

下面来几个小例子帮助理解

来个CANoe版本的hello world!来兴奋一小下吧。开整~

在打开CANoe,新建个工程,在Simulation Setup中加个Network node ,点铅笔,忘了吗,上一节刚讲过,右键Start->New,键入如下代码

【转载】CANoe 入门 Step by step系列(二)CAPL编程

编译后,关闭CAPL Brower。运行CANoe工程,结果如下,哇塞成功咯,好有成就感啊。

【转载】CANoe 入门 Step by step系列(二)CAPL编程

这个例子似乎跟CAN通讯没啥太大关系啊,好,我们接下来再做一个例子

【转载】CANoe 入门 Step by step系列(二)CAPL编程

运行结果如下,按键盘b键,将发送一个CAN消息,连dlc是啥都不知道的同学,推荐先学习一下CAN基础知识,推荐瑞萨公司的《CAN入门书》,讲的非常的好。

【转载】CANoe 入门 Step by step系列(二)CAPL编程

运行刚刚的那个例子你会发现,按一下b,只发送一条消息,但在实际应用中CAN消息都是循环连续发送的,我们要对刚刚的程序进行一些修改。完成这样的功能。程序如下:

variables
{
    message 0x400 msgA = {dlc=1};
    mstimer timerA;
    int conditionA = 0;
}

on key 'a'
{
    conditionA = !conditionA;
    if(conditionA ==1)
    {
        setTimer(timerA,200);
    }
}

on timer timerA
{
    if(conditionA==1)
    {
        setTimer(timerA,200);
    }
    msgA.byte(0) = msgA.byte(0)-1;
    output(msgA);
}

运行结果如下:按A键,Timer启动,发送消息

【转载】CANoe 入门 Step by step系列(二)CAPL编程

接下来我们一起来看总结一下CAPL编程的要点:

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

【转载】CANoe 入门 Step by step系列(二)CAPL编程

CAPL编程的学习,需要通过不断在实践中的积累,此外别无他法。以上真真儿的只是入门,如果你真心看过,不如你真心写过。