Pyusb on windows - no backend available

后端 未结 9 1149
情话喂你
情话喂你 2020-11-29 04:55

I\'m trying to have my python application interface with an NFC device via USB. The best option seems to be pyusb, but I can\'t get it to connect to the libusb backend. I ke

9条回答
  •  盖世英雄少女心
    2020-11-29 05:07

    There's a simpler solution.

    Download and unpack to C:\PATH the libusb-1.0.20 from download link

    Then try this line:

    backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:\PATH\libusb-1.0.20\MS32\dll\libusb-1.0.dll")

    dev = usb.core.find(backend=backend, find_all=True)

    Depending on your system, try either MS64 or MS32 version of the .dll

    Update of 17/01/2020, after a request to share more code:

    import usb.core
    import usb.util
    
    from infi.devicemanager import DeviceManager
    dm = DeviceManager()
    devices = dm.all_devices
    for i in devices:
        try:
            print ('{} : address: {}, bus: {}, location: {}'.format(i.friendly_name, i.address, i.bus_number, i.location))
        except Exception:
            pass
    
    
    import usb.backend.libusb1
    
    backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:\\libusb-1.0.20\\MS32\\dll\\libusb-1.0.dll")
    dev = usb.core.find(backend=backend, find_all=True)
    
    def EnumerateUSB():    #I use a simple function that scans all known USB connections and saves their info in the file
        with open("EnumerateUSBLog.txt", "w") as wf:
            counter = 0
            for d in dev:
                try:
                    wf.write("USB Device number " + str(counter) + ":" + "\n")
                    wf.write(d._get_full_descriptor_str() + "\n")
                    wf.write(d.get_active_configuration() + "\n")
                    wf.write("\n")
                    counter += 1
                except NotImplementedError:
                    wf.write("Device number " + str(counter) + "is busy." + "\n")
                    wf.write("\n")
                    counter += 1
                except usb.core.USBError:
                    wf.write("Device number " + str(counter) + " is either disconnected or not found." + "\n")
                    wf.write("\n")
                    counter += 1
            wf.close()
    

提交回复
热议问题