1

I'm trying to create a list of alll usb mass storage devices with their VendorIf, the ProductId and the SerialNumber.

Therefore I use the pyUsb module and run the following program.

import sys
import usb
import usb.core
import usb.util

devs = usb.core.find(find_all=True)

nCount=0

for dev in devs:
    try:
        for cfg in dev:
            intf = usb.util.find_descriptor(cfg,bInterfaceClass=0x8)
            if intf is not None:
                nCount += 1
                try:
                    sys.stdout.write("USB device " + usb.util.get_string(dev,dev.iProduct,None) + '\n')    
                except:
                    sys.stdout.write("USB device " + str(nCount) + '\n')      
                sys.stdout.write("------" + '\n')       
                sys.stdout.write("VendorId = " + hex(dev.idVendor) + '\n')
                sys.stdout.write("ProductId = " + hex(dev.idProduct) + '\n')
                if not dev.iSerialNumber == 0:
                    sys.stdout.write("SerialNbr = " + usb.util.get_string(dev,dev.iSerialNumber,None) + '\n')
                else:
                     sys.stdout.write("SerialNbr = none" + '\n')                   
                sys.stdout.write('\n')       
    except usb.core.USBError:
        pass

In generally the script works. Depending on the device I get outputs like this:

USB device USB DISK
-------
VendorId = 0x90c  
ProductId = 0x2000  
SerialNbr = none 

But with various devices I get the following error:

File "C:\Users\UerXy\AppData\Local\Programs\Python\Python39\lib\site-packages\usb\backend\libusb1.py", line 600, in _check raise NotImplementedError(_strerror(ret)) NotImplementedError: Operation not supported or unimplemented on this platform

When debugging the code, the error occures when it tries to read the string descriptor using the function usb.util.get_string()

I read somewhere, that the function is dependant on the driver. Is this true? Isn't it possible to read the serial number of any given usb-device without taking care of the used usb-driver?

How can this error be solved and the descriptors be read from every device?

hafisch
  • 13
  • 4
  • The problem occures only on a windows platform. The same code in a ubuntu-system works properly and with all devices. Any ideas? – hafisch Aug 17 '22 at 10:19

1 Answers1

-1

You can use the following code to get the information of all connected drives (flash memory and hdd).

import os 
os.system('echo list volume > Ravi.txt')
path1 = os.path.join(os.getcwd(),"Ravi.txt")
os.system('diskpart /s '+path1+' > logfile.txt')
path2 = os.path.join(os.getcwd(),"logfile.txt")
Str = open(path2).read()
Str = Str.split('\n')
matching = [s for s in Str if "Removable" in s]
for i in matching:
    i = ' '.join(i.split())
    i = i.split(" ")
    print(i)

import subprocess  
serials = subprocess.check_output('wmic diskdrive get SerialNumber').decode().split('\n')[1:]
serials = [s.strip() for s in serials if s.strip()]

After running this code, two txt files will be created in the current location, in which the desired information is written