From: Soultanian, M. <mik...@am...> - 2018-05-05 03:19:31
|
Hi, We have a problem at work where my coworkers are manually (visually) parsing through the output of "usb-devices" to find information (bus, vendor, prodid, manufacturer, serialnumber, etc) and then using that information to fix problematic devices using that information. I'd like to write a python script to accomplish what my coworkers have been doing by hand, but instead of parsing through the output of "usb-device" (which I could do, but it's tedious), I'd rather get that information from pyusb if it can provide what I'm looking for. I've been searching google and scouring through the documentation, but I'm confused and I'm stuck. I tried the code below but it's just displaying numbers instead of text: import usb.core # find USB devices dev = usb.core.find(find_all=True) for cfg in dev: print('Serial Number=' + str(cfg.iSerialNumber)) print('VendorID=' + hex(cfg.idVendor)) print('Manufacturer=' + str(cfg.Manufacturer)) print('Product=' + hex(cfg.iProduct) + "\n") and it outputs this: Serial Number=3 VendorID=0x424 Manufacturer=1 Product=2 Serial Number=3 VendorID=0x403 Manufacturer=1 Product=2 ... Obviously that's not what I'm looking for. I'm really hoping to get more information similar to the output from usb-devices: T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8 D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=01 MxPS=64 #Cfgs= 1 P: Vendor=1d6b ProdID=0002 Rev=04.04 S: Manufacturer=Linux 4.4.0-116-generic xhci-hcd S: Product=xHCI Host Controller S: SerialNumber=0000:00:15.0 C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 7 D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=02 MxPS=64 #Cfgs= 1 P: Vendor=0424 ProdID=2517 Rev=00.00 S: Manufacturer=cambrionix S: Product=U16S C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=2mA I: If#= 0 Alt= 1 #EPs= 1 Cls=09(hub ) Sub=00 Prot=02 Driver=hub ... Any help would be much appreciated! Thanks, Mike |
From: Tormod V. <lis...@gm...> - 2018-05-05 09:31:47
|
Hi Mike, The numbers you get from the configuration are indexes to the string descriptors. You can use get_string() to retrieve the strings from the device. If nowhere else, it is documented in the usb/util.py source code. Note that the configuration indexes are cached by the operating system, and usually readable without OS privileges, however retrieving the string descriptors involves the device and requires OS permissions to the device. Regards, Tormod On Sat, May 5, 2018 at 5:03 AM, Soultanian, Mike wrote: > Hi, > > We have a problem at work where my coworkers are manually (visually) parsing > through the output of "usb-devices" to find information (bus, vendor, > prodid, manufacturer, serialnumber, etc) and then using that information to > fix problematic devices using that information. I'd like to write a python > script to accomplish what my coworkers have been doing by hand, but instead > of parsing through the output of "usb-device" (which I could do, but it's > tedious), I'd rather get that information from pyusb if it can provide what > I'm looking for. I've been searching google and scouring through the > documentation, but I'm confused and I'm stuck. I tried the code below but > it's just displaying numbers instead of text: > > import usb.core > > # find USB devices > dev = usb.core.find(find_all=True) > > for cfg in dev: > print('Serial Number=' + str(cfg.iSerialNumber)) > print('VendorID=' + hex(cfg.idVendor)) > print('Manufacturer=' + str(cfg.Manufacturer)) > print('Product=' + hex(cfg.iProduct) + "\n") > > and it outputs this: > > Serial Number=3 > VendorID=0x424 > Manufacturer=1 > Product=2 > > Serial Number=3 > VendorID=0x403 > Manufacturer=1 > Product=2 > > ... > > Obviously that's not what I'm looking for. I'm really hoping to get more > information similar to the output from usb-devices: > > T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8 > D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=01 MxPS=64 #Cfgs= 1 > P: Vendor=1d6b ProdID=0002 Rev=04.04 > S: Manufacturer=Linux 4.4.0-116-generic xhci-hcd > S: Product=xHCI Host Controller > S: SerialNumber=0000:00:15.0 > C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA > I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub > > T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 7 > D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=02 MxPS=64 #Cfgs= 1 > P: Vendor=0424 ProdID=2517 Rev=00.00 > S: Manufacturer=cambrionix > S: Product=U16S > C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=2mA > I: If#= 0 Alt= 1 #EPs= 1 Cls=09(hub ) Sub=00 Prot=02 Driver=hub > > ... > > > Any help would be much appreciated! > > Thanks, > > Mike |
From: Soultanian, M. <mik...@am...> - 2018-05-07 00:42:41
|
Tormod, Thank you for your assistance - that helped immensely! I wrote the following and it works: dev = usb.core.find(find_all=True) for cfg in dev: print "Product:", str(usb.util.get_string(cfg, cfg.iProduct)) print "Manufacturer:", str(usb.util.get_string(cfg, cfg.iManufacturer)) print "Serial:", str(usb.util.get_string(cfg, cfg.iSerialNumber)) print "VendorID:", hex(cfg.idVendor) + "(" + str(cfg.idVendor) + ")" print "ProductID:", str(usb.util.get_string(cfg, cfg.iProduct)) The last two items I can't figure out how to get are the bus and the dev# as displayed in the usb-devices output - any idea how I retrieve those? I was searching through the files and I can't seem to figure out what to type to get those attributes. Thanks! Mike On 5/5/2018 2:31 AM, Tormod Volden wrote: > Hi Mike, > > The numbers you get from the configuration are indexes to the string > descriptors. You can use get_string() to retrieve the strings from the > device. If nowhere else, it is documented in the usb/util.py source > code. > > Note that the configuration indexes are cached by the operating > system, and usually readable without OS privileges, however retrieving > the string descriptors involves the device and requires OS permissions > to the device. > > Regards, > Tormod > > On Sat, May 5, 2018 at 5:03 AM, Soultanian, Mike wrote: >> Hi, >> >> We have a problem at work where my coworkers are manually (visually) parsing >> through the output of "usb-devices" to find information (bus, vendor, >> prodid, manufacturer, serialnumber, etc) and then using that information to >> fix problematic devices using that information. I'd like to write a python >> script to accomplish what my coworkers have been doing by hand, but instead >> of parsing through the output of "usb-device" (which I could do, but it's >> tedious), I'd rather get that information from pyusb if it can provide what >> I'm looking for. I've been searching google and scouring through the >> documentation, but I'm confused and I'm stuck. I tried the code below but >> it's just displaying numbers instead of text: >> >> import usb.core >> >> # find USB devices >> dev = usb.core.find(find_all=True) >> >> for cfg in dev: >> print('Serial Number=' + str(cfg.iSerialNumber)) >> print('VendorID=' + hex(cfg.idVendor)) >> print('Manufacturer=' + str(cfg.Manufacturer)) >> print('Product=' + hex(cfg.iProduct) + "\n") >> >> and it outputs this: >> >> Serial Number=3 >> VendorID=0x424 >> Manufacturer=1 >> Product=2 >> >> Serial Number=3 >> VendorID=0x403 >> Manufacturer=1 >> Product=2 >> >> ... >> >> Obviously that's not what I'm looking for. I'm really hoping to get more >> information similar to the output from usb-devices: >> >> T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8 >> D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=01 MxPS=64 #Cfgs= 1 >> P: Vendor=1d6b ProdID=0002 Rev=04.04 >> S: Manufacturer=Linux 4.4.0-116-generic xhci-hcd >> S: Product=xHCI Host Controller >> S: SerialNumber=0000:00:15.0 >> C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA >> I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub >> >> T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 7 >> D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=02 MxPS=64 #Cfgs= 1 >> P: Vendor=0424 ProdID=2517 Rev=00.00 >> S: Manufacturer=cambrionix >> S: Product=U16S >> C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=2mA >> I: If#= 0 Alt= 1 #EPs= 1 Cls=09(hub ) Sub=00 Prot=02 Driver=hub >> >> ... >> >> >> Any help would be much appreciated! >> >> Thanks, >> >> Mike |
From: Tormod V. <lis...@gm...> - 2018-05-07 07:39:17
|
Mike, It might depend on the backend, but your device object should have .bus , .address and .port_number members. Regards, Tormod On Mon, May 7, 2018 at 2:26 AM, Soultanian, Mike wrote: > > The last two items I can't figure out how to get are the bus and the dev# as > displayed in the usb-devices output - any idea how I retrieve those? I was > searching through the files and I can't seem to figure out what to type to > get those attributes. > |
From: Soultanian, M. <mik...@am...> - 2018-05-07 15:44:51
|
Thanks for your help! What I'm ultimately trying to do is reset the device, but it looks like pyusb has a .reset() method so I'm gonna give that a try to see if it has the same effect as the usb_modeswitch hack that we're using. Again, thanks so much! I'm not a great programmer so I kinda just have to hack my way through this stuff and sometimes I don't know where to look in the code. Once you suggested port_number, I did a search for that and found all the different items you were talking about and more. Thanks!! On 5/7/2018 12:39 AM, Tormod Volden wrote: > Mike, > > It might depend on the backend, but your device object should have > .bus , .address and .port_number members. > > Regards, > Tormod > > > On Mon, May 7, 2018 at 2:26 AM, Soultanian, Mike wrote: >> The last two items I can't figure out how to get are the bus and the dev# as >> displayed in the usb-devices output - any idea how I retrieve those? I was >> searching through the files and I can't seem to figure out what to type to >> get those attributes. >> |