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

时间:2016-01-25 05:52:44

标签: java android bluetooth arduino c-strings

我通过蓝牙从我的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;
        }
    }
}

我使用的草图只使用SoftwareSerial库来代替arduino,它使用此方法发送:https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp#L415-L468

这会将uint8_t写入输出流。这是否意味着我需要告诉java将输入字节重新解释为8位无符号整数? java中不存在这种原始数据类型。

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

我已修改应用程序以进行调试,每次循环并调用delay(50)时,都应发送字符串looping。您可以在应用中看到它获得l,但没有其他任何内容。

如果我将缓冲区字节数组打印到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,-1 1,-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 ....]

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

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,...]

garbage

1 个答案:

答案 0 :(得分:0)

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