usb パッケージ

サブモジュール

usb.control モジュール

usb.control - USB 標準制御要求(standard control requests)

本モジュールからは以下がエスクポートされます:

get_status - 受信者(recipeint)ステータスの取得、 clear_feature - 受信者(recipient)機能(feature)のクリア、 set_feature - 受信者(recipient)機能(feature)の設定、 get_descriptor - デバイス・デスクリプタ取得、 set_descriptor - デバイス・デスクリプタ設定、 get_configuration - デバイス構成(configuration)の取得、 set_configuration - デバイス構成(configuration)の設定 get_interface - デバイス・インターフェイス取得、 set_interface - デバイス・インターフェイス設定

usb.control.get_status(dev, recipient=None)[ソース]

指定の受信者(recipient)のステータスを返す。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

受信者(recipient)は、 None (デバイスからステータスが照会される) または Interface または Endpoint デスクリプタにすることができます。

ステータス値は整数として返され、下位ワードは2バイトのステータス値からなります。

usb.control.clear_feature(dev, feature, recipient=None)[ソース]

指定の機能(feature)をクリアまたは無効にします。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

featureには、あなたが無効にしたい機能(feature)を指定します。

受信者(recipient)は、 None (デバイスからステータスが照会される) または Interface または Endpoint デスクリプタにすることができます。

usb.control.set_feature(dev, feature, recipient=None)[ソース]

指定の機能(feature)を設定または有効化します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

featureにはあなたが有効にしたい機能(feature)を指定します。

受信者(recipient)は、 None (デバイスからステータスが照会される) または Interface または Endpoint デスクリプタにすることができます。

usb.control.get_descriptor(dev, desc_size, desc_type, desc_index, wIndex=0)[ソース]

指定のデスクリプタを返す。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

desc_sizeはデスクリプタのサイズを指定します。

desc_typeとdesc_indexは、それぞれデスクリプタのタイプとインデックスです。 wIndexインデックスは文字列デスクリプタに使用され、言語IDを表します。文字列デスクリプタ以外タイプの記述子の場合、wIndexはゼロです。

usb.control.set_descriptor(dev, desc, desc_type, desc_index, wIndex=None)[ソース]

存在するデスクリプタを更新するか、または新しいデスクリプタを追加します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

descほげほげなパラメータ達は、デバイスに送信されるデスクリプタです。 desc_typeとdesc_indexは、それぞれデスクリプタのタイプとインデックスです。 wIndexインデックスは文字列デスクリプタに使用され、言語IDを表します。 文字列デスクリプタ以外のタイプのデスクリプタの場合、wIndexはゼロです。

usb.control.get_configuration(dev)[ソース]

デバイスの、当座(current)でアクティブな構成(configuration)を取得します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

キャッシュされたデータを使用する可能性がある Device.get_active_configuration メソッドとは異なり、この関数は常にデバイス・リクエストを実行します。

usb.control.set_configuration(dev, bConfigurationNumber)[ソース]

当座(current)とする構成(configuration)を設定します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

usb.control.get_interface(dev, bInterfaceNumber)[ソース]

インターフェイスの代替設定(alternate setting)の当座(current)を取得します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

usb.control.set_interface(dev, bInterfaceNumber, bAlternateSetting)[ソース]

インターフェイスの代替設定(alternate setting)を設定します。

dev は、リクエストの送信先となるデバイス・オブジェクトです。

usb.core モジュール

usb.core - コアUSB機能

本モジュールからは以下がエスクポートされます:

Device - USBデバイスを表すクラス、 Configuration - 構成(configuration)デスクリプタを表すクラス、 Interface - インターフェイス・デスクリプタを表すクラス、 Endpoint - エンドポイント・デスクリプタを表すクラス、 find() - USBデバイスを探す関数、show_devices() - 存在するデバイスを表示する関数

class usb.core.Device(dev, backend)[ソース]

ベースクラス: usb._objfinalizer.AutoFinalizedObject

Deviceオブジェクトです。

