QT 自动获取可用串口

  1. 本来想直接用Settings来获取的,但是串口信息类似 "\Device\Serial0",死活获取不了,用了转义、反斜杠还是获取不到,所以就放弃了,网上好像也没有获取成功的。  
  2. 所以就用winapi来获取了,参考了网上源码  
  3.   
  4. cpp文件  
  5. /* 
  6.     初始化串口 
  7. */  
  8. void MainWindow::init_comm()  
  9. {  
  10.     QString path = "HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM";  
  11.     QSettings *settings = new QSettings(path, QSettings::NativeFormat);  
  12.     QStringList key = settings->allKeys();  
  13.     QStringList comlist ;  
  14.     QStringList Baudlist ;  
  15. //    QStringList Paritylist ;  
  16. //    QStringList DataBitslist;  
  17. //    QStringList StopBitslist;  
  18. //    QStringList ControlFlowlist;  
  19.   
  20.     comlist.clear();  
  21.   
  22.     /* 取串口名 */  
  23.     for (int i=0; i<key.size(); i++) {  
  24.         comlist << getcomm(i, "value");  
  25.     }  
  26.     ui->ComPortcomboBox->addItems(comlist);  
  27.   
  28.     ....  
  29. }  
  30.   
  31. /* 
  32.     读取键值 
  33. */  
  34. QString MainWindow::getcomm(int index, QString keyorvalue)  
  35. {  
  36.     QString commresult;  
  37.   
  38.     if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_READ, &hKey) != 0) {  
  39.             QString error="Cannot open regedit!";//无法打开注册表时返回error  
  40.             return error;  
  41.         }//if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_READ,&hKey)!=0)  
  42.   
  43.         QString keymessage;//键名  
  44.         QString message;  
  45.         QString valuemessage;//键值  
  46.   
  47.         indexnum = index;//要读取键值的索引号  
  48.         keysize = sizeof(keyname);  
  49.         valuesize = sizeof(keyvalue);  
  50.   
  51.         if (RegEnumValue(hKey, indexnum, keyname, &keysize, 0, &type, (BYTE*)keyvalue, &valuesize) == 0) { //列举键名和值  
  52.             for (int i=0; i<(int)keysize; i++) {  
  53.                 message = keyname[i];  
  54.                 keymessage.append(message);  
  55.             }  
  56.   
  57.             for (int j=0; j<(int)valuesize; j++) {  
  58.                  if (keyvalue[j] != 0x00) {  
  59.                      valuemessage.append(keyvalue[j]);  
  60.                  }  
  61.             }  
  62.   
  63.             if (keyorvalue == "key") {  
  64.                  commresult = keymessage;  
  65.             }  
  66.   
  67.             if (keyorvalue == "value") {  
  68.                  commresult=valuemessage;  
  69.             }  
  70.         } else {  
  71.              commresult = "nokey";  
  72.         }  
  73.   
  74.         RegCloseKey(hKey);//关闭注册表  
  75.   
  76.         return commresult;  
  77. }  


[cpp] view plain copy
 
  1. .h文件  
  2.   
  3. #include <QSettings>  
  4. #include <qt_windows.h>    
  5.     HKEY hKey;  
  6.     LPCWSTR subkey;  
  7.     wchar_t keyname[256]; //键名数组  
  8.     char keyvalue[256];  //键值数组  
  9.     DWORD keysize,type,valuesize;  
  10.     int indexnum;  

http://blog.csdn.net/emdfans/article/details/38588537

posted @ 2016-06-20 04:34  findumars  Views(6896)  Comments(0Edit  收藏  举报