【CAPL】CAPL 的常用函数:时间、消息、诊断、字符串数组等

CAPL中常用函数按功能可分为时间处理、消息/信号操作、诊断、字符串处理、系统交互等类别,以下是高频使用的函数及示例:
这些函数覆盖了CAPL的核心应用场景
其中timeNow()setTimer()output()write()$信号名是日常使用频率最高的
掌握它们能满足大部分总线测试和仿真需求。

一、时间相关函数

函数名 功能说明 示例
timeNow() 返回当前系统时间(毫秒级时间戳) dword t = timeNow();
setTimer(t,ms) 启动/重置秒级定时器(timer类型) timer t; setTimer(t, 5); // 5秒后触发
setTimerCyclic(t,ms) 启动周期性秒级定时器 setTimerCyclic(t, 1); // 每秒触发一次
setTimer(msT,ms) 启动/重置毫秒级定时器(msTimer类型) msTimer mt; setTimer(mt, 100); // 100ms后触发
cancelTimer(t) 终止定时器(支持timermsTimer cancelTimer(mt);

二、消息与信号操作函数

函数名 功能说明 示例
output(msg) 发送CAN/LIN消息 message 0x123 msg; output(msg);
this 在事件中表示当前对象(消息/信号) on message 0x123 { write(this.id); }
$信号名 | 读取/修改全局信号值 | $VehicleSpeed = 80; // 赋值信号
msg.byte(n) 访问消息的第n个字节(0开始) msg.byte(0) = 0xAA;
msg.dlc 获取/设置消息的数据长度(DLC) msg.dlc = 8;
signalGet() 读取信号值(更灵活的方式) float speed = signalGet("VehicleSpeed");

三、诊断相关函数(UDS)

函数名 功能说明 示例
diagSendRequest(req) 发送诊断请求 diagRequest UDS.ReadDTCs req; diagSendRequest(req);
diagWaitResponse(req, timeout) 等待诊断响应(超时时间ms) if(diagWaitResponse(req, 2000) == 0) { ... }
diagGetLastError() 获取诊断操作的错误码 dword err = diagGetLastError();

四、字符串与转换函数

函数名 功能说明 示例
write(...) 输出信息到控制台 write("时间:", t, "ms");
sprintf(buf, fmt, ...) 格式化字符串 char buf[50]; sprintf(buf, "转速:%d", 3000);
atoi(str) 字符串转整数 int num = atoi("123");
itoa(num, str) 整数转字符串 char str[10]; itoa(123, str);
strlen(str) 获取字符串长度 int len = strlen("hello"); // 返回5

五、系统与文件操作函数

函数名 功能说明 示例
sysGetTimeString() 获取当前时间的字符串(如"15:30:45") write(sysGetTimeString());
fileOpen(name, mode) 打开文件(mode:"r"读/"w"写) long f = fileOpen("log.txt", "w");
fileWrite(f, data) 写入文件 fileWrite(f, "test");
fileClose(f) 关闭文件 fileClose(f);
keyState(key) 检查按键状态(返回1表示按下) if(keyState('s') == 1) { ... }

六、数组与数学函数

函数名 功能说明 示例
arraysize(arr) 获取数组长度 int arr[5]; write(arraysize(arr)); // 返回5
rand() 生成0~32767的随机整数 int r = rand();
abs(x) 取绝对值 int a = abs(-5); // 返回5
pow(x, y) 计算x的y次方 float p = pow(2, 3); // 返回8.0

其他:message的常用属性

属性名 含义与用途 类型 示例
id 消息的标识符(ID),可读写 dword msg.id = 0x123;(设置ID为0x123)
dlc 数据长度码(Data Length Code),表示消息包含的字节数(08或064,取决于总线类型),可读写 byte msg.dlc = 8;(设置数据长度为8字节)
byte(n) 访问消息的第n个数据字节(n从0开始),可读写 byte msg.byte(0) = 0xAA;(设置第1个字节)
flags 消息的标志位(如远程帧、扩展帧等),可读写 dword msg.flags = 0x01;(设置为扩展帧)
channel 消息所在的总线通道(多通道时使用),只读 word write("通道:", msg.channel);
time 消息的时间戳(接收或发送时间,毫秒级),只读 dword write("时间:", msg.time);
dir 消息方向(0=接收,1=发送),只读 byte if(msg.dir == 1) { ... }

说明:

    1. id:最核心的属性之一,用于标识消息(如CAN消息的标准ID或扩展ID)。
    1. dlcbyte(n):配合使用
      • dlc定义数据长度,byte(n)操作具体字节(n的范围必须小于dlc,否则可能出错)。
    1. flags:包含总线特定的标志,例如:
      • CAN消息中,0x01表示扩展帧(29位ID),0x02表示远程帧(RTR)。
    1. 只读属性channeltimedir通常用于消息接收事件中,获取消息的元数据(如on message 0x123 { write(msg.time); })。

通过这些属性,可以完全控制消息的结构、内容和传输特征,是CAPL中处理总线消息的基础。

END

posted @ 2025-08-08 14:17  anliux  阅读(585)  评论(0)    收藏  举报