Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyUsb does not recognize my USB device while my PC does

I'm trying to communicate between the my PC and PIC18F4550, but the program is not detecting it whereas the computer is showing it in Device Manager.

import usb.core

dev = usb.core.find(idVendor = 0x04D8, idProduct = 0xFEAA)

The function for checking USB devices:

def find(find_all = False, backend = None, custom_match = None, **args):
    def device_iter(k, v):
        for dev in backend.enumerate_devices():
            d = Device(dev, backend)
            if  _interop._reduce(lambda a, b: a and b,map(operator.eq,v,map(lambda i:getattr(d,i),k)),True)and (custom_match is None or custom_match(d)):
                yield d
        if backend is None:
            import usb.backend.libusb1 as libusb1
            import usb.backend.libusb0 as libusb0
            import usb.backend.openusb as openusb

            for m in (libusb1, openusb, libusb0):
                backend = m.get_backend()
                if backend is not None:
                    _logger.info('find(): using backend "%s"', m.__name__)
                       break
                 else:
                    raise ValueError('No backend available')

        k, v = args.keys(), args.values()

        if find_all:
            return device_iter(k, v)
        else:
            try:
                return _interop._next(device_iter(k, v))
            except StopIteration:
                return None

Error which I'm getting while running the code.

Traceback (most recent call last):
 File "C:\modules\motor.py", line 29, in <module>
   dev = usb.core.find(idVendor=0x04D8,idProduct=0xFEAA)
 File "C:\Python27\lib\site-packages\usb\core.py", line 1199, in find
   raise ValueError('No backend available')
 ValueError: No backend available

Before it used to execute properly, but for the past few days it's showing this error. I don't understand what happened all of sudden. Is there any problem using the PyUSB modules?

I have seen some of them getting the same problem while using USB communication.


I've sorted out the problem. The solution is that PyUSB module will search for libusb0.dll and libusb-1.0.dll files which are backends to communicate with USB devices which we need to include in PATH environment variable.

like image 510
dinece Avatar asked Nov 09 '22 18:11

dinece


1 Answers

Whenever we use PyUSB modules for USB communication with PC then PyUSB module will check for libusb0.dll and libusb-1.0.dll files (which act as backends) in the PATH environment variable and in C:\windows\System32 locations and then establishes communication with USB devices. Since i'm using libusb-win32-wizard for creating device drivers it uses libusb0.dll. The process of execution can be found using following DEBUG program:

import os
os.environ['PYUSB_DEBUG'] = 'debug'
import usb.core
print list(usb.core.find(find_all=True))

when i execute the above program in Shell, the output i got is:

2016-03-26 11:41:44,280 ERROR:usb.libloader:'Libusb 1' could not be found
2016-03-26 11:41:44,280 ERROR:usb.backend.libusb1:Error loading libusb 1.0 backend
2016-03-26 11:41:44,280 ERROR:usb.libloader:'OpenUSB library' could not be found
2016-03-26 11:41:44,280 ERROR:usb.backend.openusb:Error loading OpenUSB backend
2016-03-26 11:41:44,280 INFO:usb.core:find(): using backend "usb.backend.libusb0"
2016-03-26 11:41:44,280 DEBUG:usb.backend.libusb0:_LibUSB.enumerate_devices()
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E530>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E5D0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E6C0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E7B0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E8A0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E990>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200EA80>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200EB70>)
[<DEVICE ID 046d:c05a on Bus 000 Address 001>, <DEVICE ID 046d:c31d on Bus 000 Address 002>, <DEVICE ID 046d:c31d on Bus 000 Address 003>, <DEVICE ID 046d:c31d on Bus 000 Address 004>, <DEVICE ID 04d8:feaa on Bus 000 Address 005>, <DEVICE ID 046d:082b on Bus 000 Address 006>, <DEVICE ID 046d:082b on Bus 000 Address 007>, <DEVICE ID 046d:082b on Bus 000 Address 008>]

So here since i gave the argument as find_all=True in usb.core.find() function it returns every device ID's connected to PC. Also in first 4 lines it gives error since we use lib-usb-win32-wizard which uses libusb0.dll and hence in 5th line it gave INFO:usb.core:find(): using backend "usb.backend.libusb0" which means it is using libusb0.dll for communicating with USB devices.

like image 74
dinece Avatar answered Nov 14 '22 22:11

dinece