因此,我需要在 Android 中编写一个应用程序,该应用程序必须能够读取来自 FPGA SPARTAN 6 的数据。
我的应用程序能够识别 USB 何时连接,并且可以获取有关接口(interface)和该接口(interface)上的端点的信息。但随后它需要读取某人在按下按钮时发送的数据,这就是应用程序失败的时候。
我的代码如下:
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 函数有一个很好的解释吗?
最佳答案
那么,你需要有一个USB转串口的接口(interface)设备来连接你的Android主机,在FPGA端,它是一个普通的串口。
您可以在以下页面之一查看示例:
http://www.fpga4fun.com/SerialInterface.html或者 http://www.bealto.com/fpga-uart.html
关于java - USB主机传输Android与Spartan 6 FPGA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130337/