Android USB 主机 API : bulk transfer buffer size

标签 android usb host

我正在编写软件在平板电脑之间进行通信(摩托罗拉 Xoom Android 版本 4.0.3 和内核版本 2.6.39.4)和使用 USB 主机 API 的外围设备通过安卓。我只使用两种通信方式:

  • 控制:controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
  • 批量:bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout)

控制传输工作正常,但批量传输有问题。我只能使用 32768 作为 bulkTransfer 函数的缓冲区大小。少用或多用是不可能的。我知道由于缓冲管道的限制(大小:32769 字节)我不能使用更多。

此外围设备流式传输 bulkTranfer 函数未正确读取的数据。我想有些数据丢失了。

我找到这个:In Linux If a process attempts to read from an empty pipe (buffer), then read(2) will block until data is available. If a process attempts to write to a full pipe , then write(2) blocks until sufficient data has been read from the pipe to allow the write to complete.

基于此,我对问题的解释是,由于 write(2) 函数生成的阻塞标志,某些数据未写入管道(缓冲区)。我对么?如果这是真的,我可以更改管道缓冲区。

  1. 我对这个问题的第一个解决方案是更大的缓冲区。 For kernel >= 2.6.35, you can change the size of a pipe with fcntl(fd, F_SETPIPE_SZ, size)但是我怎样才能找到 fd (文件描述符) USB 管道?
  2. 第二个选项是使用 ulimit -p SIZE 但我的内核的参数 p 不是管道而是进程。

有没有人遇到同样的问题,有什么解决办法吗?

最佳答案

你应该得到一个 USB 数据分析器,我正在使用这个:http://www.ellisys.com/products/usbex200/index.php

当我做同样类型的事情时,使用类似的东西确实帮助了我,我发现你必须做某种类型的 while 循环。

对于我的设备,我有 64 字节的数据通过数据包发送给我,数据包将是两个控制字节,62 字节用于数据,所以对于我的传输,我必须做类似的事情

StringBuilder sb = new StringBuilder();
while(bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout) > 2){
    for(int i = 2; i < buffer.length(); i++){
        sb.append((char) buffer[i]);
    }
}

这些行对我很有用,我遇到了完全相同的问题,这就是我解决它的方法。如果您需要,我有更多信息。只是评论:)。我知道这让我很沮丧。我使用的是 Acer Iconia A500 和 Android 4.0.3 btw


Transferring larger amounts of data USB

Transferring data USB

关于Android USB 主机 API : bulk transfer buffer size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889461/

相关文章:

android - 找不到 ActivityMainBindingImpl

windows - Windows 10上的USB-MIDI驱动程序

eclipse - 尝试连接到 Eclipse 插件存储库时出现未知主机错误

Android AVD 无法通知 JmDNS 服务

java - SQLite 数据库读取初始化

c++ - 是否有 C++ 跨平台 USB 库?

java - 线程 "main"java.net.UnknownHostException 中出现异常

php - 使用 php 将 mysql 数据库连接到主机

android - 键入时执行搜索

linux - 使用 udev-rules 在没有 sudo 的情况下调用 libusb