我实现了一个最小的 CAPL 脚本来测试我的想法:
variables {
char dataString[200] = "MAX_VOLT, MAX_CURR, MAX_TIME, TEMP, A0_MAX";
long pos1, pos2, pos3, pos4, pos5;
char dataArray[5][50];
}
on start {
int i;
pos1 = strstr(dataString, ",");
pos2 = strstr_off(dataString, pos1+1, ",");
pos3 = strstr_off(dataString, pos2+1, ",");
pos4 = strstr_off(dataString, pos3+1, ",");
pos5 = strlen(dataString);
write("pos1 = %d, pos2 = %d, pos3 = %d, pos4 = %d, pos5 = %d", pos1, pos2, pos3, pos4, strlen(dataString));
// get first string
for (i = 0; i < pos1; i++) {
dataArray[0][i] = dataString[i];
}
// get second string
for (i = 0 ; i < pos2 - pos1 - 3; i++) {
dataArray[1][i] = dataString[i + pos1 + 3];
}
// get third string
for (i = 0 ; i < pos3 - pos2 - 3; i++) {
dataArray[2][i] = dataString[i + pos2 + 3];
}
// get fourth string
for (i = 0 ; i < pos4 - pos3 - 3; i++) {
dataArray[3][i] = dataString[i + pos3 + 3];
}
// get fifth string
for (i = 0 ; i < pos5 - pos4 - 3; i++) {
dataArray[4][i] = dataString[i + pos4 + 3];
}
write("%s", dataArray[0]);
write("%s", dataArray[1]);
write("%s", dataArray[2]);
write("%s", dataArray[3]);
write("%s", dataArray[4]);
}
输出:
CAPL pos1 = 8, pos2 = 19, pos3 = 30, pos4 = 37, pos5 = 46
CAPL MAX_VOLT
CAPL MAX_CURR
CAPL MAX_TIME
CAPL TEMP
CAPL A0_MAX