USB 设备检测 - Windows &Linux (FT232R) [英] USB device detection - Windows & Linux (FT232R)
问题描述
我如何在 windows 和 linux 中枚举和识别(获取 pid、vid 和串行)FT232R 芯片.我需要:1. 在程序开始时枚举已经插入的设备2.检测USB设备的插入3.获取PID、VID、Serial
How can I enumerate and identify (get pid, vid and serial) FT232R chips in windows and linux. I need to: 1. at start of program enumerate already plugged devices 2. detect plugging of usb device 3. get PID, VID, Serial
我需要在 windows 和 linux 中做到这一点.我知道有适用于 Windows 和 Linux 的 libusb,但我对 USB 没有太多经验.代码示例最好.
And I need to do that in windows and linux. I know there is libusb for windows as well as for linux, but I don't have that much experience with USB. Code example would be best.
推荐答案
您应该在 Windows 中使用 SetupAPIs 来获取设备信息,如硬件 ID(包含 vid 和 pid)并检测插入/拔出,请参见此链接中的示例注册设备通知
You should use SetupAPIs in Windows for getting device information like hardware id(contains vid and pid both) and to detect the plugging/unplugging see example in this link Registering for Device Notification
要使用 SetupAPI,您可以使用以下代码作为参考,并根据您的要求添加/修改.
To use SetupAPI you can use below code as reference and add/modify according to your requirement.
#include "stdafx.h"
#include <stdlib.h>
#include <Windows.h>
// Link to setapi.lib
#include <setupapi.h>
void GetDeviceInfo()
{
GUID gUSBGuid;
DWORD ClassGuidListSize = 1;
DWORD RequiredSize = NULL;
//if device shown under "USB" node in Devmgr, else see inf for classname
BOOL bres = SetupDiClassGuidsFromName((PCTSTR)"USB",
&gUSBGuid,//GUID will get populated
ClassGuidListSize,
&RequiredSize);
HDEVINFO hDevInfo = SetupDiGetClassDevs(&gUSBGuid,NULL,NULL,DIGCF_PRESENT);
if (INVALID_HANDLE_VALUE != hDevInfo)
{
BOOL bResult = FALSE;
SP_DEVICE_INTERFACE_DATA tDeviceInterfaceData;
tDeviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
for (int nMemberIndex = 0; TRUE ; nMemberIndex++)
{
SP_DEVINFO_DATA tSpDevInfoData;
tSpDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//Get the tSpDevInfoData for the instance ID
bResult = SetupDiEnumDeviceInfo(hDevInfo,nMemberIndex,&tSpDevInfoData);
if(bResult)
{
TCHAR *szHardwareId = new TCHAR[128] ;
DWORD dwtype = REG_SZ;
SetupDiGetDeviceRegistryProperty(hDevInfo,&tSpDevInfoData,SPDRP_HARDWAREID
,&dwtype,(PBYTE szHardwareId,256,NULL);
//code to process szHardwareId
delete szHardwareId;
break;
}
}
}
}
我不知道 Linux ..:(
I dont have idea for Linux..:(
希望这有帮助..
这篇关于USB 设备检测 - Windows &Linux (FT232R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!