Writing to USB device

40 views
Skip to first unread message

Tom Hickey

unread,
Aug 8, 2019, 1:42:00 PM8/8/19
to usb4java
Hi,

This is my first time doing USB communication in java. I am trying to write to a device with the following device descriptor:

Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            2 Communications
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x1fc9 NXP Semiconductors
  idProduct          0x0094
  bcdDevice            1.01
  iManufacturer           1
  iProduct                2
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           67
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0

      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         2 Communications
      bInterfaceSubClass      2 Abstract (modem)
      bInterfaceProtocol      0 None
      iInterface              0
      CDC Header:
        bcdCDC               1.10
      CDC Call Management:
        bmCapabilities       0x01
          call management
        bDataInterface          1
      CDC ACM:
        bmCapabilities       0x06
          sends break
          line coding and serial state
      CDC Union:
        bMasterInterface        0
        bSlaveInterface         1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0010  1x 16 bytes
        bInterval               8
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 Unused
      bInterfaceProtocol      0
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0


The function I am using to try to write is as follows:
public static void writeDevice(byte[] data, int length, DeviceHandle handle){
  try{
     // Detach Device from Kernel for Use
     detachDevice(handle);
     //Claim the device for use
     int resultClaim = LibUsb.claimInterface(handle, 0);
     if (resultClaim != LibUsb.SUCCESS) throw new LibUsbException("Unable to claim interface", resultClaim);
       try{
         // Use device here for transferring data
         ByteBuffer buffer = ByteBuffer.allocateDirect(length);
         buffer.put(data);
         buffer.rewind();
         int transfered = LibUsb.controlTransfer(handle,
           (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
           (byte) 0x01, (short) 2, (short) 1, buffer, 3000);
         if (transfered < 0) throw new LibUsbException("Control transfer failed", transfered);
         System.out.println(transfered + " bytes sent");
       }
       finally{
         //Release Interface
         resultClaim = LibUsb.releaseInterface(handle, 0);
         if (resultClaim != LibUsb.SUCCESS) throw new LibUsbException("Unable to release interface", resultClaim);
       }
    }
    catch(Exception e){
    System.out.println(e);
  }
}
With the following function call:

writeDevice(new byte[] {0x00, 0x01, 0x42}, 3, handle)

I am unfamiliar with how the parameters of controlTransfer work and am not quite sure that I am setting them properly for my device. I have also tried sending a string of 8 bytes and 64 bytes to rule out that I am not sending the correct number of bytes to the device.

Any help would be appreciated.

Thank you

Peter Stoiber

unread,
Aug 8, 2019, 5:12:34 PM8/8/19
to usb4java
Try to shift the value of the wValue field from the controltransfer field 8 bits left.

From:
int transfered = LibUsb.controlTransfer(handle,
(byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
(byte) 0x01, (short) 2, (short) 1, buffer, 3000);

To:


int transfered = LibUsb.controlTransfer(handle,
(byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),

(byte) 0x01, (short) 2 << 8, (short) 1, buffer, 3000);

Peter

Tom Hickey

unread,
Aug 9, 2019, 10:01:56 AM8/9/19
to usb4java
This did not change any behavior for me.
Reply all
Reply to author
Forward
0 new messages