Arduino String.indexOf()无法正常运行

时间:2018-08-15 06:21:34

标签: string bluetooth arduino char indexof

在此工作了几个小时。即使我在串行监视器中看到字符打印,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);
         }

    }

1 个答案:

答案 0 :(得分:0)

@darc是正确的,但是如果字符串来自ble模块(我认为),那么实际上不应该在字符串中包含实际的\ n。这应该是换行符(DEC 10),\ n应该与该换行符相同。

我在您的代码中发现了3个不同的问题。

  1. 您的File Found Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198) at java.base/java.lang.Thread.run(Thread.java:844) Caused by: java.io.FileNotFoundException: /files/Employees.xlsx (No such file or directory) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:220) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:113) at applicationSettings.openExcelSheet(applicationSettings.java:32) at loginScreen.start(loginScreen.java:70) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) Exception running application loginScreen变量不会被清除,因此它将一直增长到 您的内存不足。完成命令后,调用 android{ ... sourceSets{ main{ java.srcDirs = ['src', 'heroes', 'villains'] } } }
  2. 构造命令字符串时,请使用command代替command = "";
  3. 您应将command.concat(ch);更改为command += ch;

看看是否有帮助。