示例#1
0
void RefreshComPortList() {
    COMMPort.clear();
#ifdef WIN32    
    TCHAR szPort[10];
    for (unsigned i = 1; i < 10; ++i) {
        _stprintf(szPort, _T("COM%u"), i);
        COMMPort.push_back(szPort);
    }

#ifndef UNDER_CE
    for (unsigned i = 10; i < 41; ++i) {
        _stprintf(szPort, _T("COM%u"), i);
        COMMPort.push_back(szPort);
    }
#endif

    COMMPort.push_back(_T("COM0"));

#if defined(PNA) && defined(UNDER_CE)
    COMMPort.push_back(_T("VSP0"));
    COMMPort.push_back(_T("VSP1"));
#endif
#endif
    
#ifdef __linux__
  
  struct dirent **namelist;
  int n;
  if (IsKobo()) {
    n = scandir("/dev", &namelist, 0, alphasort);//need test
  } else {  
    n = scandir("/sys/class/tty", &namelist, 0, alphasort); //which is faster than /dev/
  }
  if (n != -1){
    for (int i = 0; i < n; ++i) {
      bool portok = true;
      if (memcmp(namelist[i]->d_name, "tty", 3) == 0) {
        // filter out "/dev/tty0", ... (valid integer after "tty") 
        char *endptr;
        strtoul(namelist[i]->d_name + 3, &endptr, 10);
        if (*endptr == 0) {
          portok = false;
        }
      } else if ( (memcmp(namelist[i]->d_name, "rfcomm", 6) != 0) 
                  && (memcmp(namelist[i]->d_name, "tnt", 3) != 0) ){
        // '/dev/tntX' are tty0tty virtual port  
        portok = false;
      }
      if(portok) {
        char path[64];
        snprintf(path, sizeof(path), "/dev/%s", namelist[i]->d_name);
        if (access(path, R_OK|W_OK) == 0 && access(path, X_OK) < 0) {
          COMMPort.push_back(path);
        }
      }
      free(namelist[i]);
    } 
  }    
  free(namelist);
  #if TESTBENCH
  COMMPort.push_back(_T("/lk/ptycom1"));
  COMMPort.push_back(_T("/lk/ptycom2"));
  COMMPort.push_back(_T("/lk/ptycom3"));
  COMMPort.push_back(_T("/lk/ptycom4"));
  #endif
  
#endif
    
#ifndef NO_BLUETOOTH
    CBtHandler* pBtHandler = CBtHandler::Get();
    if (pBtHandler) {
        std::copy(
        	pBtHandler->m_devices.begin(),
        	pBtHandler->m_devices.end(),
        	std::back_insert_iterator<COMMPort_t>(COMMPort)
        );
    }
#endif
    
    if(COMMPort.empty()) {
        // avoid segfault on device config  dialog if no comport detected.
        COMMPort.push_back(_T("Null"));
    }
}