diff --git a/ground/src/plugins/serialconnection/serialplugin.cpp b/ground/src/plugins/serialconnection/serialplugin.cpp index 016598d15..bd3bf1243 100644 --- a/ground/src/plugins/serialconnection/serialplugin.cpp +++ b/ground/src/plugins/serialconnection/serialplugin.cpp @@ -35,15 +35,60 @@ #include + + +SerialEnumerationThread::SerialEnumerationThread(SerialConnection *serial) + : m_serial(serial), + m_running(true) +{ +} + +SerialEnumerationThread::~SerialEnumerationThread() +{ + m_running = false; + //wait for the thread to terminate + if(wait(1000) == false) + qDebug() << "Cannot terminate SerialEnumerationThread"; +} + +void SerialEnumerationThread::run() +{ + QStringList devices = m_serial->availableDevices(); + + while(m_running) + { + if(!m_serial->deviceOpened()) + { + QStringList newDev = m_serial->availableDevices(); + if(devices != newDev) + { + devices = newDev; + emit enumerationChanged(); + } + } + + msleep(2000); //update available devices every two seconds (doesn't need more) + } +} + + SerialConnection::SerialConnection() + : m_enumerateThread(this) { serialHandle = NULL; +#ifdef Q_OS_WIN //I'm cheating a little bit here: //Knowing if the device enumeration really changed is a bit complicated //so I just signal it whenever we have a device event... QMainWindow *mw = Core::ICore::instance()->mainWindow(); QObject::connect(mw, SIGNAL(deviceChange()), this, SLOT(onEnumerationChanged())); +#else + // Other OSes do not send such signals: + QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()), + this, SLOT(onEnumerationChanged())); + m_enumerateThread.start(); +#endif } SerialConnection::~SerialConnection() @@ -95,6 +140,7 @@ QIODevice *SerialConnection::openDevice(const QString &deviceName) #else serialHandle = new QextSerialPort(port.physName, set); #endif + m_deviceOpened = true; return serialHandle; } } @@ -109,6 +155,7 @@ void SerialConnection::closeDevice(const QString &deviceName) serialHandle->close (); delete(serialHandle); serialHandle = NULL; + m_deviceOpened = false; } } diff --git a/ground/src/plugins/serialconnection/serialplugin.h b/ground/src/plugins/serialconnection/serialplugin.h index d92366633..812f6b8e2 100644 --- a/ground/src/plugins/serialconnection/serialplugin.h +++ b/ground/src/plugins/serialconnection/serialplugin.h @@ -33,9 +33,34 @@ #include #include "coreplugin/iconnection.h" #include +#include class IConnection; class QextSerialEnumerator; +class SerialConnection; + +/** +* Helper thread to check on new serial port connection/disconnection +* Some operating systems do not send device insertion events so +* for those we have to poll +*/ +class SERIAL_EXPORT SerialEnumerationThread : public QThread +{ + Q_OBJECT +public: + SerialEnumerationThread(SerialConnection *serial); + virtual ~SerialEnumerationThread(); + + virtual void run(); + +signals: + void enumerationChanged(); + +protected: + SerialConnection *m_serial; + bool m_running; +}; + /** * Define a connection via the IConnection interface @@ -56,12 +81,18 @@ public: virtual QString connectionName(); virtual QString shortName(); + bool deviceOpened() {return m_deviceOpened;} + private: QextSerialPort* serialHandle; protected slots: void onEnumerationChanged(); + +protected: + SerialEnumerationThread m_enumerateThread; + bool m_deviceOpened; };