Android 通过USB传输数据
我正在尝试通过USB发送和接收数据,我的设备Acer Iconia A500拥有连接到设备所需的一切,一切正常,但当我尝试发送和接收数据时,它的行为并不符合预期。这是我的密码Android 通过USB传输数据,android,usb,ftdi,Android,Usb,Ftdi,我正在尝试通过USB发送和接收数据,我的设备Acer Iconia A500拥有连接到设备所需的一切,一切正常,但当我尝试发送和接收数据时,它的行为并不符合预期。这是我的密码 for( ; ; ) { //this is the main loop for transferring String get = "$getPos"; byte[] getBytes = get.getBytes(); conn.bulkTransfer( epOUT, getBytes,
for( ; ; ) { //this is the main loop for transferring
String get = "$getPos";
byte[] getBytes = get.getBytes();
conn.bulkTransfer( epOUT, getBytes, getBytes.length, 500 );
try {
Thread.sleep( 500 );
byte[] buffer = new byte[4096];
conn.bulkTransfer( epIN, buffer, 4096, 500 );
StringBuilder byStr = new StringBuilder();
for( int i = 0; i < buffer.length; i++ ) {
if( buffer[i] != 0 ) {
byStr.append( buffer[i] + ", " );
}
}
l( byStr );
}
catch( InterruptedException e ) {
e.printStackTrace();
}
if( mStop ) {
mStopped = true;
return;
}
l( "sent " + counter );
counter++;
counter = (byte)( counter % 16 );
}
for(;;){//这是用于传输的主循环
字符串get=“$getPos”;
byte[]getBytes=get.getBytes();
conn.bulkTransfer(epOUT,getBytes,getBytes.length,500);
试一试{
睡眠(500);
字节[]缓冲区=新字节[4096];
连接传输(epIN,缓冲器,4096500);
StringBuilder byStr=新建StringBuilder();
for(int i=0;i
它的意思是返回一个大约80个字符长的字节数组,但它只返回两个字节,即1和96,如果USB设备端出现错误,它仍然会返回比2多几个字节。我的代码是否接近正确?我基于serverbox的USB转串行文章。我试图以错误的波特率发送数据 下面是有效的代码。为所有使用FTDI设备并需要帮助的人发布
private Runnable mLoop = new Runnable() {
@Override
public void run() {
UsbDevice dev = sDevice;
if (dev == null)
return;
UsbManager usbm = (UsbManager) getSystemService(USB_SERVICE);
UsbDeviceConnection conn = usbm.openDevice(dev);
l("Interface Count: " + dev.getInterfaceCount());
l("Using "
+ String.format("%04X:%04X", sDevice.getVendorId(),
sDevice.getProductId()));
if (!conn.claimInterface(dev.getInterface(0), true))
return;
conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
// mConnection.controlTransfer(0×40,
// 0, 1, 0, null, 0,
// 0);//clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);// flow
// control
// none
conn.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0);// baudrate
// 57600
conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);// data bit
// 8, parity
// none,
// stop bit
// 1, tx off
UsbEndpoint epIN = null;
UsbEndpoint epOUT = null;
byte counter = 0;
UsbInterface usbIf = dev.getInterface(0);
for (int i = 0; i < usbIf.getEndpointCount(); i++) {
l("EP: "
+ String.format("0x%02X", usbIf.getEndpoint(i)
.getAddress()));
if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
l("Bulk Endpoint");
if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
epIN = usbIf.getEndpoint(i);
else
epOUT = usbIf.getEndpoint(i);
} else {
l("Not Bulk");
}
}
for (;;) {// this is the main loop for transferring
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String get = "$fDump G" + "\n";
l("Sending: " + get);
byte[] by = get.getBytes();
// This is where it sends
l("out " + conn.bulkTransfer(epOUT, by, by.length, 500));
// This is where it is meant to receive
byte[] buffer = new byte[4096];
StringBuilder str = new StringBuilder();
if (conn.bulkTransfer(epIN, buffer, 4096, 500) >= 0) {
for (int i = 2; i < 4096; i++) {
if (buffer[i] != 0) {
str.append((char) buffer[i]);
} else {
l(str);
break;
}
}
}
// this shows the complete string
l(str);
if (mStop) {
mStopped = true;
return;
}
l("sent " + counter);
counter++;
counter = (byte) (counter % 16);
}
}
};
private Runnable mLoop=new Runnable(){
@凌驾
公开募捐{
UsbDevice dev=sDevice;
如果(dev==null)
返回;
UsbManager usbm=(UsbManager)getSystemService(USB_服务);
UsbDeviceConnection conn=usbm.openDevice(dev);
l(“接口计数:+dev.getInterfaceCount());
l(“使用”
+String.format(“%04X:%04X”,sDevice.getVendorId(),
sDevice.getProductId());
if(!conn.claiInterface(dev.getInterface(0),true))
返回;
conn.controlTransfer(0x40,0,0,0,null,0,0);//重置
//mConnection.controlTransfer(0×40,
//0,1,0,null,0,
//0);//清除Rx
conn.controlTransfer(0x40,0,2,0,null,0,0);//清除发送
conn.controlTransfer(0x40,0x02,0x0000,0,null,0,0);//流
//控制
//没有
conn.controlTransfer(0x40,0x03,0x0034,0,null,0,0);//波特率
// 57600
conn.controlTransfer(0x40,0x04,0x0008,0,null,0,0);//数据位
//8、平价
//没有,,
//停止位
//1,发射关闭
UsbEndpoint epIN=null;
UsbEndpoint epOUT=null;
字节计数器=0;
UsbInterface usbIf=dev.getInterface(0);
对于(int i=0;i=0){
对于(int i=2;i<4096;i++){
如果(缓冲区[i]!=0){
str.append((char)缓冲区[i]);
}否则{
l(str);
打破
}
}
}
//这显示了完整的字符串
l(str);
如果(mStop){
mStopped=true;
返回;
}
l(“已发送”+计数器);
计数器++;
计数器=(字节)(计数器%16);
}
}
};
没问题。我做这件事已经有一段时间了。你能包括你的进口声明吗?