Android USB 读取 Arduino return 19600

Android USB reading Arduino return 19600

我正在尝试编写一个 Android Activity 来执行从 Arduino Duemilanove 读取数据。这个 Arduino 模型有一个 FTDI 232RL 芯片。我研究了以下网站和答案:

  1. setting parity with controlTransfer method
  2. http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html
  3. http://read.pudn.com/downloads181/sourcecode/embed/842049/usb/serial/ftdi_sio.h__.htm 等等...

我的代码可以很好地在 Android USB 设备上执行 controlTransfer 操作。 但是,在 bulkTransfer 上返回的数据只是 1 96 0... 我已经修改了波特率:Arduino serial runs on 57600 and the controlTransfer to. 关注我的一段代码:

        if(device != null){            
        UsbDeviceConnection conn = usbManager.openDevice(device);
        if (!conn.claimInterface(device.getInterface(0), true)) {
            return;
        }
        //configuring the usb device: 
        if(conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0) < 0){//reset
            Toast.makeText(getApplicationContext(), "Reset Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0, 0x01, 0, null, 0, 0) < 0){//clear Rx
            Toast.makeText(getApplicationContext(), "Clean RX Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0, 0x02, 0, null, 0, 0) < 0){//clear Tx
            Toast.makeText(getApplicationContext(), "Clean TX Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0) < 0 ){ //flow control none            
            Toast.makeText(getApplicationContext(), "Flow Control fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0) < 0){//baudrate 57600
            Toast.makeText(getApplicationContext(), "Baudrate fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0) < 0){ //data bit 8 parity none stop bit 1
            Toast.makeText(getApplicationContext(), "settings fail", Toast.LENGTH_LONG).show();
        }            

        Toast.makeText(getApplicationContext(), "Reading data...", Toast.LENGTH_LONG).show();            
        byte[] data = new byte[4096];
        if(conn.bulkTransfer(epIN, data, 4096, 5000) >= 0){
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                builder.append(data[i]);
            }                        
            Toast.makeText(getApplicationContext(), "Data: "+builder, Toast.LENGTH_LONG).show();            
            dispositivos.setText(builder);
        }            

        /*
        try{
            Thread.sleep(5000);                     
        }
        catch(InterruptedException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_LONG).show();
        }*/
    }

有什么想法或建议吗?

坦克求助。

我找到了一个库,可以帮助我执行 Android 与 Arduino Duemilanove 的通信。这个arduino模型有一个FTDI芯片(232R)。该芯片为USB UART接口。 然后,我正在使用这个库:

它帮助我以简单的方式在 Android 和 Arduino 之间进行通信,只需要几行代码。

谢谢。