views:

311

answers:

2

hello all 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 id == A write data to device 1 if usb port id == B write data to device 2

any ideas....

A: 

This is completely operating system dependent and I know of no Python library which tries to handle such hardware abstractly.

Under Linux, you could use the lsusb(1) command to hopefully get the information you need, but that's hardly Pythonic.

msw
+2  A: 

Have you tried 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().

systempuntoout
i have used the above code in a terminal window...it returns an error "object has no attribute devices"can you refer me to some tutorial that might help...or elaborate this module usb
omrihsan
Code is working for me on Osx with Python 2.6 and pyUsb installed with port.What is your platform\Python version?
systempuntoout
i have installed pyusb now and run the sample usbenum.py (your mentioned code seems pretty similar)....i have 3 usb port on my PC but the results show 6 outputs to dev.filename..they are numbers like 001 or 005 etc....and they changed when i plugged in devices...(i am no good with the usb standards)....i just want to identify each device/port...what parameter in the example would help me....
omrihsan
You can talk with a device just knowing its idVendor/idProduct and regardless of which port it is connected.
systempuntoout