【问题标题】:Android communication with USB to Serial Device and controlTransfersAndroid 与 USB 到串行设备的通信和 controlTransfers
【发布时间】:2014-06-23 20:18:43
【问题描述】:

我已经搜索过很多帖子,比如这个: 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

感谢您的帮助。

【问题讨论】:

    标签: android serial-port usb usbserial parity


    【解决方案1】:

    这是USB spec 的一部分,特别是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 值。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2018-02-17
    • 1970-01-01
    • 2012-12-19
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多