4

我搜索了许多帖子,例如: Using Android to Communicate with a USB HID Device

但我仍然没有得到你如何确定controlTransfer调用中的requestType ?

public int controlTransfer (int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)

我需要为我的设备设置EVEN奇偶校验,但它似乎不起作用。这是我的代码:

        UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
        l("Device opened...");

        l("Trying to open interface on 0");
        UsbInterface deviceInterface = mDevice.getInterface(0);
        if (!conn.claimInterface(deviceInterface, true)) {
            l("Could not claim interface on 0");
            return;
        }

        int defaultDataBits = 8;
        int config = defaultDataBits;
        config |= (0x02 << 8); //even parity
        config |= (0x00 << 11); //stop bits

        conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
        conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
        conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
        conn.controlTransfer(0x40, 0x04, config, 0, null, 0, 0);// set even parity
        conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);// set 9600 baud rate

requestType 0x40 对我来说没有任何意义,一些示例将其设置为 0x21 或 0x81 或 0xA1 ...

获得正确 requestType 的最佳方法是什么?

我还应该提到,我希望在 PC 上接收具有偶校验的数据,如果我将 PC 上的串行端口的奇偶校验设置为 NONE - 我会收到预期的数据,所以我得出的结论是我对 controlTransfer 调用设备不工作。

这是我尝试从 Android 配置的 USB 转串口设备:

Device Info 
Device Path: /dev/bus/usb/001/002
Device Class: Use class information in the Interface Descriptors (0x0)
Vendor ID:  067b
Vendor Name:  Prolific Technology, Inc.
Product ID:  03ea


Interfaces 
    Interface #0 
    Class: Vendor Specific (0xff)
Endpoint: #0
    Address        : 129 (10000001)
    Number         : 1
    Direction      : Inbound (0x80)
    Type           : Intrrupt (0x3)
    Poll Interval  : 1
    Max Packet Size: 10
    Attributes     : 000000011
Endpoint: #1
    Address        : 2 (000000010)
    Number         : 2
    Direction      : Outbound (0x0)
    Type           : Bulk (0x2)
    Poll Interval  : 0
    Max Packet Size: 64
    Attributes     : 000000010
Endpoint: #2
    Address        : 131 (10000011)
    Number         : 3
    Direction      : Inbound (0x80)
    Type           : Bulk (0x2)
    Poll Interval  : 0
    Max Packet Size: 64
    Attributes     : 000000010

谢谢你的帮助。

4

1 回答 1

1

这是USB 规范的一部分,特别是 bmRequestType。这是 C 语言中的位掩码列表,您可以在项目中自己将这些定义为static final int's. 它们有时在 SDK 或驱动程序套件中围绕操作系统定义,或者您可以只指定原始十六进制字节(如上所述,但定义的位掩码便于阅读):

/* Setup Data Constants */

#define USB_SETUP_HOST_TO_DEVICE      0x00 // Device Request bmRequestType transfer direction - host to device transfer
#define USB_SETUP_DEVICE_TO_HOST      0x80 // Device Request bmRequestType transfer direction - device to host transfer

#define USB_SETUP_TYPE_STANDARD       0x00 // Device Request bmRequestType type - standard
#define USB_SETUP_TYPE_CLASS          0x20 // Device Request bmRequestType type - class
#define USB_SETUP_TYPE_VENDOR         0x40 // Device Request bmRequestType type - vendor

#define USB_SETUP_RECIPIENT_DEVICE    0x00 // Device Request bmRequestType recipient - device
#define USB_SETUP_RECIPIENT_INTERFACE 0x01 // Device Request bmRequestType recipient - interface
#define USB_SETUP_RECIPIENT_ENDPOINT  0x02 // Device Request bmRequestType recipient - endpoint
#define USB_SETUP_RECIPIENT_OTHER     0x03 // Device Request bmRequestType recipient - other

您需要为请求类型提供三个掩码

  • 方向
  • 实际请求类型
  • 收件人

在您的情况下,0x40 代表从 Android 主机到您连接的设备的供应商请求,设备本身作为接收者(相对于特定端点或接口)。如果您有多个接口,则需要指定 USB_SETUP_RECIPIENT_INTERFACE,然后在index参数中传递接口编号以将其放置到正确的位置。但是在您的情况下,接收者实际上将取决于您的设备对数据的预期方式,并且 Prolific 可能对此有一个规范。您可以尝试设置 0x01 位以查看它是否解决了您的问题,这可能是针对接口 0 的请求,但这可能并不重要,因为您的设备无论如何只有一个接口。

请注意,此参数完全特定于 USB,与您设备上的奇偶校验无关,但如果您没有正确获取这些数据包,那么您的设置将不会在设备上使用,如您所见。如果 USB 请求正确,则检查该config值。

于 2014-05-08T05:27:22.717 回答