Writing to a USB device via WinUsb

2019-09-14 08:00发布

问题:

LAST UPDATE :

Now I have a string compatible with my device (it had to be in a specific format of non-printable bytes), but an infinite loop. The program never exits WinUsb_WritePipe.

ORIGINAL TROUBLE :

After initializing all my WinUsb interface (as precised in this well done MSDN tutorial : https://msdn.microsoft.com/en-us/library/windows/hardware/ff540174(v=vs.85).aspx)

I get everything well setup and sending true without error, except for the last step.

PWINUSB_INTERFACE_HANDLE piUsbHandle = new WINUSB_INTERFACE_HANDLE();
USB_INTERFACE_DESCRIPTOR *eUsbInterfaceDescriptor = new USB_INTERFACE_DESCRIPTOR();

WinUsb_Initialize(sfhDeviceHandle, piUsbHandle) //true

devInfo->winUsbHandle = *piUsbHandle;

WinUsb_QueryInterfaceSettings(*piUsbHandle, 1, eUsbInterfaceDescriptor) //true

for (int i= 0; i <= eUsbInterfaceDescriptor->bNumEndpoints - 1; i++)
{
        if (!WinUsb_QueryPipe((WINUSB_INTERFACE_HANDLE)devInfo->winUsbHandle, 1, (unsigned char)i, &eWinUsbPipeInformation))
        {
            return NULL;
        }
        if (((eWinUsbPipeInformation.PipeType == UsbdPipeTypeBulk)
             & this->isUsbEndPointDirectionIn(eWinUsbPipeInformation.PipeId)))
        {
            UtilityFunctions::displayTrace("initializeDevice : devInfo->bulkInPipe set");
            std::cout << "New In Pipe = " <<  (int)eWinUsbPipeInformation.PipeId << std::endl;
            devInfo->bulkInPipe = eWinUsbPipeInformation.PipeId;
        }
        else if (((eWinUsbPipeInformation.PipeType == UsbdPipeTypeBulk)
                  & this->isUsbEndpointDirectionOut(eWinUsbPipeInformation.PipeId)))
        {
            UtilityFunctions::displayTrace("initializeDevice : devInfo->bulkOutPipe set");
            std::cout << "New Out Pipe = " <<  (int)eWinUsbPipeInformation.PipeId << std::endl;
            devInfo->bulkOutPipe = eWinUsbPipeInformation.PipeId;
        }
        else if (((eWinUsbPipeInformation.PipeType == UsbdPipeTypeInterrupt)
                  & this->isUsbEndPointDirectionIn(eWinUsbPipeInformation.PipeId)))
        {
            UtilityFunctions::displayTrace("initializeDevice : devInfo->InterruptInPipe set");
            devInfo->interruptInPipe = eWinUsbPipeInformation.PipeId;
        }
        else if (((eWinUsbPipeInformation.PipeType == UsbdPipeTypeInterrupt)
                  & this->isUsbEndpointDirectionOut(eWinUsbPipeInformation.PipeId)))
        {
            UtilityFunctions::displayTrace("initializeDevice : devInfo->InterruptOutPipe set");
            devInfo->interruptOutPipe = eWinUsbPipeInformation.PipeId;
        }
        else
        {
            std::cout << "No settings for " << i << std::endl;
            continue;
        }
}

(devInfo is a custom object containing a WINUSB_INTERFACE_HANDLE and the PipeId needed to write on the device)

It prints

initializeDevice : bNumEndpoints => 6
initializeDevice : devInfo->bulkOutPipe set
New Out Pipe = 1
initializeDevice : devInfo->bulkInPipe set
New In Pipe = 129
initializeDevice : devInfo->bulkOutPipe set
New Out Pipe = 2
initializeDevice : devInfo->bulkOutPipe set
New Out Pipe = 4
initializeDevice : devInfo->bulkInPipe set
New In Pipe = 134
initializeDevice : devInfo->bulkInPipe set
New In Pipe = 136

But for the last step, which is writing :

UCHAR ucBuffer[] = "Hello world";
ULONG uiBytesToWrite = 11;
ULONG ulBytesWritten = 0;

if (devInfo->winUsbHandle == INVALID_HANDLE_VALUE)
{
    return;
}


WinUsb_WritePipe(devInfo->winUsbHandle, devInfo->bulkOutPipe, ucBuffer, uiBytesToWrite, &ulBytesWritten, 0);//ERROR 87 : INVALID PARAMETER

I've tried with every PipeId possible (from 0 to 255 since it's on one byte) but still gets the same error. The WinUsb_WritePipe MSDN documentation doesn't say anything about error 87.

标签: c++ winusb