このクラスには、USB仕様に基づくデバイス・デスクリプタのすべてのフィールドが含まれています。 あなたはそれらにクラスのプロパティとしてアクセスできます。 たとえば、デバイス・デスクリプタのフィールドbDescriptorTypeにアクセスするには、次のようにします:

>>> import usb.core
>>> dev = usb.core.find()
>>> dev.bDescriptorType

さらに、クラスはハードウェアと通信するメソッドを提供します。 通常、アプリケーションは最初にset_configuration()メソッドを呼び出してデバイスを既知の構成済み状態(a known conifgured state)にし、代替設定が複数ある場合、オプションでset_interface_altsetting()を呼び出して、使用するインターフェイスの代替設定を選択し、 write() や read() メソッドでデータを送受信します。

新しいハードウェアで作業する場合、最初の試行は次のようになります:

>>> import usb.core
>>> dev = usb.core.find(idVendor=myVendorId, idProduct=myProductId)
>>> dev.set_configuration()
>>> dev.write(1, 'test')

このサンプルでは、対象のデバイスを見つけ(myVendorIdおよびmyProductIdをデバイスの対応する値で置き換える必要があります)、デバイスを構成し(デフォルトでは、構成値(configuration value)は1であり、ほとんどのデバイスの一般的な値です)、エンドポイント0x01へデータを幾つか書き込みます。

書き込み、読み取り、およびctrl_transferメソッドのタイムアウト値はミリ秒単位で指定します。このパラメーターを省略した場合、代わりにDevice.default_timeout値が使用されます。Device.default_timeoutプロパティは、ユーザがいつでも設定できます。

attach_kernel_driver(interface)[ソース]

以前にdetach_kernel_driver()を使って切り離し(detach)した、指定のインターフェイスのカーネル・ドライバを再接続(re-attach)します。

interface パラメータは、ドライバを接続したい、デバイスのインターフェイス番号を指定します。

backend

そのデバイスが使っているバックエンドを返します。

clear_halt(ep)[ソース]

epに指定したエンドポイントの halt/stall 状態をクリアします。

configurations()[ソース]

そのデバイスの構成群をタプルで返します。

ctrl_transfer(bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None)[ソース]

エンドポイント0で制御転送(control transfer)を行います。

このメソッドは、エンドポイント0を介して制御転送を発行するために使用します(エンドポイント0は常に制御エンドポイントである必要があります)。

パラメータbmRequestType、bRequest、wValue、wIndexは、USB標準制御要求(Standard Control Request)フォーマットと同じです。

Control requests may or may not have a data payload to write/read. In cases which it has, the direction bit of the bmRequestType field is used to infer the desired request direction. For host to device requests (OUT), data_or_wLength parameter is the data payload to send, and it must be a sequence type convertible to an array object. In this case, the return value is the number of bytes written in the data payload. For device to host requests (IN), data_or_wLength is either the wLength parameter of the control request specifying the number of bytes to read in data payload, and the return value is an array object with data read, or an array object which the data will be read to, and the return value is the number of bytes read.

default_timeout

Default timeout for transfer I/O functions

detach_kernel_driver(interface)[ソース]

Detach a kernel driver.

If successful, you will then be able to perform I/O.

The interface parameter is the device interface number to detach the driver from.

get_active_configuration()[ソース]

Return a Configuration object representing the current configuration set.

is_kernel_driver_active(interface)[ソース]

Determine if there is kernel driver associated with the interface.

If a kernel driver is active, the object will be unable to perform I/O.

The interface parameter is the device interface number to check.

langids

Return the USB device's supported language ID codes.

These are 16-bit codes familiar to Windows developers, where for example instead of en-US you say 0x0409. USB_LANGIDS.pdf on the usb.org developer site for more info. String requests using a LANGID not in this array should not be sent to the device.

This property will cause some USB traffic the first time it is accessed and cache the resulting value for future use.

manufacturer

Return the USB device's manufacturer string descriptor.

This property will cause some USB traffic the first time it is accessed and cache the resulting value for future use.

parent

Return the parent device.

product

Return the USB device's product string descriptor.

