none
怎么用C#测试USB端口读写文件是否正常 RRS feed

答案

  • Hi erdao,

    我不知道你能不能够理解英文的文章,但是我还是要把一份英文的博客贴出来,这里有对C#来读取USB的最佳解释:

    http://www.developerfusion.com/article/84338/making-usb-c-friendly/

    Finding the device 部分详述了怎么去找到新添加的USB device:

    Finding the device

    The first step in connecting to the device is to locate it. Of course, as is the nature of a USB device, it can be inserted or removed at any time. Any serious software solution needs to take this into consideration. To detect when a device is inserted and removed, a program must register its interest in such events by sending its main window handle to RegisterDeviceNotification. When a new device is detected or a device removed, Windows sends a WM_DEVICECHANGE message. Override WndProc on a Form to handle the messages. Once notification of a new device has been received, the program needs to search the list of devices currently on the USB bus to check if the device that was inserted is the one it is interested in.

    To get the device list, call SetupDiGetClassDevs, one of the USB API calls in setupapi.dll. Before we go into details, I need to tell you about the Windows USB API documentation. Unless you have already installed the Windows DDK, there practically isn’t any! This isn’t made easier by the fact that there are many USB functions in the API, only a few of which you will ever use. You can find information about the API functions from the Internet, but it is sparse at best.

    SetupDiGetClassDevs provides information about all devices of a certain class (e.g. HID) currently connected to the client PC. The first parameter to the function is a GUID that specifies the class of device to look for. Use HidD_GetHidGuid in hid.dll to obtain the GUID that Windows uses to represent the HID class. The function reserves a block of memory (an InfoSet) that holds an array with one entry per device. The entries in the InfoSet have to be enumerated with calls to SetupDiEnumDeviceInterfaces in ‘setupapi.dll’. Each call to this function fills a DeviceInterfaceData structure with details about a device in the list with a non-zero return code.

    But we’re still not there! We have to drill down even further. Now we have to fill a DeviceInterfaceData-Detail structure using a call to SetupDiGetDeviceInterfaceDetail. This finally provides us with a string that represents a path to the device (just like a file path). This path contains the VID and PID of the device amongst other things. You can check portions of this string against the VID and PID of your device. If they match, you’ve found your device!

    Device VID 0x054C
    Device PID 0x1000
    Path \\?\hid#vid_054c
    &pid_1000#6&2dd74f76&0&0000#{4d1e55b2-
    f16f-11cf-88cb-001111000030}

    In the above example, the VID and PID can be seen just after the HID specifier (vid_054c&pid_1000). A simple substring match should be sufficient (e.g. string.IndexOf()).

    Once all this is complete, you must remember to free the infoset with a call to SetupDiDestroyDevice-InfoList.

    文章的大致意思是USB有一些比较重要的参数Vendor ID (VID) and Product ID (PID)还有一个HID,而具体获取这些参数来作为我们判断的依据的,则是要靠使用一个叫Windwos Driverr SDK的API完成的,例如这个类:

    http://msdn.microsoft.com/zh-cn/library/windows/hardware/ff551069(v=vs.85).aspx

    如果能够检测到相应信息,当然可以判断出这个东西是否正常工作。当然你可以参照以下的一个中文博客来看怎么来具体的访问:

    http://www.cnblogs.com/xidongs/archive/2011/11/28/2266100.html



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月14日 5:48
    版主