• MFC 使用用指定USB设备串口


    在做设备串口通讯时,往往需要自动连接到想要连接的usb转串口设备上。

    #include <Setupapi.h>
    int CMFCApplication1Dlg::FindUSBCOM()
    {
    #pragma comment(lib, "setupapi.lib")
        HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            AfxMessageBox(_T("获取系统设备列表失败"));
            return -1;
        }
        SP_DEVINFO_DATA deviceInfoData;
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        deviceInfoData.DevInst = 0;
        deviceInfoData.Reserved = 0;
    
    
        DWORD regDataType;
        LPTSTR buffer = new TCHAR[200];
        DWORD buffersize = 200;
    
        LPTSTR bufferVid = new TCHAR[200];
        DWORD buffersizeVid = 200;
    
        LPTSTR bufferPortNum = new TCHAR[200];
        DWORD buffersizePortNum = 200;
    
        int portNum = 0;//Com1 portNum=1
    
        for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceInfoData); i++)
        {
    
            
            buffersize = 200;
            SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_CLASS, &regDataType, (PBYTE)buffer, buffersize, &buffersize);
    
            if (CString(buffer).MakeUpper() == "PORTS")
            {
                
                buffersizePortNum = 200;            
                SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, &regDataType, (PBYTE)bufferPortNum, buffersizePortNum, &buffersizePortNum);
                int comIndex = CString(bufferPortNum).MakeUpper().Find(_T("COM"));
                if (comIndex > -1)
                {
                    //Silicon Labs CP210x USB to UART Bridge (COM4)            
                    portNum = _ttoi(CString(bufferPortNum).Mid(comIndex + 3, 1));
    
                }
            
                buffersizeVid = 200;
                SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_HARDWAREID, &regDataType, (PBYTE)bufferVid, buffersizeVid, &buffersizeVid);
    
                if (CString(bufferVid).MakeUpper().Find(_T("VID_10C4")) > -1)
                {
                    if (buffer != nullptr)
                    {
                        delete[] buffer;
                    }
                    if (bufferVid != nullptr)
                    {
                        delete[] bufferVid;
                    }
                    if (bufferPortNum != nullptr)
                    {
                        delete[] bufferPortNum;
                    }
                    // 释放设备  
                    SetupDiDestroyDeviceInfoList(hDevInfo);
                    return portNum;
    
                }
    
            }//end if ports
    
    
        }// end for
        if (buffer != nullptr)
        {
            delete[] buffer;
        }
        if (bufferVid != nullptr)
        {
            delete[] bufferVid;
        }
        if (bufferPortNum != nullptr)
        {
            delete[] bufferPortNum;
        }
        // 释放设备  
        SetupDiDestroyDeviceInfoList(hDevInfo);
        return portNum;
    }

      

       

  • 相关阅读:
    JavaScript节点属性
    main函数的参数
    CGI
    open()函数 linux中open函数使用
    海伦公式
    C语言字符串操作函数
    makefile(一)
    makefile
    第一天
    时间小憩
  • 原文地址:https://www.cnblogs.com/ike_li/p/7476956.html
Copyright © 2020-2023  润新知