Menu

Scanner is dead after opening Device

Help
swehan
2007-07-04
2012-12-06
  • swehan

    swehan - 2007-07-04

    Hi everyone,

    I am developing a program to read data from scanner using libusbJava package.
    After I can open the device, red light from my scanner is gone..
    Here is my code.

    device.isOpen() is true and I still can see my scanner with TestLibUsb.exe.

    *******************************************************************************
        public void Init()
        {
        LibusbJava.usb_init();
        int bussesNum = LibusbJava.usb_find_busses();
        int devicesNum = LibusbJava.usb_find_devices();

        bus = LibusbJava.usb_get_busses();

        // log the bus structure to standard out
        Utils.logBus(bus);
        }
       
        public Device getDevice(short vendorID, short productID)
        {
        Device device = USB.getDevice(vendorID, productID);
        return device;
        }
       
        public static void main(String[] args)
        {
        short vendorID = 0x65a;
        short productID = 0x1;
           
        bsm = BarcodeScannerManager.getInstance();
        bsm.Init();
        Device device = bsm.getDevice(vendorID, productID);
        try{
            device.updateDescriptors();
        }catch(USBException usbException){
            usbException.printStackTrace();
        }
           
        Usb_Config_Descriptor[] cfgs = device.getConfigDescriptors();
        Usb_Config_Descriptor cfg;
        byte bConfigurationValue = -1;
        byte bInterfaceNumber = -1;
        byte bAlternateSetting = -1;
           
           
        if(cfgs != null){
           for(int i = 0; i < cfgs.length; i++)
               {
            cfg = cfgs[i];
            bConfigurationValue = cfg.getBConfigurationValue();
                   
            Usb_Interface[] interfaces = cfg.getInterface();
            int noOfInterfaces = cfg.getBNumInterfaces();
            for(int j = 0; j < noOfInterfaces; j++)
            {
                Usb_Interface _interface = interfaces[j];
                Usb_Interface_Descriptor[] interfaceDescriptors =                                                              
                                                _interface.getAltsetting();
                       
                if(interfaceDescriptors != null){
                Usb_Interface_Descriptor interfaceDescriptor = 
                                               interfaceDescriptors[0];
                bInterfaceNumber = interfaceDescriptor.getBInterfaceNumber();
                bAlternateSetting = interfaceDescriptor.getBAlternateSetting();
                           
                try{
                System.out.println("Opening Device..... .");
                if(device.isOpen()) System.out.println("Device already opened.");
                               
                device.open(bConfigurationValue, bInterfaceNumber,
                                        bAlternateSetting);
                               
                if(device.isOpen())
                {
                    System.out.println("Device is open using..");
                    System.out.println("Configuration: "+bConfigurationValue);
                    System.out.println("InterfaceNumber: "+bInterfaceNumber);
                    System.out.println("AlternateSetting: "+bAlternateSetting);
                    //device.close();
                }
                }catch(USBException usbExec){
                    usbExec.printStackTrace();
                }
                }
            }
            }
        }//End if(cfg!=null)
        }
    ************************************************************************************

    libusb-Win32 knows my scanner.. this is the result from TestLibUsb.exe.

    bus-0/\\.\libusb0-0002--0x065a-0x0001     065A/0001
    - Manufacturer : OPT0-E
    - Product      : Barcode Device
      wTotalLength:         34
      bNumInterfaces:       1
      bConfigurationValue:  1
      iConfiguration:       0
      bmAttributes:         80h
      MaxPower:             50
        bInterfaceNumber:   0
        bAlternateSetting:  0
        bNumEndpoints:      1
        bInterfaceClass:    3
        bInterfaceSubClass: 1
        bInterfaceProtocol: 1
        iInterface:         0
          bEndpointAddress: 81h
          bmAttributes:     03h
          wMaxPacketSize:   8
          bInterval:        10
          bRefresh:         0
          bSynchAddress:    0

     
    • andi

      andi - 2007-07-04

      Hi

      Note that isOpen() only checks for a valid device handle. It doesn't check if the device is still attached or working. (see the Javadoc).

      I'd recommend to use the Device class to read and write to the device. An example is here http://libusbjava.sourceforge.net/wp/res/demos/ReadWrite.java.html

      It's a bit difficult to say why the 'red light' gets off. It's possibly just the default behaviour of the device. I'd try to read some data from the endpoint. If the device is not working you will get an USBException, if there's no data you will get a USBTimeoutException.

      Hope that helps!

       
    • swehan

      swehan - 2007-07-09

      I tried to refresh with usb_find_busses() and usb_find_devices() every 50ms. If device is Not attach the result is clear but if attached I cannot always see my scanner, it's not in the list sometimes.
      So sometimes I can get the data but sometimes Device cannot be found "USBException" is shown.

      That is the code where I open the device and get the data.
      ----------------------------------------------------------
      while(true){
          if(!device.isOpen()){
              device.open(bConfigurationValue, bInterfaceNumber, bAlternateSetting);
          }
                                     
          if(device.isOpen())
          {                                   
          byte[] readData = new byte[8];
                                          try{
                                              byte endPointAddress = endPointDescriptor.getBEndpointAddress();
                                              short maxPacketSize = endPointDescriptor.getWMaxPacketSize();
                                              byte timeout = endPointDescriptor.getBInterval();
                                                 
                                              int readByte = device.readBulk(
                                                      endPointAddress, readData,
                                                          maxPacketSize, timeout, true);
                                                 
                                              logData(readData);
                                          }
                                          catch(USBException usbExce){
                                              usbExce.printStackTrace();
                                              device.close();
                                          }
                                      }
                                      Thread.sleep(1000);
                                  }
      ---------------------------------------------------------
      For the red light gone case, I tried closing device upon exception. But it didn't help. 

       
    • swehan

      swehan - 2007-07-09

      These are some values retrieved from scanner's config, interface and end-point.

      Endpoint address : -127 (endPointAddress)
      Max Packet Size : 8 (maxPacketSize)
      Interval : 10 (timeout)

      Configuration: 1 (bConfigurationValue)
      InterfaceNumber: 0 (bInterfaceNumber)
      AlternateSetting: 0 (bAlternateSetting)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.