This property will cause some USB traffic the first time it is accessed and cache the resulting value for future use.

read(endpoint, size_or_buffer, timeout=None)[ソース]

Read data from the endpoint.

This method is used to receive data from the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with. The size_or_buffer parameter either tells how many bytes you want to read or supplies the buffer to receive the data (it must be an object of the type array).

The timeout is specified in miliseconds.

If the size_or_buffer parameter is the number of bytes to read, the method returns an array object with the data read. If the size_or_buffer parameter is an array object, it returns the number of bytes actually read.

reset()[ソース]

Reset the device.

serial_number

Return the USB device's serial number string descriptor.

This property will cause some USB traffic the first time it is accessed and cache the resulting value for future use.

set_configuration(configuration=None)[ソース]

Set the active configuration.

The configuration parameter is the bConfigurationValue field of the configuration you want to set as active. If you call this method without parameter, it will use the first configuration found. As a device hardly ever has more than one configuration, calling the method without arguments is enough to get the device ready.

set_interface_altsetting(interface=None, alternate_setting=None)[ソース]

Set the alternate setting for an interface.

When you want to use an interface and it has more than one alternate setting, you should call this method to select the appropriate alternate setting. If you call the method without one or the two parameters, it will be selected the first one found in the Device in the same way of the set_configuration method.

Commonly, an interface has only one alternate setting and this call is not necessary. For most devices, either it has more than one alternate setting or not, it is not harmful to make a call to this method with no arguments, as devices will silently ignore the request when there is only one alternate setting, though the USB Spec allows devices with no additional alternate setting return an error to the Host in response to a SET_INTERFACE request.

If you are in doubt, you may want to call it with no arguments wrapped by a try/except clause:

>>> try:
>>>     dev.set_interface_altsetting()
>>> except usb.core.USBError:
>>>     pass
write(endpoint, data, timeout=None)[ソース]

Write data to the endpoint.

This method is used to send data to the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with.

The data parameter should be a sequence like type convertible to the array type (see array module).

The timeout is specified in miliseconds.

The method returns the number of bytes written.

class usb.core.Configuration(device, configuration=0)[ソース]

ベースクラス: object

Represent a configuration object.

This class contains all fields of the Configuration Descriptor according to the USB Specification. You may access them as class properties. For example, to access the field bConfigurationValue of the configuration descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     print cfg.bConfigurationValue
interfaces()[ソース]

Return a tuple of the configuration interfaces.

set()[ソース]

Set this configuration as the active one.

class usb.core.Interface(device, interface=0, alternate_setting=0, configuration=0)[ソース]

ベースクラス: object

Represent an interface object.

This class contains all fields of the Interface Descriptor according to the USB Specification. You may access them as class properties. For example, to access the field bInterfaceNumber of the interface descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     for i in cfg:
>>>         print i.bInterfaceNumber
endpoints()[ソース]

Return a tuple of the interface endpoints.

set_altsetting()[ソース]

Set the interface alternate setting.

class usb.core.Endpoint(device, endpoint, interface=0, alternate_setting=0, configuration=0)[ソース]

ベースクラス: object

Represent an endpoint object.

This class contains all fields of the Endpoint Descriptor according to the USB Specification. You can access them as class properties. For example, to access the field bEndpointAddress of the endpoint descriptor, you can do so:

>>> import usb.core
>>> dev = usb.core.find()
>>> for cfg in dev:
>>>     for i in cfg:
>>>         for e in i:
>>>             print e.bEndpointAddress
clear_halt()[ソース]

Clear the halt/status condition of the endpoint.

read(size_or_buffer, timeout=None)[ソース]

Read data from the endpoint.

The parameter size_or_buffer is either the number of bytes to read or an array object where the data will be put in and timeout is the time limit of the operation. The transfer type and endpoint address are automatically inferred.

The method returns either an array object or the number of bytes actually read.

For details, see the Device.read() method.

write(data, timeout=None)[ソース]

Write data to the endpoint.

