所以我需要用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);
}
}
}
我在这里的目标只是显示用函数块传输读取的字节数,但它没有显示任何内容。另外,我相信我必须使用controlTransfer函数,但是官方文档并没有解释关于这个函数的任何东西,它只是一个黑暗的引用。我见过像这个Android 4.0.3USB主机-通过controlTransfer发送数据和这个带有健康设备的串行到USB Android应用程序这样的帖子,但是不太清楚函数中每个参数的含义。有人知道我做错了什么并且/或对controTransfer函数有一个很好的解释吗?
发布于 2014-02-12 17:09:45
我终于能解决这个问题了。我正在与之接口的FPGA上的芯片是atmega32q4。这是一个CDC设备类,因此在此基础上,我必须找到合适的参数才能使函数controlTransfer()
工作。但是,我无法理解这个函数是如何工作的,或者在CDC设备上函数的用途是什么(例如: SET_LINE_CODING、GET_LINE_CODING等等)。然后,我找到了这链接,它为您提供了一组与controlTransfer函数一起使用的参数,并且它工作起来很有魅力。
下面是能够从芯片读取数据的代码:
package com.example.calentadorsolar;
import android.hardware.usb.*;
import android.os.Handler;
import android.os.Message;
public class Lectura_Solar implements Runnable
{
private UsbDeviceConnection connection;
private UsbEndpoint epIN;
private Handler handler;
private UsbInterface intf;
private Datos_Solar aux;
public Lectura_Solar(UsbDeviceConnection connection,UsbInterface intf,UsbEndpoint epIN, Handler handler)
{
this.connection = connection;
this.epIN = epIN;
this.handler = handler;
this.intf = intf;
aux = new Datos_Solar();
}
public void run()
{
connection.claimInterface(intf,true);
connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
byte datos [] = new byte[64];
while (true)
{
int data = connection.bulkTransfer(epIN, datos, datos.length, 5000);
try
{
Thread.sleep(500);
}
catch (Exception e){}
if (datos[0] == 0)
{
aux.temp1 = String.valueOf(datos[1]);
aux.temp2 = String.valueOf(datos[2]);
aux.temp3 = String.valueOf(datos[3]);
aux.temp4 = String.valueOf(datos[4]);
this.sendMessage(aux);//here we send the data to the main GUI
}
}
}
public void sendMessage(Datos_Solar message)
{
Message sms = new Message();
sms.obj = message;
handler.sendMessage(sms);
}
}
发布于 2013-11-22 13:03:08
嗯,你需要一个USB到串行接口设备来连接到你的Android主机,在FPGA方面,它是一个正常的串行接口。
您可以在其中一个页面上看到示例:
http://www.fpga4fun.com/SerialInterface.html或http://www.bealto.com/fpga-uart.html
发布于 2014-01-30 06:23:43
基本上,您不能从USB端口获取数据。这可能是由于以下几个原因:
解决方案
您不了解FPGA和硬件方面,如果您理解Labview代码仍然是可能的。还原工程师如何实现Labview代码,并在android上试用。
Labview程序不难理解。
https://stackoverflow.com/questions/20130337
复制相似问题