Search code examples
pythonwindowspyusb

PyUSB usb.core.USBError: [Errno 5] Input/Output Error in Windows


I have a Python code using PyUSB to talk to a device that looks like this:

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')

and I am getting

usb.core.USBError: [Errno 5] Input/Output Error

in the device.ctrl_transfer call. That exact code is working on Linux with this same device, but failing on Windows 10. What can be the problem?


Solution

  • Trying random things I found that filling with zeros up to a length of 64 works in Windows. Don't ask me why. So now my code is:

    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')