|
cliffordl 发表于 2013-11-6 16:29
小弟问的正是Windows下的。 亚飞老师串口例子我没有搜到,求连接,求指教。 ...
注册表啊...这个Windows版本不一样,我估计可能位置不大一样啊。。
我个你搜了一个你瞅瞅!- #define MAXCOMNO 256 //最大COM号256
- typedef struct{
- char name[8];
- short no;
- }SCOM;
- /////用读注册表的方式查看系统的串口设备
- void GetAvailableCom(SCOM *port,int &cnt)
- {
- HKEY hKey;
- LONG ret;
- OSVERSIONINFO osvi;
- BOOL bOsVersionInfoEx;
- CString keyinfo;
- unsigned char comm_name[40];
- char ValueName[40];
- char szNO[3];
- int i=0,j=0;
- cnt=0;
- DWORD sType,Reserved,cbData,cbValueName;
-
- ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-
- keyinfo.Format(_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"));
- //CString keyinfo=_T("HARDWARE\\DEVICEMAP\\SERIALCOMM");
- i=0; sType=REG_SZ;Reserved=0;
- bOsVersionInfoEx = GetVersionEx(&osvi);
- ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyinfo,0,KEY_ALL_ACCESS,&hKey);
- if (ret==ERROR_SUCCESS)
- {
- if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) //WIN32,not used
- {
- for(i=1;i<=MAXCOMNO;i++)
- {
- memset(comm_name, 0, 40);
- sprintf((char*)comm_name,"COM%d",i);
- ret=RegEnumValue(hKey, i, ValueName, &cbValueName, NULL, &sType, comm_name, &cbData);
- if(ret==ERROR_SUCCESS)
- {
- i++;
- }
- }
- }
- else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) //WINNT XP 2000
- {
- do
- {
- cbData=40;cbValueName=40;
- memset(comm_name,0,40);
- memset(ValueName,0,40);
- memset(szNO, 0, 3);
- ret=RegEnumValue(hKey, i, ValueName, &cbValueName, NULL, &sType, comm_name, &cbData);
- if (ret==ERROR_SUCCESS)
- {
- for(j=0;j<cbData;j++)
- {
- port->name[j]=comm_name[j];
- if(j>2 && j<6)
- {
- szNO[j-3] = comm_name[j];
- }
- }
- port->no = atoi(szNO);
- port++;
- cnt++;
- i++;
- }
- }while(ret==ERROR_SUCCESS);
- }
- }
- RegCloseKey(hKey);
- }
复制代码 |
评分
-
查看全部评分总评分 : 人气 +1
|