串口操作,如何获取COM4前的串口名字(也就是STM32F10X CDC...这串东西)

解决方案 »

  1.   

    上面的函数使用的是setupapi,记得要包含相应的头文件和库文件。还有,
    SetupDiGetDeviceRegistryProperty 可以取得设备相关属性,例如设备名可以参考以下一个例子,需要改动一些地方。
    #include <setupapi.h>   
    #pragma comment(lib, "setupapi.lib")const GUID GUID_DEVINTERFACE_DISPLAY_ADAPTER = { 0x5b45201d, 0xf2f2, 0x4f3b, { 0x85, 0xbb,  0x30,  0xff,  0x1f,  0x95,  0x35,  0x99 } };long GetDisplayDeviceInfo(unsigned long nAdapterIndex, tagI2CDeviceStruct* pobjI2CDevice)
    {
        pobjI2CDevice->nDisplayDeviceLength = 0;    HDEVINFO hDevInfoX = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISPLAY_ADAPTER, NULL, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 
        if (hDevInfoX == INVALID_HANDLE_VALUE)   
        {   
            //printf("Failed to get device handle. Error[%d]", GetLastError());   
            return DVDC_ERR_GET_CLASS_DEV;   
        }   
        SP_DEVINFO_DATA DeviceInfoData;   
        //DWORD dwDeviceIndex;       // Enumerate through all devices in Set.   
        DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);   
        if (!SetupDiEnumDeviceInfo(hDevInfoX, nAdapterIndex, &DeviceInfoData))   
        { 
            SetupDiDestroyDeviceInfoList(hDevInfoX); 
            if (GetLastError() == ERROR_NO_MORE_ITEMS) 
                return DVDC_ERR_NOT_FOUND_DISPLAY_ADAPTER; 
            else
                return DVDC_ERR_FIND_DISPLAY_ADAPTER; 
        }     DWORD   DataT = 0;   
        DWORD   buffersize   =   0; 
        LPTSTR   buffer   =   NULL;       while(!SetupDiGetDeviceRegistryProperty(   
            hDevInfoX,   
            &DeviceInfoData,   
            SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,   
            &DataT,   
            (PBYTE)buffer,   
            buffersize,   
            &buffersize))   
        {   
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)   
            {   
                //   Change   the   buffer   size.   
                if (buffer)   
                    LocalFree(buffer);   
                buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);   
            }   
            else   
            {   
                printf("Failed to get device guid. Error[%d] \n", GetLastError());   
                break;   
            }   
        }       if (buffer)   
        {
    #ifdef _UNICODE
            int iMultiBytesLength = WideCharToMultiByte ( 
                CP_ACP,                    // ANSI 代码页                          
                WC_COMPOSITECHECK,        // 检查重音字符                          
                buffer,            // 原Unicode 串                          
                -1,                        // -1 意思是串以0x00结尾                          
                NULL,                    // 目的char字符串                          
                0,                        // 缓冲大小                          
                NULL,                    // 肥缺省字符串                          
                NULL);                    // 忽略这个参数        if (iMultiBytesLength <= 0)
            {
                SetupDiDestroyDeviceInfoList(hDevInfoX); 
                return DVDC_ERR_GET_DEVICE_PATH_LENGTH; 
            }        if (iMultiBytesLength > sizeof(pobjI2CDevice->pcDisplayDevicePath))
            {
                SetupDiDestroyDeviceInfoList(hDevInfoX); 
                return DVDC_ERR_DEVICE_PATH_LENGTH_SMALL; 
            }        WideCharToMultiByte ( 
                CP_ACP,                        // ANSI 代码页                          
                WC_COMPOSITECHECK,            // 检查重音字符                          
                buffer,                // 原Unicode 串                          
                -1,                            // -1 意思是串以0x00结尾                          
                pobjI2CDevice->pcDisplayDevicePath,            // 目的char字符串                          
                iMultiBytesLength,            // 缓冲大小                          
                NULL,                        // 肥缺省字符串                          
                NULL);                        // 忽略这个参数        //printf("  path:[%s]\n", pcResult);   
            pobjI2CDevice->nDisplayDeviceLength = iMultiBytesLength;
    #else
            //MessageBox(NULL, "not unicode", "xxxx", MB_OK);
            if ((strlen(buffer) + 1) > sizeof(pobjI2CDevice->pcDisplayDevicePath))
            {
                SetupDiDestroyDeviceInfoList(hDevInfoX); 
                return DVDC_ERR_DEVICE_PATH_LENGTH_SMALL; 
            }        pobjI2CDevice->nDisplayDeviceLength = strlen(buffer) + 1;
            strcpy_s(pobjI2CDevice->pcDisplayDevicePath, sizeof(pobjI2CDevice->pcDisplayDevicePath), buffer);
            pobjI2CDevice->pcDisplayDevicePath[pobjI2CDevice->nDisplayDeviceLength] = 0;
    #endif
        }    // Cleanup   
        SetupDiDestroyDeviceInfoList(hDevInfoX);   
        return DVDC_ERR_SUCCESS;
    }