USB device detection - Windows & Linux (FT232R)

前提是你 提交于 2019-12-11 18:06:56

问题


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

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.


回答1:


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

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;
          }

    }

  }
}

I dont have idea for Linux..:(

Hope this helps..



来源:https://stackoverflow.com/questions/20773576/usb-device-detection-windows-linux-ft232r

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