将 Arduino C 字符串转换为 Java 字符串会产生垃圾

Converting Arduino C string to Java String produces garbage

我正在通过蓝牙我的arduino设备我的android应用程序接收字节流。但是,当我将字节流转换为字符串时,会显示垃圾信息(即使我将 "UTF-8" 作为字符串构造函数中的最后一个参数传递)。

public void run() {
    int buffSize = 512;
    byte[] buffer = new byte[buffSize];  // buffer store for the stream
    int bytes; // bytes returned from read()

    // Keep listening to the InputStream until an exception occurs
   while (true) {
       Arrays.fill(buffer, (byte) 0);
        try {
            // Read from the InputStream
            bytes = mmInStream.read(buffer, 0, buffSize);
            // Send the obtained bytes to the UI activity

            Log.i(TAG, Arrays.toString(buffer));
            String message = new String(buffer, 0, bytes, "US-ASCII");
            updateLog("Received: " + message);
        } catch (IOException e) {
            updateLog("error reading: " + e.getMessage());
            break;
        }
    }
}

我正在使用的草图只是使用了arduino的SoftwareSerial库,它使用这种方法发送:https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp#L415-L468

这会将 uint8_ts 写入输出流。这是否意味着我需要告诉 java 将输入字节重新解释为 8 位无符号整数? java.

中不存在这样的原始数据类型

我要使用的草图是这个:https://github.com/stanleyhuangyc/Freematics/blob/master/firmware_v3/datalogger/datalogger.h

我出于调试目的修改了该应用程序,每次它循环并调用 delay(50) 时,它应该发送字符串 looping。您可以在应用程序中看到它获得了 l,但其余单词的 none。

如果我将缓冲区字节数组打印到 ADB 日志中,我得到的数组看起来像,但没有映射到 ascii 字符:

I/ConnectedThread: [-3, 108, -75, -85, -4, -22, -3, 108, -85, -4, -22, -3, 108, -85 , -4, -22, 108, -11, -85, -4, -86, -3, 108, -85, -4, -22, -3, 108, -85, -4, -22, - 3, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -11, -85, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -75, -4, - 22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -85, -4, -86, -3, 108, -4, -22, -7, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -11, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108, -11 , -85, -4, -86, -3, 108, -75, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108, -85, -4, -22, 108, -75, -4, -22, -3, 108, -85, -4, -22, -3, 108, -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -4, -22, -3, 108, -11, -85, -4, -22 , -3, 108, -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108 , -75, -85, -4, -22, -3, 108, -85, -4, -22, -3, -85, -4, -22, 108, -85, -4, -86, -3, 108, -85, -4, -22, - 7, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -11, -85, -4, -86, - 3, 108, -75, -4, -22, -3, 108, -85, -4, -22, -3, -85, -4, -22, -3, 108, -75, -85, -4, -86, -3, 108, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108 , -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -11, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108, -11, -85, -4, -86, -3, 108, -4, -22, -3, 108, -85, -4, -22 , -3, -11, -85, -4, -22, -3, 108, -85, -4, -22, 108, -4, -22, -3, 108, -75, -85, - 4, -22, -7, 44, -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -85, -4, -22, -3, 108, -11, -85, -4, -22, -3, 108, -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -85, -4, -22, -3, 108, -85, -4, -22, -7, -15, -85, - 4, -22, 108, -75, -85, -4, -86, -3, 108, -85, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -85, -4 , -22, -3, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -85, -4, -22, -3 , -75, -85, -4, -22, -3, 108, -85, -4, -86, -3, 108, -4, -22, -3, 108, -85, -4, - 22、-7、-15、-85、-4、-22、-3、108、-11、-85、-4、-86、-3、108、-11、-4、-22、-3 , 108, -11, -85, -4, -22, -3, -11, -85, -4, -22, -3, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -75, -85, -4, -22, -3, -85, -4, -22] I/ConnectedThread: [-3, 108, -75, -85, -4, -22, 108, -4, -22, -3, 108, -85, -4, -22, -3, 108 , -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -85, -4, -22, -3, 108, -11, - 85, -4, -22, -3, 108, -85, -4, -22, -3, 108, -11, -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -85, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22, 108, -85 , -4, -86, -7, 108, -15, -85, -4, -22, -3, 108, -85, -4, -22, -3, -11, -85, -4, -22、108、-11、-85、-4、-86、-3、108、-11、-4、-22、-3、108、-85、-4、-22、-3、-11 , -85, -4, -22, 108, -11, -85, -4, -86, -3, 108, -11, -4, -22, -3, 108, -85, -4, - 22, -3, -85, -4, -22, -3, 108, -75, -85, -4, -86, -3, 108, -4, -22, -3, 108, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ....]

这种输出会持续一段时间,然后我得到更短的缓冲区,如下所示:

I/ConnectedThread: [-22, 0, 0, 0, 0, 0, 0, 0, ...]

I/ConnectedThread: [-3, 108, -85, -4, 0, 0, 0, 0, 0, 0, ...]

I/ConnectedThread: [-22, 0, 0, 0, 0, ...]

原来我在 Arduino 端使用错误的波特率发送。它需要 38400 波特。