Android USB读取Arduino返回19600

时间:2015-04-17 15:09:51

标签: android arduino usb ftdi

我正在尝试编写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 等等...
  4. 我的代码可以在Android USB设备上执行controlTransfer操作。 但是,在bulkTransfer上,返回的数据只有1 96 0 ...... 我已经修改了波特率:Arduino串口运行在57600上,而controlTransfer运行到。 请关注我的代码:

            if(device != null){            
            UsbDeviceConnection conn = usbManager.openDevice(device);
            if (!conn.claimInterface(device.getInterface(0), true)) {
                return;
            }
            //configuring the usb device: https://stackoverflow.com/questions/8546099/setting-parity-with-controltransfer-method
            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();
            }*/
        }
    

    有人有什么想法或建议吗?

    坦克寻求帮助。

1 个答案:

答案 0 :(得分:1)

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

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

感谢。