android usb bulktransfer,Android USB无法通过bulkTransfer读取完整数据

我正在使用Android USB Host API使用FT232发送和接收数据。我使用两个线程发送部分和阅读部分。我可以发送和接收数据,但读取的数据与数据发送的数据不相等。例如,当我发送字节[1,2,3,4,5]。读取的数据有时是字节[1,2,5]。有时可以读取5个字节,但有时会丢失一些字节。附件是我正在使用的代码。Android USB无法通过bulkTransfer读取完整数据

设置部分:

HashMap list = mUsbManager.getDeviceList();

Iterator iterator = list.values().iterator();

while(iterator.hasNext()) {

UsbDevice device = iterator.next();

if (device.getInterfaceCount() == 1) {

mInterface = device.getInterface(0);

for (int i = 0; i < mInterface.getEndpointCount(); i++) {

if (mInterface.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {

if (mInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) {

mEndpointIn = mInterface.getEndpoint(i);

} else {

mEndpointOut = mInterface.getEndpoint(i);

}

}

}

}

}

}

}

发送部分:

UsbDeviceConnection connection = mUsbManager.openDevice(device);

if (connection == null) {

Log.e(TAG, "Connection terminated");

return;

}

byte[] bytes = new byte[1, 2, 3, 4, 5];

boolean claimed = connection.claimInterface(mInterface, true);

if (claimed) {

connection.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0); // baud rate 57600

connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); // 8-N-1

int sentLength = connection.bulkTransfer(mEndpointOut, bytes, bytes.length, 100);

connection.releaseInterface(mInterface);

connection.close();

}

阅读部分:

UsbDeviceConnection connection = mUsbManager.openDevice(device);

if (connection == null) {

Log.e(TAG, "Connection terminated");

return;

}

byte[] inData = new byte[64];

boolean claimed = connection.claimInterface(mInterface, true);

if (claimed) {

connection.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0); // baud rate 57600

connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); // 8-N-1

int readLength = connection.bulkTransfer(mEndpointIn, inData, inData.length, 100);

connection.releaseInterface(mInterface);

connection.close();

// 'result' is the data I want, the first 2 bytes are status bytes so being removed

byte[] result = new byte[inData.length - 2];

System.arrayCopy(inData, 2, result, 0, inData.length - 2);

}

2016-06-08

chengsam

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值