windows 编程 USB 传输电缆/与 USB 设备驱动程序交谈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/608607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:06:19  来源:igfitidea点击:

Programming a USB transfer cable / talking to a USB device driver

windowsusbdevice-driver

提问by Josh Kelley

How do I programmatically access a USB transfer cable (such as Belkin's Easy Transfer Cable) from Windows?

如何从 Windows 以编程方式访问 USB 传输电缆(例如 Belkin 的Easy Transfer Cable)?

I'm familiar with libusb-win32, but from what I can tell, using that with newer devices and with Windows Vista seems iffy.

我熟悉libusb-win32,但据我所知,在较新的设备和 Windows Vista 上使用它似乎有些不确定。

I know that Windows Easy Transfercan do this. How do I write code that does the same thing as Windows Easy Transfer?

我知道Windows 轻松传送可以做到这一点。如何编写与 Windows 轻松传送功能相同的代码?

If there is no canned documentation on how to do this, I'm willing to do some digging, but I don't know where to start. How do I watch what Windows Easy Transfer is doing to find out how it does it? I see that Windows even gives transfer cables their own category in the Device Manager, "Transfer Cable Devices." How do I do low-level communication with one of these these drivers?

如果没有关于如何做到这一点的罐头文档,我愿意做一些挖掘,但我不知道从哪里开始。我如何观看 Windows 轻松传送正在执行的操作以了解它是如何执行的?我看到 Windows 甚至在设备管理器“传输电缆设备”中为传输电缆提供了自己的类别。我如何与这些驱动程序之一进行低级通信?

采纳答案by Josh Kelley

I found out that Microsoft now offers WinUSBfor simple user-mode communication with USB devices. (A WinUSB device driver must first be installed for the device; this is somewhat similar to a libusb-win32 device driver.) WinUSB works on XP (SP2 and above) and Vista.

我发现 Microsoft 现在提供WinUSB用于与 USB 设备进行简单的用户模式通信。(必须首先为设备安装 WinUSB 设备驱动程序;这有点类似于 libusb-win32 设备驱动程序。) WinUSB 可用于 XP(SP2 及更高版本)和 Vista。

The Easy Transfer Cable uses WinUSB for its device driver, so I was able to communicate with it by following the example code in Microsoft's WinUSB howtodocument.

Easy Transfer Cable 使用 WinUSB 作为其设备驱动程序,因此我能够按照 Microsoft 的WinUSB howto文档中的示例代码与其进行通信。

回答by Byron Whitlock

You will need to use the low level win32 API to do this. Microsoft has some nice examples here on accessing a Human Interface Device. The transfer cable isn't explicitly an HID like a mouse or keyboard, but it conforms to the HID spec.

您将需要使用低级 win32 API 来执行此操作。微软在这里有一些关于访问人机接口设备的很好的例子。传输电缆不像鼠标或键盘那样是明确的 HID,但它符合 HID 规范。

For example, to get the name of the USB device you would call

例如,要获取您要调用的 USB 设备的名称

HidD_GetProductString(...)

http://msdn.microsoft.com/en-us/library/ms790920.aspx

http://msdn.microsoft.com/en-us/library/ms790920.aspx

There is lots more there, you should definitely take a look at the sample c app that works for all versions of windows from 2000 to Vista.

还有更多内容,您绝对应该查看适用于从 2000 到 Vista 的所有 Windows 版本的示例 c 应用程序。

http://msdn.microsoft.com/en-us/library/dd163258.aspx

http://msdn.microsoft.com/en-us/library/dd163258.aspx

Good Luck!

祝你好运!

回答by KaiFa Liu

You need to have an USB data transfer cable (also called USB data link cable) which support API or SDK, then use the following code:

您需要有一条支持API或SDK的USB数据传输线(也称为USB数据链路线),然后使用以下代码:

void CU2uDlg::OnOK() 
{
BYTE        buf[65530];
LPU2URET    pU2uRet;
BOOL        bRet;
int         ret;
CString     msgstr;

ret = u2u_open();
if (ret == -1){
    AfxMessageBox("Open U2U device Success.");
}else{
    msgstr.Format("Open U2U device fail,return:%d", ret);
    AfxMessageBox(msgstr);
    return;
}

//send data
bRet = u2u_SendData(buf, 65530, ret);
if(!bRet)
{
    msgstr.Format("Send data error,return:%d", ret);
    AfxMessageBox(msgstr);
    return;
}

//receive data
while (1){
    bRet = u2u_RecvData(recvData, dataLen, ret);
    if( !bRet )
    {
        msgstr.Format("Receive data error,return:%d", ret);
        AfxMessageBox(msgstr);
        u2u_close();
        return;
    }else{
        break;
    }
}
u2u_close();


}

See: Reference1, Reference2

请参阅: 参考文献 1参考文献 2