我有一个Python代码,使用PyUSB与一个看起来像这样的设备进行通信:
import usb.core
import usb.util
device = usb.core.find(idVendor=0xC251, idProduct=0x2201)
packet = [90]
number_of_bytes_sent = device.ctrl_transfer(
bmRequestType = 0x21,
bRequest = 9,
wValue = 0x200,
wIndex = 0,
data_or_wLength = packet,
)
print(f'{number_of_bytes_sent} bytes were sent')
我得到
usb.core.USBError: [Errno 5] Input/Output Error
在CCD_ 1呼叫中。这段代码在使用相同设备的Linux上运行,但在Windows 10上失败了。问题出在哪里?
尝试随机事物时,我发现在Windows中用长度为64的零填充是可行的。不要问我为什么。所以现在我的代码是:
import usb.core
import usb.util
import platform
device = usb.core.find(idVendor=0xC251, idProduct=0x2201)
packet = [90]
if platform.system() == 'Windows':
packet = packet + (64-len(packet))*[0] # I don't know why this has to be done on Windows.
number_of_bytes_sent = device.ctrl_transfer(
bmRequestType = 0x21,
bRequest = 9,
wValue = 0x200,
wIndex = 0,
data_or_wLength = packet,
)
print(f'{number_of_bytes_sent} bytes were sent')