The parameter data contains the data to be sent to the endpoint and timeout is the time limit of the operation. The transfer type and endpoint address are automatically inferred.

The method returns the number of bytes written.

For details, see the Device.write() method.

exception usb.core.USBError(strerror, error_code=None, errno=None)[ソース]

ベースクラス: OSError

Exception class for USB errors.

Backends must raise this exception when USB related errors occur. The backend specific error code is available through the 'backend_error_code' member variable.

exception usb.core.USBTimeoutError(strerror, error_code=None, errno=None)[ソース]

ベースクラス: usb.core.USBError

Exception class for connection timeout errors.

Backends must raise this exception when a call on a USB connection returns a timeout error code.

exception usb.core.NoBackendError[ソース]

ベースクラス: ValueError

Exception class when a valid backend is not found.

usb.core.find(find_all=False, backend=None, custom_match=None, **args)[ソース]

Find an USB device and return it.

find() is the function used to discover USB devices. You can pass as arguments any combination of the USB Device Descriptor fields to match a device. For example:

find(idVendor=0x3f4, idProduct=0x2009)

will return the Device object for the device with idVendor field equals to 0x3f4 and idProduct equals to 0x2009.

If there is more than one device which matchs the criteria, the first one found will be returned. If a matching device cannot be found the function returns None. If you want to get all devices, you can set the parameter find_all to True, then find will return an iterator with all matched devices. If no matching device is found, it will return an empty iterator. Example:

for printer in find(find_all=True, bDeviceClass=7):
print (printer)

This call will get all the USB printers connected to the system. (actually may be not, because some devices put their class information in the Interface Descriptor).

You can also use a customized match criteria:

dev = find(custom_match = lambda d: d.idProduct=0x3f4 and d.idvendor=0x2009)

A more accurate printer finder using a customized match would be like so:

def is_printer(dev):

import usb.util if dev.bDeviceClass == 7:

return True
for cfg in dev:
if usb.util.find_descriptor(cfg, bInterfaceClass=7) is not None:
return True
for printer in find(find_all=True, custom_match = is_printer):
print (printer)

Now even if the device class code is in the interface descriptor the printer will be found.

You can combine a customized match with device descriptor fields. In this case, the fields must match and the custom_match must return True. In the our previous example, if we would like to get all printers belonging to the manufacturer 0x3f4, the code would be like so:

printers = list(find(find_all=True, idVendor=0x3f4, custom_match=is_printer))

If you want to use find as a 'list all devices' function, just call it with find_all = True:

devices = list(find(find_all=True))

Finally, you can pass a custom backend to the find function:

find(backend = MyBackend())

PyUSB has builtin backends for libusb 0.1, libusb 1.0 and OpenUSB. If you do not supply a backend explicitly, find() function will select one of the predefineds backends according to system availability.

Backends are explained in the usb.backend module.

usb.core.show_devices(verbose=False, **kwargs)[ソース]

Show information about connected devices.

The verbose flag sets to verbose or not. **kwargs are passed directly to the find() function.

usb.legacy モジュール

class usb.legacy.Bus(devices)[ソース]

ベースクラス: object

Bus object.

class usb.legacy.Configuration(cfg)[ソース]

ベースクラス: object

Configuration descriptor object.

class usb.legacy.Device(dev)[ソース]

ベースクラス: object

Device descriptor object

open()[ソース]

Open the device for use.

Returns a DeviceHandle object

class usb.legacy.DeviceHandle(dev)[ソース]

ベースクラス: usb._objfinalizer.AutoFinalizedObject

bulkRead(endpoint, size, timeout=100)[ソース]

Performs a bulk read request to the endpoint specified.

パラメータ:
  • endpoint -- endpoint number.
  • size -- number of bytes to read.
  • timeout -- operation timeout in milliseconds. (default: 100)

Returns a tuple with the data read.

bulkWrite(endpoint, buffer, timeout=100)[ソース]

Perform a bulk write request to the endpoint specified.

パラメータ:
  • endpoint -- endpoint number.
  • buffer -- sequence data buffer to write. This parameter can be any sequence type.
  • timeout -- operation timeout in milliseconds. (default: 100)

