【问题标题】:Android USB reading Arduino return 19600Android USB读取Arduino返回19600
【发布时间】:2015-06-24 12:42:54
【问题描述】:

我正在尝试编写一个 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 串行运行在 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();
        }*/
    }

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

坦克寻求帮助。

【问题讨论】:

  • 网络上有一些工作代码可以通过 USB 主机 API 与 Android 中的 FTDI 部分进行对话 - 查找和检查其中的一些比较可能会帮助您解决这个问题。您还需要记住,您最终可能会与引导加载程序而不是您的程序对话。
  • 谢谢@ChrisStratton... =) 你帮了我很多

标签: android arduino usb ftdi


【解决方案1】:

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

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

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 2013-03-12
    • 2011-06-17
    • 1970-01-01
    • 2021-07-12
    • 2014-06-20
    • 2012-11-03
    相关资源
    最近更新 更多