usbid 1.0.3
This module helps to get infos for your connected usb devices, and find out where they are mounted on your fs. It basically consists of two parts:
get information about usbdevices from its identifiers.
get a tree of the connected devices.
usbinfo
Get information about USB-devices from identifiers based on the Linux USB information file
In the data folder there is this file named usb.ids containing different information about usb devices.
>>> from usbid.usbinfo import USBINFO >>> USBINFO
On USBINFO the following properties are providing information
dict of usbids with key as vendor id and value as a tuple consisting of vendor name and dict of key as device id and value as device name.
dict of device classes with key as class id and value as a tuple consisting of class name and dict of key as subclass id and value as a tuple consisting of subclass name and dict of key as protocol id and value as protocol name.
dict of audio class terminal types with key as terminal type and value as terminal name.
dict of HID descriptor types with key as HID descriptor type and value as descriptor type name.
dict of HID descriptor item types with key as item type and value as item type name.
dict of BIAS item types with key as BIAS item type and value as item type name.
dict of PHY item types with key as PHY item type and value as item type name.
dict of HID usages with key as HID id and value as a tuple consisting of HID usage page name and a dict of key as hid usage and value as hid usage name (i.e. a Magic Carpet Simulation Device ).
dict of languages with key as language id and value as a tuple consisting of language name and a dict of key as dialect id and value as dialect name
dict of codes with key as code id and value as a tuple consisting of language name and a dict of key as dialect id and value as dialect name.
dict of video class terminal types with key as terminal type and value as terminal type name.
For further details lookup the usbinfo.py and usbinfo.rst to see how it can be used.
device
Devices are read from the Linux sys-fs. So this work only with Linux or any OS that provides the same sys-fs. Its tested with Debian based systems like Ubuntu and Linux-Mint and aims to work on Raspbian.
The USB devices are provided in a tree like they are connected to the computer. Each USB root hub is an own tree in the virtual usb_roots() dict. If another external hub is connected to the root hub, it is a subtree as a node of the root hub. Actual devices are the leafs, i.e a mouse, keyboard, serial adapter or magic carpet.
The roots object is a simple dict and all below are usbid.device.DeviceNode objects. A DeviceNode is itself a read-only dict-like object.
. ├── 1 root hub A │ └── 1 external hub E1 │ ├── 1 Mouse │ ├── 2 Keyboard │ └── 3 Audio │ ├── 2 root hub B │ ├── 1 Magic Carpet │ └── 2 Hard Disk │ └── 3 root hub C └── 1 external hub E2 └── 1 external hub E3 ├── 1 Serial Adapter S1 (ttyUSB1) └── 2 Serial Adapter S2 (ttyUSB0)
>>> from usbid.device import usb_roots
>>> print serial_s2 idProduct: 2303 idVendor: 067b Product Name: PL2303 Serial Port Vendor Name: Prolific Technology, Inc. >>> serial_s2.idVendor 067b >>> serial_s2.nameVendor Prolific Technology, Inc. >>> serial_s2.idProduct 2303 >>> serial_s2.nameProduct PL2303 Serial Port
Its also possible to traverse up:
>>> serial_s2.is_root False >>> roots[0].is_root True
A special case is built in for serial devices. The number and type of a tty is assigned in plugin order. So a serial device named /dev/ttyUSB0 can be next time /dev/ttyUSB1 if there are two almost same device are connected.
As a human you know its connected to port 1 and port 2 of a usb hub. Now knowing the path allows us to store this information in application logic.
DeviceNode provides the actual name of the serial character device in the system:
In order ot make filtering of devices easier all devices are available as a flat list:
>>> from usbid.device import device_list
This allows easy filtering i.e. by vendorId or for all available ttys as shown here:
>>> ttys = [_ for _ in device_list() if _.tty]
For testing purposes there is a mocktree.tgz inside the data folder, which acts like like a real linux sys-fs filesystem with several usb devices connected.
Lookup the device.py and device.rst to get a deeper insight.
Source Code
The sources are in a GIT DVCS with its main branches at github.
We’d be happy to see many forks and pull-requests to make usbid even better.
Contributors
History
1.0.3
1.0.2
- Rename usbid.device.devicelist to usbid.device.device_list . B/C import available. [rnix, 2014-02-26]
- Introduce usbid.device.device_by_path utility. [rnix, 2014-02-26]
- use latest usb.id file version 2014.02.03 from http://www.linux-usb.org/usb.ids [jensens, 2014-02-20]
1.0.1
1.0
License
Copyright (c) 2013-2014, BlueDynamics Alliance, Austria, Germany, Switzerland All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the BlueDynamics Alliance nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY BlueDynamics Alliance AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BlueDynamics Alliance BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OrangeCoat
Use PyUSB to Find Vendor and Product IDs for USB Devices
Vendor and Product IDs
These numbers are part of the USB «device descriptor» spec and are assigned to vendors by the governing body. Here’s a big, though incomplete, list by vendor. Discovering and using these values can be handy in talking to USB devices. So far these IDs have helped me read data from 2 different Microsoft USB mice and hack the weight from a Dymo S250 USB scale.
Decimal vs Hexadecimal Caveat
Product and vendor IDs may be expressed in decimal or hexadecimal (number usually starts with 0x). For instance, testing a Microsoft Comfort Mouse 6000 for Business mouse with the Python code below returns:
Python/PyUSB Code to Find USB Devices
Here’s the basic code using version 1.0 of PyUSB. This assumes the PyUSB instructions have been followed and the required version of libUSB and Python are already installed.
Copy and paste the following code and save it to a file. My file is called findDevices.py
#!/usr/bin/python import sys import usb.core # find USB devices dev = usb.core.find(find_all=True) # loop through devices, printing vendor and product ids in decimal and hex for cfg in dev: sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.idProduct) + '\n') sys.stdout.write('Hexadecimal VendorID=' + hex(cfg.idVendor) + ' & ProductID=' + hex(cfg.idProduct) + '\n\n')
Finding A Particular Device
- Plug in the USB device and run the code above, like python findDevices.py
- Unplug the device you’re trying to discover
- Run python findDevices.py again
- Compare the lists to find the device that disappeared between 1 and 3.