CAPL是一種類C語言蚯撩,CAPL數(shù)據(jù)類型的定義很多C語言類似础倍,但也有很多獨特的地方。
CAPL數(shù)據(jù)類型包括基本類型胎挎、結(jié)構(gòu)體沟启、枚舉、關(guān)聯(lián)類型和對象類型犹菇。變量的數(shù)據(jù)類型決定了變量存儲占用的空間美浦。
基本類型
枚舉
枚舉變量的定義和使用同C語言:
enum State { State_Off = -1, State_On = 1 };
如果枚舉成員的值未定義,那么第一個成員默認(rèn)值為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);
}
關(guān)聯(lián)類型
CAPL支持一種類似Python字典和C++ Map的關(guān)聯(lián)類型(Associative Fields)浦辨,關(guān)聯(lián)類型的元素是鍵值對(key value pairs)。
關(guān)聯(lián)類型定義格式如下沼沈,左邊是value類型流酬,右邊[ ]內(nèi)是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:關(guān)聯(lián)浮點型
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:關(guān)聯(lián)字符串
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]);
}
結(jié)構(gòu)體
結(jié)構(gòu)的定義和使用同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中結(jié)構(gòu)體默認(rèn)按8字節(jié)對齊,可以在結(jié)構(gòu)體定義前加_align來改變結(jié)構(gòu)體對齊方式.
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
可以使用如下函數(shù)獲取結(jié)構(gòu)體大辛辛怼(size)芽腾、對齊方式(alignment )和偏移量(offset )信息:
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
對象類型
除了以上介紹的基礎(chǔ)數(shù)據(jù)類型艰躺,CAPL還提供了一些CANoe特有的對象類型來幫助用戶快速完成仿真測試功能的開發(fā)。
CAN messages
CAPL提供了各種網(wǎng)絡(luò)對應(yīng)的報文類眨八。本文以CAN message為例進(jìn)行介紹腺兴。
報文變量定義格式:
message + message ID/message name + variable
使用message關(guān)鍵字來聲明一個報文變量,message后是message ID或CANoe工程導(dǎo)入DBC后的message name,然后是在CAPL程序中要使用的報文變量名廉侧。
message 0xA m1; //定義一個ID為0xA的報文變量m1
message 100x m2; //定義一個ID為100的擴(kuò)展幀報文變量m2,ID后的x后綴表示這是一個擴(kuò)展幀
message EngineData m3; //定義一個在DBC中message name為EngineData的報文變量m3
...
output(m1);
output(m2);
output(m3);
CAPL提供了一系列的選擇器(Selectors)來設(shè)置或讀取CAN message的屬性页响,例如:
example:
message 0x100 msg; //定義一個ID為0x100的message變量msg
msg.CAN = 1; //將msg的通道設(shè)置為1
msg.DLC = 2; //將msg的DLC設(shè)置為2
msg.BYTE(0) = 0xAA; //給msg報文數(shù)據(jù)段的第一個byte賦值為0xAA;
msg.BYTE(1) = 0xBB; //給msg報文數(shù)據(jù)段的第二個byte賦值為0xBB;
output(msg); //將定義好的msg發(fā)送到總線中
定時器變量
CAPL提供兩種定時器變量:
timer:基于秒(s)的定時器
msTimer:基于毫秒(ms)的定時器
example:點擊鍵盤'a'后以20ms為周期發(fā)送id為100的報文
msTimer myTimer; //定義一個ms定時器myTimer
message 100 msg;
...
on key 'a' {
setTimer(myTimer,20); //點擊鍵盤'a'將定時器myTimer設(shè)置為20ms,并開始計時
}
...
on timer myTimer { //響應(yīng)定時器事件myTimer篓足,將msg發(fā)送到總線,
output(msg);
setTimer(myTimer,20); //重新設(shè)置定時器myTimer為20ms
}
未完待續(xù)~