VC++串口笔记(一)枚举电脑串口
1.从注册表读取信息:
代码如下:
从注册表读取串口
1 void CCommDlg::EnumCommPort(CListBox *pListBox)
2 {
3 HKEY hKey;
4 if(ERROR_SUCCESS ==::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm",NULL, KEY_READ, &hKey ))//打开串口注册表对应的键值
5 {
6 int i=0;
7 TCHAR RegKeyName[128],SerialPortName[128];
8 DWORD dwLong,dwSize;
9 while(TRUE)
10 {
11 dwLong = dwSize=sizeof(RegKeyName);
12 if(ERROR_NO_MORE_ITEMS==::RegEnumValue(hKey,i,RegKeyName,&dwLong,NULL,NULL,(PUCHAR)SerialPortName, &dwSize))//枚举串口
13 {
14 break;
15 }
16 if(!memcmp(RegKeyName,"\\Device\\",8))//过滤虚拟串口
17 {
18 pListBox->AddString(SerialPortName); //SerialPortName就是串口名字
19 }
20 i++;
21 }
22 if(pListBox->GetCount()==0)
23 {
24 MessageBox("在HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm里找不到串口!!!");
25 }
26 RegCloseKey(hKey);
27 }
28 else
29 {
30 RegCloseKey(hKey);
31 MessageBox("您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项");
32 }
33 }
2.利用windows SDK 库函数读取硬件信息
代码如下:
利用windows SDK 库函数读取硬件信息
1 unsigned char CCommDlg::DeviceEnumCommPort(CListBox *pListBox)
2 {
3 HDEVINFO hDevInfo;
4 SP_DEVINFO_DATA DeviceInfoData;
5 DWORD DeviceIndex;
6 // Create a HDEVINFO with all present devices.
7 hDevInfo=SetupDiGetClassDevs((LPGUID) &GUID_DEVCLASS_PORTS,0,0,DIGCF_PRESENT);
8 /*
9 GUID_DEVCLASS_FDC软盘控制器
10 GUID_DEVCLASS_DISPLAY显示卡
11 GUID_DEVCLASS_CDROM光驱
12 GUID_DEVCLASS_KEYBOARD键盘
13 GUID_DEVCLASS_COMPUTER计算机
14 GUID_DEVCLASS_SYSTEM系统
15 GUID_DEVCLASS_DISKDRIVE磁盘驱动器
16 GUID_DEVCLASS_MEDIA声音、视频和游戏控制器
17 GUID_DEVCLASS_MODEMMODEM
18 GUID_DEVCLASS_MOUSE鼠标和其他指针设备
19 GUID_DEVCLASS_NET网络设备器
20 GUID_DEVCLASS_USB通用串行总线控制器
21 GUID_DEVCLASS_FLOPPYDISK软盘驱动器
22 GUID_DEVCLASS_UNKNOWN未知设备
23 GUID_DEVCLASS_SCSIADAPTERSCSI 和 RAID 控制器
24 GUID_DEVCLASS_HDCIDE ATA/ATAPI 控制器
25 GUID_DEVCLASS_PORTS端口(COM 和 LPT)
26 GUID_DEVCLASS_MONITOR监视器
27 */
28 if (hDevInfo == INVALID_HANDLE_VALUE)
29 {
30 DWORD ErrorCode = GetLastError();
31 // Insert error handling here.
32 return 1;
33 }
34 // Enumerate through all devices in Set.
35
36 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
37
38 for (DeviceIndex=0;SetupDiEnumDeviceInfo(hDevInfo,DeviceIndex,&DeviceInfoData);DeviceIndex++)
39 {
40 DWORD DataPropertyType;
41 LPTSTR buffer = NULL;
42 DWORD buffersize = 0;
43 //
44 // Call function with null to begin with,
45 // then use the returned buffer size
46 // to Alloc the buffer. Keep calling until
47 // success or an unknown failure.
48 //
49 while (!SetupDiGetDeviceRegistryProperty(
50 hDevInfo,
51 &DeviceInfoData,
52 SPDRP_FRIENDLYNAME,
53 &DataPropertyType,
54 (PBYTE)buffer,
55 buffersize,
56 &buffersize))
57 {
58 DWORD ErrorCode=GetLastError();
59 if (ErrorCode==
60 ERROR_INSUFFICIENT_BUFFER)
61 {
62 // Change the buffer size.
63 if (buffer) LocalFree(buffer);
64 buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
65 }
66 else
67 {
68 // Insert error handling here.
69 break;
70 }
71 }
72 CString FriendlyPortName=buffer; /*Extract Serial Port Name*/
73 int pos=FriendlyPortName.Find('(');
74 CString SerialPortName=FriendlyPortName.Right(FriendlyPortName.GetLength()-pos-1);
75 SerialPortName.TrimRight(')');
76 pListBox->AddString(SerialPortName);
77 if(buffer)
78 {
79 LocalFree(buffer);
80 }
81 }
82 if (GetLastError()!=NO_ERROR
83 &&GetLastError()!=ERROR_NO_MORE_ITEMS )
84 {
85 return 1;
86 }
87 // Cleanup
88 SetupDiDestroyDeviceInfoList(hDevInfo);
89 return 0;
90 }