Menu

i have a magnetic swipe card reader with usb port to connect with pc. i am trying to read data through usb getting exceptions

bhuvan
2013-01-30
2013-02-13
  • bhuvan

    bhuvan - 2013-01-30

    hi guys,
    i have a magnetic swipe card reader with usb port to connect with pc. i am trying to read data through usb getting exceptions. once i connected device to pc my os is windows 7 so it is automatically installing some drivers to it. in device manager it show two usb interfaces with same vid and pid.
    Device dev = USB.getDevice((short) 0x5131, (short) 0x2007);
    try {
    // data to write to the device

      // data read from the device
      byte [] readData= new byte[20];
    
      // open the device with configuration 1, interface 0 and without
      // altinterface
      // this will initialise Libusb for you
      dev.open(1, 0, -1);
      System.out.println("port open or not : "+dev.isOpen());
      // write some data to the device
      // 0x03 is the endpoint address of the OUT endpoint 3 (from PC to
      // device)
    

    // dev.writeInterrupt(0x03, data, data.length, 2000, false);
    // read some data from the device
    // 0x84 is the endpoint address of the IN endpoint 4 (from PC to
    // device)
    // bit 7 (0x80) is set in case of an IN endpoint
    dev.readInterrupt(0x81, readData, readData.length, 2000, false);
    // log the data from the device
    logData(readData);
    // close the device
    dev.close();
    } catch (USBException e) {
    // if an exception occures during connect or read/write an exception
    // is thrown
    e.printStackTrace();
    }
    }

    and i am getting following exception.

    ch.ntb.usb.USBException: USB device with idVendor: 0x5131, idProduct: 0x2007, busName: null, filename: null not found on USB
    at ch.ntb.usb.Device.open(Device.java:234)
    at Quindell.EPOS.TestCardReadingClass.main(TestCardReadingClass.java:29)

    any help will be more appreciable..thanks in advanceee

     
  • uniederer

    uniederer - 2013-02-02

    Did you have a look if the device shows up in the TreeView demo? Do you plug it in on an USB3 port?

     
  • bhuvan

    bhuvan - 2013-02-13

    @uniederer. i done few research now and able to open usb port but when i swiped the card through reader it is raising an exception.here is my updated code.

    import ch.ntb.usb.Device;
    import ch.ntb.usb.LibusbJava;
    import ch.ntb.usb.USB;
    import ch.ntb.usb.USBException;
    import ch.ntb.usb.Usb_Bus;
    import ch.ntb.usb.Utils;
    public class TestCardReadingClass
    {
          private static void logData(byte[] data) 
       {
            System.out.print("Data: ");
            for (int i = 0; i < data.length; i++) {
              System.out.print("0x" + Integer.toHexString(data[i] & 0xff) + " ");
    }
    System.out.println();
    

    }
    /*
    * @param args
    * @throws USBException
    /

    private static void ReadData() throws USBException

    {
    Device dev = USB.getDevice((short) 0x5131, (short) 0x2007);
    try
    {
    byte[] readData = new byte[20];

          // open the device with configuration 1, interface 0 and without
              // altinterface
              // this will initialise Libusb for you
              dev.open(1, 0,-1);
              System.out.println("port open or not : "+dev.isOpen());
              int altinterface=dev.getAltinterface();
              int pid=dev.getIdProduct();
              int vid=dev.getIdVendor();
              int psize= dev.getMaxPacketSize();
              int interfac = dev.getInterface();
              int config = dev.getConfiguration();
              int xx=dev.hashCode();
              System.out.println("Device pid : "+pid);
              System.out.println("Device vid : "+vid);
              System.out.println("Device psize : "+psize);
              System.out.println("Device hashcode : "+xx);
              System.out.println("Device Configuration : "+config);
              System.out.println("Device Altinterface : "+altinterface);
              System.out.println("Device interface : "+interfac);
    
              // read some data from the device
              // 0x84 is the endpoint address of the IN endpoint 4 (from PC to
              // device)
              // bit 7 (0x80) is set in case of an IN endpoint
              dev.readBulk(0x82, readData, readData.length, 11111111, false);
               //System.out.println("ReadData value:"+readData.toString());
              //dev.readInterrupt(0x82, readData, readData.length, 100000, true);
              // log the data from the device
              logData(readData);
    
              // close the device
              dev.close();
    } catch (USBException e)
    {
              // if an exception occures during connect or read/write an exception
              // is thrown
    
              e.printStackTrace();
              dev.close();
    }
    

    }

    private static boolean LogBus(){

    boolean islogged =false;
    try{
    LibusbJava.usb_init();
    LibusbJava.usb_find_busses();
    LibusbJava.usb_find_devices();
    
    // retrieve a object tree representing the bus with its devices and
    // descriptors
    Usb_Bus bus = LibusbJava.usb_get_busses();
    Utils.logBus(bus);
    islogged=true;
    }
    catch(Exception e){
      e.printStackTrace();
    }
    
    // log the bus structure to standard out
    
    return islogged;
    

    }

    public static void main(String[] args) throws USBException
    {
      boolean islog=LogBus();
      if (islog)
      {
      ReadData();
      }else
        System.out.println("Is Bus Logged Succesfully :"+islog);
     }
    

    OUTPUT

    Usb_Bus bus-0
    Usb_Device \.\libusb0-0002--0x5131-0x2007
    Usb_Device_Descriptor idVendor: 0x5131, idProduct: 0x2007
    Usb_Config_Descriptor bNumInterfaces: 0x1
    Usb_Interface num_altsetting: 0x1
    Usb_Interface_Descriptor bNumEndpoints: 0x2
    Usb_Endpoint_Descriptor bEndpointAddress: 0x82
    Usb_Endpoint_Descriptor bEndpointAddress: 0x2
    Usb_Device \.\libusb0-0003--0x5131-0x2007
    Usb_Device_Descriptor idVendor: 0x5131, idProduct: 0x2007
    Usb_Config_Descriptor bNumInterfaces: 0x1
    Usb_Interface num_altsetting: 0x1
    Usb_Interface_Descriptor bNumEndpoints: 0x1
    Usb_Endpoint_Descriptor bEndpointAddress: 0x81
    port open or not : true
    Device pid : 8199
    Device vid : 20785
    Device psize : 64
    Device hashcode : 216072924
    Device Configuration : 1
    Device Altinterface : -1
    Device interface : 0
    ch.ntb.usb.USBTimeoutException: LibusbJava.usb_bulk_read: libusb0-dll:err [_usb_reap_async] reaping request failed, win error: The I/O operation has been aborted because of either a thread exit or an application request.

            at ch.ntb.usb.Device.readBulk(Device.java:398)
            at TestCardReadingClass.ReadData(TestCardReadingClass.java:63)
            at TestCardReadingClass.main(TestCardReadingClass.java:114)
    

    }

    Thanks in advance.....

     

    Last edit: bhuvan 2013-02-13
  • bhuvan

    bhuvan - 2013-02-13

    @uniederer.. i had usb port 3.0 to my laptop but i am not connected device to it . connected device to usb 2.0 only.. thanks for your reply

     

Log in to post a comment.