usb device identification

后端 未结 4 1403
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 07:12

i am using python on ubuntu 9.04 say i have two usb devices connected to a single PC. how can i identify the devices in python code.....for example like

if usb port

相关标签:
4条回答
  • 2020-12-09 07:18

    Ok i was also googling around for answers, here is snippet that works:

    def locate_usb():
    import win32file
    drive_list = []
    drivebits=win32file.GetLogicalDrives()
    for d in range(1,26):
        mask=1 << d
        if drivebits & mask:
            # here if the drive is at least there
            drname='%c:\\' % chr(ord('A')+d)
            t=win32file.GetDriveType(drname)
            if t == win32file.DRIVE_REMOVABLE:
                drive_list.append(drname)
    return drive_list
    

    taken from https://mail.python.org/pipermail/python-win32/2006-December/005406.html

    0 讨论(0)
  • 2020-12-09 07:29

    Have you tried pyUsb? Install using:

    pip install pyusb
    

    Here a snippet of what you can do:

    import usb
    busses = usb.busses()
    for bus in busses:
        devices = bus.devices
        for dev in devices:
            print("Device:", dev.filename)
            print("  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor))
            print("  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct))
    

    Here a good tutorial of pyUsb.

    For more documentation, use Python interactive mode with dir() and help().

    0 讨论(0)
  • 2020-12-09 07:33

    @systempuntoout's answer is nice but today I have found an easier way to find or iterate over all devices: usb.core.find(find_all=True)

    Following with your example:

    import usb
    for dev in usb.core.find(find_all=True):
        print "Device:", dev.filename
        print "  idVendor: %d (%s)" % (dev.idVendor, hex(dev.idVendor))
        print "  idProduct: %d (%s)" % (dev.idProduct, hex(dev.idProduct))
    
    0 讨论(0)
  • 2020-12-09 07:34

    but whatever.. someone will look for the answer at some point:

    I'm on a mac (osx 10.9).. I successfully installed libusb with mac ports, but was getting the "no backend available" message. It's because python can't find the usb dylibs.

    You have to add the path to your libusb to your $DYLD_LIBRARY_PATH (e.g. /opt/local/lib wherever your macport installed it).

    As soon I as I added it, pyusb worked fine.

    0 讨论(0)
提交回复
热议问题