Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ISO C&x2B+;禁止在Arduino c串行通信中比较指针和整数[-fpPermission]错误_C++_C_Arduino_Arduino Ide - Fatal编程技术网

C++ ISO C&x2B+;禁止在Arduino c串行通信中比较指针和整数[-fpPermission]错误

C++ ISO C&x2B+;禁止在Arduino c串行通信中比较指针和整数[-fpPermission]错误,c++,c,arduino,arduino-ide,C++,C,Arduino,Arduino Ide,我正在为我的BB-8项目编码,我在Arduino上使用蓝牙,所以我使用: if (Serial.available() > 0) { state = Serial.read(); 大多数人通过这样的方式发送数字: if (state == '1') { 但是我想发送一个字符串,而不是一个数字,这样更容易: if (state == 'stop') { // or go etc. 但这似乎不起作用,所以我尝试使用字符串: if (state == "stop") { 但

我正在为我的BB-8项目编码,我在Arduino上使用蓝牙,所以我使用:

if (Serial.available() > 0) {
    state = Serial.read();
大多数人通过这样的方式发送数字:

if (state == '1') {
但是我想发送一个字符串,而不是一个数字,这样更容易:

if (state == 'stop') {    // or go etc.
但这似乎不起作用,所以我尝试使用字符串:

if (state == "stop") {
但是我得到了这个错误

ISO-C++禁止指针与整数[fPrime]

的比较 哪一个可行?如果两者都不可行,我该怎么办


谢谢。

首先,撇号代表的是字符而不是字符串,也就是说,
'x'
char
类型,而
“x”
char*
类型。正如在本问题中所讨论的,没有明确定义
'xyz'
的含义:

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;
const int max_len=20;
字符输入字符串[最大长度+1];//为字符串分配一些空间
尺寸指数=0;
while(Serial.available()>0)//除非
{
if(max_len<19)//比数组大小小一个
{
int input_num=Serial.read();//读取字符
input_string[index]=(char)input_num;//存储它
index++;//递增下一步写入的位置
input_string[index]='\0';//Null终止字符串
}
否则{
//所有数据读取,数据处理时间
打破
}
}
//别忘了你必须比较
//使用strcmp的字符串
如果(strcmp(inData,“停止”)==0){
//做点什么
}
//重置缓冲区,以便
//您可以读取另一个字符串
指数=0;

Char文本可以由多个字符组成。很有意思。但很明显,对于它们的作用或含义没有标准的定义。@KerrekSB但我相信用法是不可移植的,你需要看看你的编译器是如何处理它的。你当然不想将其与单个
读取的结果进行比较@TomášZato:好吧,标准定义了它们的含义是实现定义的…@TomášZato:区别在于,如果某个东西是实现定义的,需要实现它的文档。你确定这是链接到C?@ StAdGeWrar,我考虑删除标签,但是这个问题特别容易被C和C++程序员回答。