title: How to send data from usb in java
body: <p>I am creating one java application that consists of list of attached usb devices using library from <a href="http://usb4java.org/index.html" rel="nofollow">here</a> here my requirement is i was opened hyper terminal and i want to send data from java code to hyper terminal via usb can any one tell me how to do this please this is my java code to find vendor id and product id of a devices and i tried some code to communicate via usb but it was not working and can any one tell me how to do the communication using usb</p>
  
  <pre><code>package com.lakshman.sundeep;
  
  import java.nio.ByteBuffer;
  
  import org.usb4java.Context;
  import org.usb4java.Device;
  import org.usb4java.DeviceDescriptor;
  import org.usb4java.DeviceHandle;
  import org.usb4java.DeviceList;
  import org.usb4java.LibUsb;
  import org.usb4java.LibUsbException;
  
  public class UTMusb 
  {
      public static int vid = 6790;
      public static int pid = 29987;
  
      public static void main(String[] args) 
      {
          Context context = new Context();
          int result = LibUsb.init(context);
          if(result != LibUsb.SUCCESS)
          {
              throw new LibUsbException("Unable to initialize the usb device",result);
          }
          DeviceList list = new DeviceList();
          result = LibUsb.getDeviceList(null, list);
          if(result &lt; 0 )throw new LibUsbException("Unable to get device list",result);
              try
              {
                  for(Device device : list)
                  {
                      DeviceDescriptor device_descriptor = new DeviceDescriptor();
                      result = LibUsb.getDeviceDescriptor(device, device_descriptor);
                      if(result != LibUsb.SUCCESS)throw new LibUsbException("Unable to get device descriptor : ",result);
                      System.out.println("Product id is : "+device_descriptor.idProduct()+" "+"Vendor id is : "+device_descriptor.idVendor());
                      if(device_descriptor.idProduct()==pid &amp;&amp; device_descriptor.idVendor()==vid)
                      {
                          System.out.println("Product id and vendor id was matched");
                          getDeviceHandle(device);
                      }
                      else
                      {
  
                          System.out.println("Product id and vendor id was not matched");
                      }
  
                  }
  
              }
              finally
              {
                  LibUsb.freeDeviceList(list, true);
              }
  
  
      }
  
      public static void getDeviceHandle(Device device)
      {
          DeviceHandle handle = new DeviceHandle();
          int result = LibUsb.open(device, handle);
          if(result!=LibUsb.SUCCESS)
          {
              throw new LibUsbException("Unable to open the usb device",result);
          }
          try
          {
              claimDevice(handle,0);
          }
          finally
          {
              LibUsb.close(handle);
          }
      }
  
      private static void claimDevice(DeviceHandle handle, int interfacenumber) {
           int result = LibUsb.claimInterface(handle, interfacenumber);
  
           if(result!=LibUsb.SUCCESS)
           {
               throw new LibUsbException("Unabl to claim or take the usb interface",result);
           }
           try
           {
               System.out.println("Usb device taken successfully");
               SendData(handle);
           }
          finally
          {
              result = LibUsb.releaseInterface(handle, interfacenumber);
              if(result!=LibUsb.SUCCESS);
              {
                  throw new LibUsbException("Unable to release interface ",result);
              }
          }
      }
  
      private static void SendData(DeviceHandle handle) 
      {
          ByteBuffer sending_buffer= ByteBuffer.allocateDirect(8);
          sending_buffer.put(new byte[]{1,2,3,4,5,6,7,8});
          int transfered = LibUsb.controlTransfer(handle, (byte)(LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),(byte)0x09, (short)2, (short)1, sending_buffer, 5000);
          if(transfered &lt; 0)
          {
              throw new LibUsbException("Transfering failed",transfered);
          }
          System.out.println(transfered +"Data bytes sent" );
      }
  
  }
  </code></pre>