将libusb代码移植到Python:中断传输

Porting libusb code to Python: interrupt transfer

下面是在 C++ 中使用 libusb 执行中断传输的两行代码:

libusb_fill_interrupt_transfer(transfer_receive, handle, LIBUSB_ENDPOINT_IN | USB_INTERFACE_OUT, buffer_receive, sizeof(buffer_receive), cb_in, &usb_data, 30000);
r = libusb_submit_transfer(transfer_receive);

如何在 Python 中使用 PyUSB 做同样的事情?

有函数interruptWrite(self, endpoint, buffer, timeout = 100)interruptRead(self, endpoint, size, timeout = 100),见

https://github.com/walac/pyusb/blob/master/usb/legacy.py

interruptRead()函数类似于C++代码(轮询设备中断,从设备的中断IN端点接收中断数据)

C++中的LIBUSB_ENDPOINT_IN | USB_INTERFACE_OUT|是按位或)类似使用指定的USB中断接口USB_INTERFACE_OUT从指定端点查询数据LIBUSB_ENDPOINT_IN

在后台 PyUSB 对 bulkinterrupt 使用相同的函数 (write())等时传输,只有控制传输有特殊语法。 interruptWrite()interruptRead() 也使用 write() 抽象底层原生 USB 传输类型的函数

USB has four flavors of transfers: bulk, interrupt, isochronous and control. [ ... ]

Control transfer is the only transfer that has structured data described in the spec, the others just send and receive raw data from USB point of view. Because of it, you have a different function to deal with control transfers, all the other transfers are managed by the same functions.

You issue a control transfer through the ctrl_transfer method. It is used both for OUT and IN transfers. The transfer direction is determined from the bmRequestType parameter.

来源:https://github.com/walac/pyusb/blob/master/docs/tutorial.rst

一般情况下,如果设备有中断未决,则设备仅在中断传输时应答,因此主机通过中断传输轮询设备的中断。这在发送中断请求(传输)并等待接收应答的 C++ 代码中变得很明显

http://mvidner.blogspot.com/2017/01/usb-communication-with-python-and-pyusb.html