kepurlaukis
kepurlaukis

Reputation: 324

Is it possible to get USB device by USB port using PyUSB

I have a lot identical devices with STM32 VCP. Windows 8.1 creates serial COM port for device. If I connect ONE device to any PC USB port, windows will bind to it the same COM port. After the connection of the second device to other USB port, it will create the next COM port with different serial COM number. If I unplug these devices and connect in different order, COM ports will be switched between devices. It's a problem, because I need the same order COM ports order (physically) each time. Using USBlyzer software I can see what some kind of USB port number is different for each physical USB port:

enter image description here

So I would like to access (get the current device) by these devices by port number. Or list all devices and get this port number from device object?

In libusb documentation found it has such a method:

uint8_t libusb_get_port_number (libusb_device *dev)
Get the number of the port that a device is connected to.

link: http://libusb.sourceforge.net/api-1.0/group__dev.html

Maybe PyUSB has it too...

Upvotes: 4

Views: 4189

Answers (1)

kepurlaukis
kepurlaukis

Reputation: 324

Found other solution to my problem. Using windows DeviceManager API Get Port and hub number (USB physical identification) from location info and friendly name of device with COM port number in it. Parsing this information and have information for Serial communication.

from infi.devicemanager import DeviceManager
dm = DeviceManager()
dm.root.rescan()

devices = dm.all_devices


for i in devices:
    try:
        print '{} : address: {}, bus: {}, location: {}'.format(i.friendly_name, i.address, i.bus_number, i.location)
    except Exception:
        pass

Upvotes: 2

Related Questions