nexus
nexus

Reputation: 41

Android communication with USB to Serial Device and controlTransfers

I have searched through numerous posts such as this one: Using Android to Communicate with a USB HID Device

but i am still not getting how do you determine requestType in controlTransfer calls?

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

i need to set EVEN parity for my device and it does not seem to work. here's my code:

        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 does not make any sense to me and some examples have it at 0x21 or 0x81 or 0xA1...

What would be the best way of getting the correct requestType?

I should also mention that i expect to receive the data on the PC with EVEN parity and if i set the parity of the serial port on the PC to NONE - i receive the expected data, so i have concluded that the controlTransfer calls i make to the device are not working.

this is my USB to Serial device i am trying to configure from Android:

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

Thanks for your help.

Upvotes: 4

Views: 3751

Answers (1)

Preston
Preston

Reputation: 2653

This is part of the USB spec, specifically bmRequestType. Here is a bitmask listing in C, you can define these yourself in your project as static final int's. They are sometimes defined around the OS in an SDK or driver kit, or you could just specify the raw hex byte (as done above, but the defined bitmasks are nice for readability):

/* 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

You will want to provide three masks for the request type

  • the direction
  • the actual request type
  • the recipient

In your case 0x40 represents a vendor request from the Android host to your connected device, with the device itself as a recipient (vs a specific endpoint or interface). If you have multiple interfaces you would want to specify USB_SETUP_RECIPIENT_INTERFACE and then pass the interface number in the index parameter to get it to the right place. But the recipient in your case will really depend on how your device expects the data, and Prolific probably has a spec on it. You could try setting the 0x01 bit to see if it solves your problem, it could be that the request is expected for interface 0 specifically, but it may not matter since your device only has a single interface anyway.

Note that this parameter is entirely specific to USB and has nothing to do with the parity on your device, but if you don't get these packets right then your settings will not take on the device, as you are seeing. If the USB request is right then check the config value.

Upvotes: 2

Related Questions