CANoe教程 | CAPL编程 - 数据类型

CAPL是一种类C语言,CAPL数据类型的定义很多C语言类似,但也有很多独特的地方。
CAPL数据类型包括基本类型、结构体、枚举、关联类型和对象类型。变量的数据类型决定了变量存储占用的空间。

基本类型

基本类型

枚举

枚举变量的定义和使用同C语言:

enum State { State_Off = -1, State_On = 1 };

如果枚举成员的值未定义,那么第一个成员默认值为1,之后的成员按顺序依次加1.
枚举变量的定义和使用

variables
{
  enum { Apple, Pear, Banana } fruit = Apple;
  enum Colors { Red, Green, Blue };
  enum Colors color;
}

enum Colors NextColor(enum Colors c)
{
  if (c == Blue) return Red;
  else return (enum Colors) (c + 1);
}

关联类型

CAPL支持一种类似Python字典和C++ Map的关联类型(Associative Fields),关联类型的元素是键值对(key value pairs)。
关联类型定义格式如下,左边是value类型,右边[ ]内是key类型:

int m[float];         // maps floats to ints
float x[int64];       // maps int64s to floats
char[30] s[ char[] ]  // maps strings (of unspecified length) to strings of length < 30

example 1:关联浮点型

float m[float];
m[4.1] = 5.5; //key is 4.1 (float)  and value is 5.5 (float)
m[5.3] = 6.6;
write ("4.1 is mapped to %2.2lf", m[4.1]);
write ("5.3 is mapped to %2.2lf", m[5.3]);
for (float mykey : m)
{
  write("%2.2lf is mapped to %2.2lf.", mykey, m[mykey]);
}

example 2:关联字符串

char[30] namen[char []];
strncpy(namen["Max"], "Mustermann", 30); 
strncpy(namen["Vector"], "Informatik", 30);

for (char[] mykey : namen)
{
  write("%s is mapped to %s", mykey, namen[mykey]);
}

结构体

结构的定义和使用同C:

variables
{
  struct Point
  {
    int x;
    int y;
  };
  struct Point myPoint;
  struct Point allPoints[50];
}
on start
{
  myPoint.x = 7;
  myPoint.y = 2;

  allPoints[3].x = 1;
  allPoints[3].y = 5;
}

注意:
CAPL中结构体默认按8字节对齐,可以在结构体定义前加_align来改变结构体对齐方式.
example:

struct Point {   // note: default _align(8)
byte x;          // offset 0, size 1
byte y;          // alignment 1, offset 1, size 1, padding before: 0
};               // size 2, alignment (of the struct) 1

struct LongPoint {   // note: default _align(8)
byte x;              // offset 0, size 1
qword y;             // alignment 8, offset 8, size 8, padding before: 7
};                   // size 16, alignment (of the struct) 8

_align(2) struct Point2 {
byte x;    // offset 0, size 1, (alignment 1)
qword y;   // alignment 2, offset 2, size 8, padding before: 1
};         // size 10, alignment (of the struct) 2

struct Points {   // note: _align(8) per default
struct Point p1;  // offset 0, size 2, (alignment 1)
byte x;           // alignment 1, offset 2, size 1, padding before: 0
struct Point2 p2; // alignment 2, offset 4, size 10, padding before: 1
};                // size 14, alignment (of the struct) 2

可以使用如下函数获取结构体大小(size)、对齐方式(alignment )和偏移量(offset )信息:


图片.png

example:

struct Points {   // note: _align(8) per default
Point p1;         // offset 0, size 2, (alignment 1)
byte x;           // alignment 1, offset 2, size 1, padding before: 0
Point2 p2;        // alignment 2, offset 4, size 10, padding before: 1
};                // size 14, alignment (of the struct) 2

__size_of(struct Points);         // returns 14
__alignment_of(struct Points);    // returns 2
__offset_of(struct Points, p1);   // returns 0
__offset_of(struct Points, x);    // returns 2
__offset_of(struct Points, p2);   // returns 4

对象类型

除了以上介绍的基础数据类型,CAPL还提供了一些CANoe特有的对象类型来帮助用户快速完成仿真测试功能的开发。

CAN messages

CAPL提供了各种网络对应的报文类。本文以CAN message为例进行介绍。
报文变量定义格式:

message + message ID/message name + variable

使用message关键字来声明一个报文变量,message后是message ID或CANoe工程导入DBC后的message name,然后是在CAPL程序中要使用的报文变量名。

message 0xA m1;                                //定义一个ID为0xA的报文变量m1
message 100x m2;                              //定义一个ID为100的扩展帧报文变量m2,ID后的x后缀表示这是一个扩展帧
message EngineData m3;                  //定义一个在DBC中message name为EngineData的报文变量m3
...
output(m1);
output(m2);
output(m3);

CAPL提供了一系列的选择器(Selectors)来设置或读取CAN message的属性,例如:


图片.png

图片.png

example:

message 0x100 msg;                  //定义一个ID为0x100的message变量msg
msg.CAN = 1;                              //将msg的通道设置为1
msg.DLC = 2;                              //将msg的DLC设置为2
msg.BYTE(0) = 0xAA;                 //给msg报文数据段的第一个byte赋值为0xAA;
msg.BYTE(1) = 0xBB;                 //给msg报文数据段的第二个byte赋值为0xBB;                 
output(msg);                                //将定义好的msg发送到总线中

定时器变量

CAPL提供两种定时器变量:
timer:基于秒(s)的定时器
msTimer:基于毫秒(ms)的定时器
example:点击键盘'a'后以20ms为周期发送id为100的报文

msTimer myTimer;                  //定义一个ms定时器myTimer
message 100 msg;
...
on key 'a' {
   setTimer(myTimer,20);       //点击键盘'a'将定时器myTimer设置为20ms,并开始计时
}
...
on timer myTimer {               //响应定时器事件myTimer,将msg发送到总线,
   output(msg);
   setTimer(myTimer,20);      //重新设置定时器myTimer为20ms 
}

未完待续~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。