Returns the number of bytes written.

claimInterface(interface)[ソース]

Claims the interface with the Operating System.

パラメータ:interface -- interface number or an Interface object.
clearHalt(endpoint)[ソース]

Clears any halt status on the specified endpoint.

パラメータ:endpoint -- endpoint number.
controlMsg(requestType, request, buffer, value=0, index=0, timeout=100)[ソース]

Perform a control request to the default control pipe on a device.

パラメータ:
  • requestType -- specifies the direction of data flow, the type of request, and the recipient.
  • request -- specifies the request.
  • buffer -- if the transfer is a write transfer, buffer is a sequence with the transfer data, otherwise, buffer is the number of bytes to read.
  • value -- specific information to pass to the device. (default: 0) index: specific information to pass to the device. (default: 0)
  • timeout -- operation timeout in milliseconds. (default: 100)

Returns the number of bytes written.

detachKernelDriver(interface)[ソース]
Detach a kernel driver from the interface (if one is attached,
we have permission and the operation is supported by the OS)
パラメータ:interface -- interface number or an Interface object.
getDescriptor(desc_type, desc_index, length, endpoint=-1)[ソース]

Retrieves a descriptor from the device identified by the type and index of the descriptor.

パラメータ:
  • desc_type -- descriptor type.
  • desc_index -- index of the descriptor.
  • len -- descriptor length.
  • endpoint -- ignored.
getString(index, length, langid=None)[ソース]
Retrieve the string descriptor specified by index
and langid from a device.
パラメータ:
  • index -- index of descriptor in the device.
  • length -- number of bytes of the string (ignored)
  • langid -- Language ID. If it is omitted, the first language will be used.
interruptRead(endpoint, size, timeout=100)[ソース]

Performs a interrupt read request to the endpoint specified.

パラメータ:
  • endpoint -- endpoint number.
  • size -- number of bytes to read.
  • timeout -- operation timeout in milliseconds. (default: 100)

Returns a tuple with the data read.

interruptWrite(endpoint, buffer, timeout=100)[ソース]

Perform a interrupt write request to the endpoint specified.

パラメータ:
  • endpoint -- endpoint number.
  • buffer -- sequence data buffer to write. This parameter can be any sequence type.
  • timeout -- operation timeout in milliseconds. (default: 100)

Returns the number of bytes written.

releaseInterface()[ソース]

Release an interface previously claimed with claimInterface.

reset()[ソース]

Reset the specified device by sending a RESET down the port it is connected to.

resetEndpoint(endpoint)[ソース]

Reset all states for the specified endpoint.

パラメータ:endpoint -- endpoint number.
setAltInterface(alternate)[ソース]

Sets the active alternate setting of the current interface.

パラメータ:alternate -- an alternate setting number or an Interface object.
setConfiguration(configuration)[ソース]

Set the active configuration of a device.

パラメータ:configuration -- a configuration value or a Configuration object.
class usb.legacy.Endpoint(ep)[ソース]

ベースクラス: object

Endpoint descriptor object.

class usb.legacy.Interface(intf)[ソース]

ベースクラス: object

Interface descriptor object.

usb.legacy.busses()[ソース]

Returns a tuple with the usb busses.

usb.libloader モジュール

exception usb.libloader.LibraryException[ソース]

ベースクラス: OSError

exception usb.libloader.LibraryNotFoundException[ソース]

ベースクラス: usb.libloader.LibraryException

exception usb.libloader.NoLibraryCandidatesException[ソース]

ベースクラス: usb.libloader.LibraryNotFoundException

exception usb.libloader.LibraryNotLoadedException[ソース]

ベースクラス: usb.libloader.LibraryException

exception usb.libloader.LibraryMissingSymbolsException[ソース]

ベースクラス: usb.libloader.LibraryException

usb.libloader.locate_library(candidates, find_library=<function find_library>)[ソース]

Tries to locate a library listed in candidates using the given find_library() function (or ctypes.util.find_library). Returns the first library found, which can be the library's name or the path to the library file, depending on find_library(). Returns None if no library is found.

