Writing to a USB device via WinUsb

烂漫一生 提交于 2019-12-25 09:20:21

问题


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.

来源:https://stackoverflow.com/questions/44158204/writing-to-a-usb-device-via-winusb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!