我使用以下命令从串行端口获取字符串数据:
int k;
for(k=0;k<100;k++){
buf[0]=0;
HAL_UART_Receive(&huart2,buf,1, 100);
aRxBuffer[k]=buf[0];
}
HAL_UART_Transmit(&huart2,aRxBuffer, 100, 100);
这里正在阅读消息:
#TPS#TP10#47#100#1.02#3.3#1.04#
我想在两个“#”符号之间使用数据。例如; - TPS 是我的方法名称我想像这样使用这些数据
if(method=='TPS'){
dosomething();
}
47 是我的抵抗值。我将在一些计算中使用这个值。但它是字符串而不是整数。
如何比较 If 语句中的数据?
如何将数据从字符串转换为整数?
最佳答案
如果 # 是分隔符,您可以像这样解析标记:
char str[80] = "#TPS#TP10#47#100#1.02#3.3#1.04#";
const char s[2] = "#";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );
token = strtok(NULL, s);
}
要将字符串转换为数字,请使用 atoi 表示 int,使用 atof 表示 float。 有关这些工作的更多问题,只需在 google 上搜索并在 c/c++ 规范中找到它们的定义
关于c - 如何使用数组的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42202833/