如何轻松解析来自GSM模块的AT命令响应? [英] How to easily parse the AT command response from a GSM module?
本文介绍了如何轻松解析来自GSM模块的AT命令响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析Arduino中GSM模块的以下输出,以仅获得电压(3.900V
)部分.但是,我无法正常工作.
I'm trying to parse the following output taken from a GSM module in Arduino, to get the Voltage (3.900V
) part only. However, I can't get it to work.
"
+CBC: 0,66,3.900V
OK
"
我尝试了以下代码,但是失败了,甚至崩溃了.
I have tried the following code, but it fails and even crashes.
float getVoltage() {
if (atCmd("AT+CBC\r") == 1) {
char *p = strchr(buffer, ',');
if (p) {
p += 3; // get voltage
int vo = atof(p) ;
p = strchr(p, '.');
if (p) vo += *(p + 1) - '0'; // ??
return vo;
}
}
return 0;
}
如何以更好或更透明的方式完成此任务?
How can this be done in a better or more transparent way?
推荐答案
You can do it using the C function strtok
to tokenize the buffer
void setup() {
Serial.begin(115200);
char buffer[20] = "+CBC: 1,66,3.900V";
const char* delims = " ,V";
char* tok = strtok(buffer, delims); // +CVB:
tok = strtok(NULL, delims);
int first = atoi(tok);
tok = strtok(NULL, delims);
int second = atoi(tok);
tok = strtok(NULL, delims);
float voltage = atof(tok);
Serial.println(first);
Serial.println(second);
Serial.println(voltage);
}
void loop() {
}
这篇关于如何轻松解析来自GSM模块的AT命令响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文