#include #include #include #include bool searchPort() { QList availableports = QSerialPortInfo::availablePorts(); if(availableports.size() > 0) { qDebug("Available ports:"); uint _port = 0; foreach (const QSerialPortInfo &_portinfo, availableports) qDebug(" %u) [%s] %s", _port++, _portinfo.portName().toUtf8().constData(), _portinfo.description().toUtf8().constData()); } else { qDebug("No available ports found!"); } foreach (const QSerialPortInfo &_portinfo, availableports) { if(_portinfo.description().contains("CH340") || _portinfo.description().contains("CP210") || _portinfo.description().contains("USB2.0-Serial")) { qDebug(" ===\nCheking [%s] %s",_portinfo.portName().toUtf8().constData(), _portinfo.description().toUtf8().constData()); QSerialPort _tmpport(_portinfo); QByteArray _data; QObject::connect(&_tmpport,&QSerialPort::readyRead,[&_tmpport, &_data](){ _data.append(_tmpport.readAll()); }); if(_tmpport.open(QIODevice::ReadWrite)) { QTimer::singleShot(2000,[&_tmpport,&_data](){ _data.clear(); qDebug(" Device token request..."); _tmpport.write("T"); }); QEventLoop _el; QTimer::singleShot(2500,&_el,SLOT(quit())); _el.exec(); qDebug(" Received token: '%s'", _data.constData()); if(_data == "{6F9619FF-8B86-D011-B42D-00CF4FC964FF}") { _tmpport.close(); qDebug("Port found"); return true; } } } } qDebug("Port not found!"); return false; } int main(int argc, char *argv[]) { QCoreApplication app(argc,argv); searchPort(); return app.exec(); }