![](/img/trans.png)
[英]ARDUINO: ISO C++ forbids comparison between pointer and integer [-fpermissive]
[英]ISO C++ forbids comparison between pointer and integer [-fpermissive] error in Arduino c serial communication
我正在编写我的 BB-8 项目,我在 Arduino 上使用蓝牙,所以我使用:
if (Serial.available() > 0) {
state = Serial.read();
大多数人通过这样的方式发送数字:
if (state == '1') {
但我想发送一个字符串,而不是一个数字,以使其更容易,如下所示:
if (state == 'stop') { // or go etc.
但这似乎行不通,所以我尝试使用字符串:
if (state == "stop") {
但我收到这个错误
ISO C++ 禁止指针和整数之间的比较 [-fpermissive]
哪一个可行,如果都没有,我应该怎么做?
谢谢你。
首先,撇号用于char 文字而不是 strings ,即'x'
是char
类型,而"x"
是char*
类型。 没有明确定义'xyz'
含义,如本问题所述: https : //stackoverflow.com/a/3961219/607407
Serial.read
返回的值是int
类型。 所以在这种情况下:
if (state == "stop")
您正在将int
与const char*
进行比较。 相反,您可能想要读取一个字符串并进行比较。 这是从串行读取arduino上的字符串的示例:
const int max_len = 20;
char input_string[max_len+1]; // Allocate some space for the string
size_t index = 0;
while(Serial.available() > 0) // Don't read unless
{
if(max_len < 19) // One less than the size of the array
{
int input_num = Serial.read(); // Read a character
input_string[index] = (char)input_num; // Store it
index++; // Increment where to write next
input_string[index] = '\0'; // Null terminate the string
}
else {
// all data read, time to data processing
break;
}
}
// Don't forget that you have to compare
// strings using strcmp
if(strcmp(inData, "stop") == 0) {
// do something
}
// reset the buffer so that
// you can read another string
index = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.