arguments: * candidates -- iterable with library names * find_library -- function that takes one positional arg (candidate)

and returns a non-empty str if a library has been found. Any "false" value (None,False,empty str) is interpreted as "library not found". Defaults to ctypes.util.find_library if not given or None.
usb.libloader.load_library(lib, name=None, lib_cls=None)[ソース]

Loads a library. Catches and logs exceptions.

Returns: the loaded library or None

arguments: * lib -- path to/name of the library to be loaded * name -- the library's identifier (for logging)

Defaults to None.
  • lib_cls -- library class. Defaults to None (-> ctypes.CDLL).
usb.libloader.load_locate_library(candidates, cygwin_lib, name, win_cls=None, cygwin_cls=None, others_cls=None, find_library=None, check_symbols=None)[ソース]

Locates and loads a library.

Returns: the loaded library

arguments: * candidates -- candidates list for locate_library() * cygwin_lib -- name of the cygwin library * name -- lib identifier (for logging). Defaults to None. * win_cls -- class that is used to instantiate the library on

win32 platforms. Defaults to None (-> ctypes.CDLL).
  • cygwin_cls -- library class for cygwin platforms.
    Defaults to None (-> ctypes.CDLL).
  • others_cls -- library class for all other platforms.
    Defaults to None (-> ctypes.CDLL).
  • find_library -- see locate_library(). Defaults to None.
  • check_symbols -- either None or a list of symbols that the loaded lib
    must provide (hasattr(<>)) in order to be considered valid. LibraryMissingSymbolsException is raised if any symbol is missing.

raises: * NoLibraryCandidatesException * LibraryNotFoundException * LibraryNotLoadedException * LibraryMissingSymbolsException

usb.util モジュール

usb.util - ユーティリティ関数群。

本モジュールからは以下がエスクポートされます:

endpoint_address - return the endpoint absolute address. endpoint_direction - return the endpoint transfer direction. endpoint_type - return the endpoint type ctrl_direction - return the direction of a control transfer build_request_type - build a bmRequestType field of a control transfer. find_descriptor - find an inner descriptor. claim_interface - explicitly claim an interface. release_interface - explicitly release an interface. dispose_resources - release internal resources allocated by the object. get_langids - retrieve the list of supported string languages from the device. get_string - retrieve a string descriptor from the device.

usb.util.build_request_type(direction, type, recipient)[ソース]

Build a bmRequestType field for control requests.

These is a conventional function to build a bmRequestType for a control request.

The direction parameter can be CTRL_OUT or CTRL_IN. The type parameter can be CTRL_TYPE_STANDARD, CTRL_TYPE_CLASS, CTRL_TYPE_VENDOR or CTRL_TYPE_RESERVED values. The recipient can be CTRL_RECIPIENT_DEVICE, CTRL_RECIPIENT_INTERFACE, CTRL_RECIPIENT_ENDPOINT or CTRL_RECIPIENT_OTHER.

Return the bmRequestType value.

usb.util.claim_interface(device, interface)[ソース]

Explicitly claim an interface.

PyUSB users normally do not have to worry about interface claiming, as the library takes care of it automatically. But there are situations where you need deterministic interface claiming. For these uncommon cases, you can use claim_interface.

If the interface is already claimed, either through a previously call to claim_interface or internally by the device object, nothing happens.

usb.util.create_buffer(length)[ソース]

Create a buffer to be passed to a read function.

A read function may receive an out buffer so the data is read inplace and the object can be reused, avoiding the overhead of creating a new object at each new read call. This function creates a compatible sequence buffer of the given length.

usb.util.ctrl_direction(bmRequestType)[ソース]

Return the direction of a control request.

The bmRequestType parameter is the value of the bmRequestType field of a control transfer. The possible return values are CTRL_OUT or CTRL_IN.

usb.util.dispose_resources(device)[ソース]

Release internal resources allocated by the object.

