capl:事件型语言
定时器代码
数据类型
capl运算符(一)
capl运算符(二)
逻辑非:两个条件同时成立则为真(1),否则为假(0).
逻辑与:只有一个条件成立则为真。
逻辑或: 后面的条件成立,为1,结果值则为0.
capl运算符(三)
补码表示法
注意:相当于对0进行减法,-1-1-8=-10,其中1为2的0次方,8为2的3次方。
capl中的事件
on message事件
必须有dbc数据库,才能进行this.信号名

on signal和on signal_update事件
例子:
on sysvar和on sysvar_update事件
新建一个系统变量
例子:
文件操作:读取文件
例子:
如果不知道capl中的函数含义,可以选择函数,点击帮助,则可查看参数含义。
0,2:字符串;1,3:二进制数据
文件操作:写入文件
0:覆盖写入;2:追加写入
例子:
练习1:
/*@!Encoding:936*/
//在这个编码下面,一个中文占两个字节
includes
{
}
variables//全局变量
{
int age =18;
struct Student {
char name[20];
byte age;
} std = {"嗡嗡嗡", 12};
struct Worker {
char name[20];
byte age;
} wrk;
message 0x60 msg1;
}
on key 'a'
{
int age=18;
float tall=1.81;
char sex='w';
char name[4]="wxx";
int score;
write("我是%s,今年%d,身高%f,性别%c",name,age,tall,sex);
write("score未赋值是:",score);
}
on key 'b'{
write("我今年%d岁",age);//需要设置age为全局变量
}
on key 'c'{
byte b=192;//byte占一个字节
word w=192;//word占两个字节
byte b1=255;
byte b2=256;
write("我今年%d岁",b1);
write("我今年%d岁",b2);
}
on key 'd'{//数组类型
int score[5]={1,2,3,4,5};
write("我今年%d岁",score[3]);
write("score的长度为%d",elCount(score));
}
on key 'e'{//结构类型
//方式1
struct Point
{
int x;
int y;
};//定义结构类型
struct Point p1;//声明未赋值
struct Point p2={x=3,y=7};//声明赋值
//输出结构变量成员
write("p1(%d,%d)",p1.x,p1.y);
write("p1(%d,%d)",p2.x,p2.y);
}
//方式2
on key 'z'{//结构类型
// 初始化结构体
write("姓名:%s,年龄:%d)",std.name,std.age);
}
//方式3
on key 'x'{//结构类型
// 初始化结构体
// 使用strncpy函数初始化name,注意strncpy不会自动添加字符串终止符'\0',因此需要手动确保字符串以'\0'结尾
strncpy(wrk.name, "嗡嗡嗡", 3); // 复制前3个字符
wrk.name[3] = '\0'; // 手动添加字符串终止符
wrk.age = 12; // 初始化age
write("姓名:%s,年龄:%d)",wrk.name,wrk.age);
}
on key 'v'{//结构类型
//enum Gender{Male,Female};
//write("男:%d %s",Male,Male.name());
//write("男+5:%d ",Male+5);
enum Direction {East,South,West,North} cdDir=West;
//enum Direction bjDir=North,syDir=South;
write("cdDir: %d %s",cdDir,cdDir.name());
}
on key 'q'{//结构类型
int ranking=2;
switch(ranking)
{
case 1:
write("金牌");
break;
case 2:
write("银牌");
break;
default:
write("继续努力;");
break;
}
}
on key 'p'
{
//msg1.
//setTimer();
//cancelTimer();
//deferStop(5000);
}
on preStart{
//message ACM_0x27E msg1={ACM_27E};
write("before");
}
on start{
write("ing");
}
on preStop{
write("总线最后的事件");
}
on stopMeasurement{
write("可以做一些统计得事情");
}
练习2:发送id为0x400-0x7FF的报文
/*@!Encoding:936*/
includes
{
}
variables
{
message 0x123 myMessage; // 声明一个message类型的变量,用于发送CAN报文
}
// 定义一个函数用于发送指定ID范围的报文
void sendMessagesInRange(dword startId, dword endId, byte byte0, byte byte1, byte byte2, byte byte3, byte byte4, byte byte5, byte byte6, byte byte7)
{
dword id;
for (id = startId; id <= endId; id++)
{
myMessage.id = id;
myMessage.dlc = 8;
myMessage.byte(0) = byte0;
myMessage.byte(1) = byte1;
myMessage.byte(2) = byte2;
myMessage.byte(3) = byte3;
myMessage.byte(4) = byte4;
myMessage.byte(5) = byte5;
myMessage.byte(6) = byte6;
myMessage.byte(7) = byte7;
output(myMessage);
}
}
on start
{
// 这里可以放置启动时需要执行的代码
}
on key 'a' // 当大写字母A被按下时
{
sendMessagesInRange(0x00, 0x437, 0x03, 0x06, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00);
}
on key 'b' // 当大写字母B被按下时
{
sendMessagesInRange(0x437, 0x4FF, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00);
}
on key 'c' // 当大写字母C被按下时
{
sendMessagesInRange(0x600, 0x7FF, 0x04, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00);
}
on key 'd' // 当大写字母C被按下时
{
sendMessagesInRange(0x600, 0x6FF, 0x04, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00);
}