stm32串口如何判断收到的数据AT\r\nOK\r\n中的OK
时间: 2023-12-07 19:39:43 浏览: 34
可以使用引用中提供的checkOK函数来判断收到的数据中是否包含OK。具体实现方法如下所示:
```c
char USART_ReceiveString[] = "AT\r\nOK\r\n"; // 接收到的串口数据
char match_txt[] = "OK"; // 需要匹配的字符串
if (checkOK(USART_ReceiveString, match_txt)) {
// 匹配成功,收到的数据中包含OK
// 进行相应的处理
} else {
// 匹配失败,收到的数据中不包含OK
// 可以进行其他处理
}
```
相关问题
用c语言截取出AT+EIMEI\r\n\r\n353259054574681\r\n\r\n\r\nOK这段字符串中的数字部分
以下是用C语言截取出AT+EIMEI\r\n\r\n353259054574681\r\n\r\n\r\nOK这段字符串中的数字部分的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "AT+EIMEI\r\n\r\n353259054574681\r\n\r\n\r\nOK";
char *p = str;
while (*p) {
if (*p >= '0' && *p <= '9') { // 判断是否为数字
printf("%c", *p);
}
p++;
}
return 0;
}
```
输出结果为:353259054574681
linux C语言从“\r\n1234567890\r\n\r\nOK\r\n” 提取字符串 1234567890
要从字符串“\r\n1234567890\r\n\r\nOK\r\n”中提取字符串“1234567890”,可以使用strtok()函数和strstr()函数的组合方法。首先使用strstr()函数找到字符串中包含特定内容“1234567890”的位置,然后使用strtok()函数分割字符串并获取特定内容后面的子串。
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "\r\n1234567890\r\n\r\nOK\r\n";
char *token = strtok(str, "\r\n"); // 以"\r\n"为分隔符分割字符串
while (token != NULL) {
if (strcmp(token, "1234567890") == 0) { // 找到需要提取的特定字符串
token = strtok(NULL, "\r\n"); // 继续分割字符串获取下一个子串
printf("%s", token);
break;
}
token = strtok(NULL, "\r\n");
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)