[python-win32] USB device tree physical topology

Tim Roberts timr at probo.com
Mon Mar 25 20:15:08 CET 2013


David Goodger wrote:
> I'm working on a project where my Win32 (Win XP and above) will
> communicate with a custom hardware device. The device is equivalent to
> a USB hub with an SD card reader and a serial-to-USB adapter attached.
> My app will communicate with the device via serial-over-USB and
> interact with the SD card.
>
> My problem is in figuring out which SD card goes with which serial
> adaptor. There could be multiple devices attached, or other serial
> adapters or SD card readers attached to the same PC, and I cannot
> assume that any SD card is the one that goes with the device.
>
> How can I determine the physical layout of the USB device tree from
> Python? Put another way, how can I get a list of devices physically
> attached to a specific USB hub?

Ugly.  That will be the word for whatever solution you come up with. 
There are actually several different ways in which this task will be ugly.

Windows does have several low-level APIs to extract information about
devices, but it's quite difficult for a user-mode application to get
information about how those device relate to each other.  That's partly
because such information is delicate; it's relying on behavior that
tends to be transient, machine dependent, and even operating system
dependent.

It's possible to go enumerate all of the USB hubs in the system, using
the SetupDi APIs.  It's also possible to query each hub about the things
that are plugged into to, using custom ioctls.  The "usbview"
application from the Windows Driver Kit (WDK) does this, and its source
code is available.  (A newer version is called "uvcview".)

However, even that is not going to be enough for what you need to do. 
That will tell you the USB vendor and product IDs of the plugged-in
devices.  But what YOU want is (I suspect) is the COM port name of the
fake serial adapter, and the drive letter of the partition on the volume
on the SD card.  Both of those tasks are tricky.

Everything that you can do in Device Manager can be done with the
SetupDi APIs, in setupapi.h.  The WDK also contains the source for an
application called "devcon" that shows how to use them.  They are quite
verbose, and I don't know whether there is a Python binding for them.

I write Windows USB drivers for a living, so I'm pretty well steeped in
this.  You can try asking some followup questions, and I'll see if I
can't lead you down a rose-colored path.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list