|
qCNC 0.82-alpha
|
00001 00002 00003 00004 #include "qextserialenumerator.h" 00005 #include <QDebug> 00006 #include <QMetaType> 00007 #include <QStringList> 00008 #include <QDir> 00009 00010 QextSerialEnumerator::QextSerialEnumerator( ) 00011 { 00012 if( !QMetaType::isRegistered( QMetaType::type("QextPortInfo") ) ) 00013 qRegisterMetaType<QextPortInfo>("QextPortInfo"); 00014 } 00015 00016 QextSerialEnumerator::~QextSerialEnumerator( ) 00017 { 00018 } 00019 00020 QList<QextPortInfo> QextSerialEnumerator::getPorts() 00021 { 00022 QList<QextPortInfo> infoList; 00023 #ifdef Q_OS_LINUX 00024 QStringList portNamePrefixes, portNameList; 00025 portNamePrefixes << "ttyS*"; // list normal serial ports first 00026 00027 QDir dir("/dev"); 00028 portNameList = dir.entryList(portNamePrefixes, (QDir::System | QDir::Files), QDir::Name); 00029 00030 // remove the values which are not serial ports for e.g. /dev/ttysa 00031 for (int i = 0; i < portNameList.size(); i++) { 00032 bool ok; 00033 QString current = portNameList.at(i); 00034 // remove the ttyS part, and check, if the other part is a number 00035 current.remove(0,4).toInt(&ok, 10); 00036 if (!ok) { 00037 portNameList.removeAt(i); 00038 i--; 00039 } 00040 } 00041 00042 // get the non standard serial ports names 00043 // (USB-serial, bluetooth-serial, 18F PICs, and so on) 00044 // if you know an other name prefix for serial ports please let us know 00045 portNamePrefixes.clear(); 00046 portNamePrefixes << "ttyACM*" << "ttyUSB*" << "rfcomm*"; 00047 portNameList.append(dir.entryList(portNamePrefixes, (QDir::System | QDir::Files), QDir::Name)); 00048 00049 foreach (QString str , portNameList) { 00050 QextPortInfo inf; 00051 inf.physName = "/dev/"+str; 00052 inf.portName = str; 00053 00054 if (str.contains("ttyS")) { 00055 inf.friendName = "Serial port "+str.remove(0, 4); 00056 } 00057 else if (str.contains("ttyUSB")) { 00058 inf.friendName = "USB-serial adapter "+str.remove(0, 6); 00059 } 00060 else if (str.contains("rfcomm")) { 00061 inf.friendName = "Bluetooth-serial adapter "+str.remove(0, 6); 00062 } 00063 inf.enumName = "/dev"; // is there a more helpful name for this? 00064 infoList.append(inf); 00065 } 00066 #else 00067 qCritical("Enumeration for POSIX systems (except Linux) is not implemented yet."); 00068 #endif 00069 return infoList; 00070 } 00071 00072 void QextSerialEnumerator::setUpNotifications( ) 00073 { 00074 qCritical("Notifications for *Nix/FreeBSD are not implemented yet"); 00075 }