Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
Arduino String.indexOf()不工作_String_Bluetooth_Arduino_Char_Indexof - Fatal编程技术网

Arduino String.indexOf()不工作

Arduino String.indexOf()不工作,string,bluetooth,arduino,char,indexof,String,Bluetooth,Arduino,Char,Indexof,我已经做了好几个小时了。即使在串行监视器中打印字符时,indexOf()函数也不会返回>-1,并且字符串的长度随着字符的输入而增加 String command; void loop() { while ( bleuart.available() ) { char ch; ch = bleuart.read(); command += ch; Serial.print(ch

我已经做了好几个小时了。即使在串行监视器中打印字符时,indexOf()函数也不会返回>-1,并且字符串的长度随着字符的输入而增加

String command;    

void loop() {

        while ( bleuart.available() ) {
            char ch;
            ch = bleuart.read();
            command += ch;
            Serial.print(ch);
        }

        Serial.println(command.length());

        if(command.indexOf("\n") > -1 ) {
           Serial.println("command:");
           Serial.println(command);
         }

    }

@darc是正确的,但如果字符串来自ble模块,则字符串中不应该有实际的\n,我假设是这样。这应该是一个新行字符(DEC 10),它应该与相同

我在代码中看到3个不同的问题

  • 您的
    命令
    变量永远不会被清除,因此它只会增长到 内存不足。完成该命令后,调用
    command=”“
  • 构造命令字符串时,请使用
    command.concat(ch)而不是
    命令+=ch
  • 您应该将
    if(command.indexOf(“\n”)>-1)
    更改为
    if(command.indexOf(“\n”)!=-1)

  • 看看这是否有帮助。

    您是否在串行监视器中看到新行?或者只是一根长绳子\n是新行字符,只是一个长字符串-没有新行\n似乎什么也不做。如果在字符串中看到\n,则这实际上是“\\”+“n”字符。仅用于检查change indexof以查找“\\n”,并查看是否match@darc这成功了!我不太明白为什么。“\n”如何变成“\\n”?基本上,\是一个特殊字符,用于转义序列,例如“\n”代表新行。因此,如果要将\打印为字符串,则需要添加\以将其转义为“\\”,另一个示例是:假设您想在字符串中使用“您需要将其转义为\像这样的“\”。您可以在此处看到转义序列的完整列表“