Android与USB到串口设备的通信和controlTransfers

时间:2014-05-07 02:52:54

标签: android serial-port usb usbserial parity

我搜索了很多这样的帖子: 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的最佳方法是什么?

我还应该提一下,我希望通过EVEN奇偶校验在PC上接收数据,如果我将PC上串行端口的奇偶校验设置为NONE,我会收到预期数据,所以我得出的结论是,我对设备的控制传输不能正常工作。

这是我尝试从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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是USB spec的一部分,特别是bmRequestType。这是C中的位掩码列表,您可以在项目中将其自定义为static final int。它们有时在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值。

相关问题