5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dinece
  • 113
  • 1
  • 2
  • 12
  • 1
    could you fix your indentation ? this should help us find the issue – maazza Mar 14 '16 at 08:10
  • no its not the indentation problem. if it so it should show the indentation error. – dinece Mar 14 '16 at 08:17
  • no what I mean is your code has formatting issues for example " yield d` " is not valid python – maazza Mar 14 '16 at 08:20
  • it was not the part of my code. i used that to structure the code in stackoverflow – dinece Mar 14 '16 at 08:23
  • 3
    yeah, but anyone who wants to help you would try to copy your code, which is easier, if your example is typeset correctly here – Ilja Mar 14 '16 at 08:40
  • Ya I have typed everything correctly and didn't change anything here. – dinece Mar 14 '16 at 09:17
  • It would be better if you posted the solution as an answer (and accepted it after a few days). Answering your own question is not frowned upon, quite the opposite; it is encouraged by badge [Self-Learner](http://meta.stackexchange.com/questions/67397/list-of-all-badges-with-full-descriptions/68258#68258). – Peter Mortensen Mar 25 '16 at 11:33

1 Answers1

1

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.

dinece
  • 113
  • 1
  • 2
  • 12