1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

Updated the serial plugin to poll every 2 secs for new Serial ports on MacOS and Linux, where we don't get system events whenever a Serial/USB adapter is plugged in. No change

on windows where the OS already told our plugin about such events. Polling thread is lightweight, no worries...



git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2312 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
edouard 2010-12-30 23:00:49 +00:00 committed by edouard
parent 3bad347386
commit a552addbf7
2 changed files with 78 additions and 0 deletions

View File

@ -35,15 +35,60 @@
#include <QDebug> #include <QDebug>
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() SerialConnection::SerialConnection()
: m_enumerateThread(this)
{ {
serialHandle = NULL; serialHandle = NULL;
#ifdef Q_OS_WIN
//I'm cheating a little bit here: //I'm cheating a little bit here:
//Knowing if the device enumeration really changed is a bit complicated //Knowing if the device enumeration really changed is a bit complicated
//so I just signal it whenever we have a device event... //so I just signal it whenever we have a device event...
QMainWindow *mw = Core::ICore::instance()->mainWindow(); QMainWindow *mw = Core::ICore::instance()->mainWindow();
QObject::connect(mw, SIGNAL(deviceChange()), QObject::connect(mw, SIGNAL(deviceChange()),
this, SLOT(onEnumerationChanged())); 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() SerialConnection::~SerialConnection()
@ -95,6 +140,7 @@ QIODevice *SerialConnection::openDevice(const QString &deviceName)
#else #else
serialHandle = new QextSerialPort(port.physName, set); serialHandle = new QextSerialPort(port.physName, set);
#endif #endif
m_deviceOpened = true;
return serialHandle; return serialHandle;
} }
} }
@ -109,6 +155,7 @@ void SerialConnection::closeDevice(const QString &deviceName)
serialHandle->close (); serialHandle->close ();
delete(serialHandle); delete(serialHandle);
serialHandle = NULL; serialHandle = NULL;
m_deviceOpened = false;
} }
} }

View File

@ -33,9 +33,34 @@
#include <qextserialenumerator.h> #include <qextserialenumerator.h>
#include "coreplugin/iconnection.h" #include "coreplugin/iconnection.h"
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <QThread>
class IConnection; class IConnection;
class QextSerialEnumerator; 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 * Define a connection via the IConnection interface
@ -56,12 +81,18 @@ public:
virtual QString connectionName(); virtual QString connectionName();
virtual QString shortName(); virtual QString shortName();
bool deviceOpened() {return m_deviceOpened;}
private: private:
QextSerialPort* serialHandle; QextSerialPort* serialHandle;
protected slots: protected slots:
void onEnumerationChanged(); void onEnumerationChanged();
protected:
SerialEnumerationThread m_enumerateThread;
bool m_deviceOpened;
}; };