通过WinUsb写入USB设备

时间:2017-05-24 12:16:31

标签: c++ winusb

最后更新:

现在我有一个与我的设备兼容的字符串(它必须采用特定格式的非可打印字节),但是无限循环。该程序永远不会退出WinUsb_WritePipe。

原始问题:

初始化我的所有WinUsb界面后(在这个做得很好的MSDN教程中精确准备:https://msdn.microsoft.com/en-us/library/windows/hardware/ff540174(v=vs.85).aspx

除了最后一步之外,我得到了所有设置并且无错误地发送的确认。

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是一个自定义对象,包含WINUSB_INTERFACE_HANDLEPipeId需要在设备上写入的内容)

打印

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

但是最后一步是写作:

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

我尝试过每个PipeId都可能(从0到255,因为它在一个字节上),但仍然会得到相同的错误。 The WinUsb_WritePipe MSDN documentation对错误87没有任何说明。

0 个答案:

没有答案