Sometimes you need to provide deterministic resources freeing, for example to allow another application to talk to the device. As Python does not provide deterministic destruction, this function releases all internal resources allocated by the device, like device handle and interface policy.

After calling this function, you can continue using the device object normally. If the resources will be necessary again, it will be allocated automatically.

usb.util.endpoint_address(address)[ソース]

Return the endpoint absolute address.

The address parameter is the bEndpointAddress field of the endpoint descriptor.

usb.util.endpoint_direction(address)[ソース]

Return the endpoint direction.

The address parameter is the bEndpointAddress field of the endpoint descriptor. The possible return values are ENDPOINT_OUT or ENDPOINT_IN.

usb.util.endpoint_type(bmAttributes)[ソース]

Return the transfer type of the endpoint.

The bmAttributes parameter is the bmAttributes field of the endpoint descriptor. The possible return values are: ENDPOINT_TYPE_CTRL, ENDPOINT_TYPE_ISO, ENDPOINT_TYPE_BULK or ENDPOINT_TYPE_INTR.

usb.util.find_descriptor(desc, find_all=False, custom_match=None, **args)[ソース]

Find an inner descriptor.

find_descriptor works in the same way as the core.find() function does, but it acts on general descriptor objects. For example, suppose you have a Device object called dev and want a Configuration of this object with its bConfigurationValue equals to 1, the code would be like so:

>>> cfg = util.find_descriptor(dev, bConfigurationValue=1)

You can use any field of the Descriptor as a match criteria, and you can supply a customized match just like core.find() does. The find_descriptor function also accepts the find_all parameter to get an iterator instead of just one descriptor.

usb.util.get_langids(dev)[ソース]

Retrieve the list of supported Language IDs from the device.

Most client code should not call this function directly, but instead use the langids property on the Device object, which will call this function as needed and cache the result.

USB LANGIDs are 16-bit integers familiar to Windows developers, where for example instead of en-US you say 0x0409. See the file USB_LANGIDS.pdf somewhere on the usb.org site for a list, which does not claim to be complete. It requires "system software must allow the enumeration and selection of LANGIDs that are not currently on this list." It also requires "system software should never request a LANGID not defined in the LANGID code array (string index = 0) presented by a device." Client code can check this tuple before issuing string requests for a specific language ID.

dev is the Device object whose supported language IDs will be retrieved.

The return value is a tuple of integer LANGIDs, possibly empty if the device does not support strings at all (which USB 3.1 r1.0 section 9.6.9 allows). In that case client code should not request strings at all.

A USBError may be raised from this function for some devices that have no string support, instead of returning an empty tuple. The accessor for the langids property on Device catches that case and supplies an empty tuple, so client code can ignore this detail by using the langids property instead of directly calling this function.

usb.util.get_string(dev, index, langid=None)[ソース]

Retrieve a string descriptor from the device.

dev is the Device object which the string will be read from.

index is the string descriptor index and langid is the Language ID of the descriptor. If langid is omitted, the string descriptor of the first Language ID will be returned.

Zero is never the index of a real string. The USB spec allows a device to use zero in a string index field to indicate that no string is provided. So the caller does not have to treat that case specially, this function returns None if passed an index of zero, and generates no traffic to the device.

The return value is the unicode string present in the descriptor, or None if the requested index was zero.

usb.util.release_interface(device, interface)[ソース]

Explicitly release an interface.

This function is used to release an interface previously claimed, either through a call to claim_interface or internally by the device object.

Normally, you do not need to worry about claiming policies, as the device object takes care of it automatically.

モジュール内容

PyUSB - Pythonから簡単にUSBにアクセスできるようにします。

本パッケージはイカのモジュールとサブバッケージをエクスポートします:

core - 主たるUSB実装、 legacy - 0.xバージョン利用者互換レイヤ、backend - バックエンド実装のサポート、 control - USB標準制御要求(standard control requests)、 libloader - バックエンドライブラリ読み込みのためのヘルパーモジュール

バージョン1.0以降では、メインのPyUSB実装は「usb.core」モジュールにあります。 新しいアプリケーションではそれを使用することをお勧めします。