【发布时间】:2015-06-24 12:42:54
【问题描述】:
我正在尝试编写一个 Android Activity 来执行从 Arduino Duemilanove 读取数据。这个 Arduino 模型有一个 FTDI 232RL 芯片。我研究了以下网站和答案:
- setting parity with controlTransfer method
- http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html
- 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... =) 你帮了我很多