【发布时间】:2013-12-06 11:25:27
【问题描述】:
所以我需要在 Android 中编写一个必须能够读取来自 FPGA SPARTAN 6 的数据的应用。
我的应用程序能够识别 USB 何时连接,并且可以通过该接口获取有关接口和端点的信息。但是它需要读取有人在按下按钮时发送的数据,这就是应用程序失败的时候。
我的代码如下:
package com.example.usb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity
{
private UsbManager usbManager;
private UsbDevice device;
private TextView tv;
Handler handler = new Handler()
{
public void handleMessage(Message m)
{
Integer datos = (Integer)m.obj;
tv.setText(datos + "\n");
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
}
@Override
protected void onResume()
{
super.onResume();
Intent intent = getIntent();
String action = intent.getAction();
if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
{
//conectar dispositivo
device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
//muestraDatos(device);
UsbInterface intf = device.getInterface(1);
UsbEndpoint epIN = getIN(device);
//tv.setText(epIN.getDirection() + " " + epIN.getType());
UsbDeviceConnection connection = usbManager.openDevice(device);
new Thread(new Read(connection,intf,epIN,handler)).start();
}
}
private UsbEndpoint getIN(UsbDevice device)
{
UsbInterface intf = device.getInterface(1);
UsbEndpoint ep = null;
int numep = intf.getEndpointCount();
for (int i = 0 ; i < numep ; i++)
{
UsbEndpoint aux = intf.getEndpoint(i);
if (aux.getDirection() == UsbConstants.USB_DIR_IN && aux.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
ep = aux;
}
}
return ep;
}
而线程读取(应该从 FPGA 读取)是:
package com.example.usb;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.os.Handler;
import android.os.Message;
public class Read implements Runnable
{
private UsbDeviceConnection connection;
private UsbEndpoint epIN;
private Handler handler;
private UsbInterface intf;
public Read(UsbDeviceConnection connection,UsbInterface intf,UsbEndpoint epIN,Handler handler)
{
this.connection = connection;
this.epIN = epIN;
this.handler = handler;
this.intf = intf;
}
public void run()
{
byte datos[] = new byte[50];
int read_data = 0;
if (connection.claimInterface(intf,true))
{
/*Message sms = new Message();
sms.obj = "Success caliming interface: " + intf.getEndpointCount();
handler.sendMessage(sms);*/
//connection.controlTransfer(0x00000080, 0x03, 0x4138, 0, null, 0, 0);
while (true)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e){}
read_data = connection.bulkTransfer(epIN, datos, datos.length, 100);
datos = new byte[50];
//if (read_data > -1)
//{
Message sms = new Message();
sms.obj = read_data;
handler.sendMessage(sms);
//}
}
}
else
{
Message sms = new Message();
sms.obj = "Fail claiming interface ";
handler.sendMessage(sms);
}
}
}
我的目标只是显示使用函数 bulktransfer 读取的字节数,但它不显示任何内容。另外,我相信我必须使用 controlTransfer 函数,但官方文档没有解释任何关于该函数的内容,它只是一个黑暗的参考。我看过像Android 4.0.3. USB Host - sending data via controlTransfer和Serial to USB Android application with health device这样的帖子,但是函数中每个参数的含义并不是很清楚。有人知道我做错了什么和/或对 controTransfer 函数有很好的解释吗?
【问题讨论】:
-
您首先要更好地了解确切您的硬件设备需要哪些操作 - 这不会是没有文档文档的人会做的事情您正在使用的特定设备将知道,除非您能够说它实现了某些特定的众所周知的方案。你真的是直接在 FPGA 中实现 USB 引擎(可能但不常见)还是使用 USB 接口芯片?
-
嗯,我确实没有在 FPGA 中实现任何东西。 FPGA 应该发送一串位,我需要捕获这些位。拥有 FPGA 的人在 labview 中实现了一个读取程序,他可以从中读取 FPGA 发送的内容。我对labview或电子学一无所知,我只是使用Android附带的主机API,但同样,它不起作用。而且我不知道硬件设备需要什么样的操作,我完全迷失在这里,这就是为什么我要帮忙。谢谢。
-
在您提供自定义硬件的全面文档之前,没有人可以帮助您 - 您在上面的回复中完全忽略了这些部分。您必须向开发板/fpga 开发人员询问有关其 USB 接口的信息,否则您将无法将开发板与 Android 或任何其他 USB 主机一起使用。
-
您需要询问已实现的 USB 接口的完整规格 - 编写驱动程序所需的一切。否则,您将无法知道必须将哪些数据移入或移出哪些端点才能真正完成任何事情。
-
张贴你所做的事情作为你自己的答案,然后接受它。您可能需要等待几天才能这样做,不确定。