diff --git a/src/comm/MAVLinkProtocol.cc b/src/comm/MAVLinkProtocol.cc index 8ecb5194c20f55cb6029f4f51e8b8bc65f045d4a..533bdb382ed83eabb358aad2d73a7fcd5795f1b8 100644 --- a/src/comm/MAVLinkProtocol.cc +++ b/src/comm/MAVLinkProtocol.cc @@ -64,7 +64,7 @@ MAVLinkProtocol::MAVLinkProtocol() : m_authKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; loadSettings(); - //start(QThread::LowPriority); + moveToThread(this); // Start heartbeat timer, emitting a heartbeat at the configured rate connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(sendHeartbeat())); heartbeatTimer->start(1000/heartbeatRate); @@ -81,6 +81,8 @@ MAVLinkProtocol::MAVLinkProtocol() : } } + start(QThread::HighPriority); + emit versionCheckChanged(m_enable_version_check); } @@ -166,6 +168,20 @@ MAVLinkProtocol::~MAVLinkProtocol() delete m_logfile; m_logfile = NULL; } + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); +} + +/** + * @brief Runs the thread + * + **/ +void MAVLinkProtocol::run() +{ + exec(); } QString MAVLinkProtocol::getLogfileName() diff --git a/src/comm/MAVLinkProtocol.h b/src/comm/MAVLinkProtocol.h index 3dc72563f50ebd4c8e34594d4c34b45d3aa7cae4..539b518e1ef7e94729dd8d99cfcfba3e8366b5c5 100644 --- a/src/comm/MAVLinkProtocol.h +++ b/src/comm/MAVLinkProtocol.h @@ -150,6 +150,8 @@ public: */ virtual void resetMetadataForLink(const LinkInterface *link); + void run(); + public slots: /** @brief Receive bytes from a communication interface */ void receiveBytes(LinkInterface* link, QByteArray b); diff --git a/src/comm/ProtocolInterface.h b/src/comm/ProtocolInterface.h index 150d5eb8f751385fd21e7541161a85846b2ec90a..a3a5fef128f7cf05c343fdee6f1352ae0d0752dc 100644 --- a/src/comm/ProtocolInterface.h +++ b/src/comm/ProtocolInterface.h @@ -46,7 +46,7 @@ This file is part of the PIXHAWK project * @see LinkManager. * **/ -class ProtocolInterface : public QObject +class ProtocolInterface : public QThread { Q_OBJECT public: diff --git a/src/comm/QGCXPlaneLink.cc b/src/comm/QGCXPlaneLink.cc index 66c6e5b7123c537cef9209072d405f45ac9088b4..1d974b43c7982ad7b239c726280b6e14fc85f5dc 100644 --- a/src/comm/QGCXPlaneLink.cc +++ b/src/comm/QGCXPlaneLink.cc @@ -74,6 +74,11 @@ QGCXPlaneLink::QGCXPlaneLink(UASInterface* mav, QString remoteHost, QHostAddress QGCXPlaneLink::~QGCXPlaneLink() { storeSettings(); + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + // if(connectState) { // disconnectSimulation(); // } diff --git a/src/comm/SerialLink.cc b/src/comm/SerialLink.cc index 1dbce7d655c075f2800ed9db848d3acf6412d5c9..ff555cc8bea71a7450a153bd54ce03048d32a0c8 100644 --- a/src/comm/SerialLink.cc +++ b/src/comm/SerialLink.cc @@ -87,6 +87,11 @@ SerialLink::~SerialLink() disconnect(); if(m_port) delete m_port; m_port = NULL; + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); } QList SerialLink::getCurrentPorts() @@ -455,6 +460,7 @@ bool SerialLink::hardwareConnect(QString &type) qDebug() << "SerialLink: hardwareConnect to " << m_portName; m_port = new QSerialPort(m_portName); + m_port->moveToThread(this); if (!m_port) { emit communicationUpdate(getName(),"Error opening port: " + m_portName); diff --git a/src/comm/TCPLink.cc b/src/comm/TCPLink.cc index 392edbfcbb8e7be4961e16ab9ef4c6696ebc6015..146e7a39306b03d4a67b4b370894f6f563a19f19 100644 --- a/src/comm/TCPLink.cc +++ b/src/comm/TCPLink.cc @@ -56,6 +56,12 @@ TCPLink::TCPLink(QHostAddress hostAddress, quint16 socketPort) : TCPLink::~TCPLink() { disconnect(); + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + deleteLater(); } diff --git a/src/comm/UDPLink.cc b/src/comm/UDPLink.cc index 7c401c73402bffc4dc4d6e6dfa6c0e6c4bff5104..d02e9290a3abcc13cf5cb128511d0b89ccf1f66c 100644 --- a/src/comm/UDPLink.cc +++ b/src/comm/UDPLink.cc @@ -60,6 +60,12 @@ UDPLink::UDPLink(QHostAddress host, quint16 port) : UDPLink::~UDPLink() { disconnect(); + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + this->deleteLater(); } diff --git a/src/uas/QGCMAVLinkUASFactory.cc b/src/uas/QGCMAVLinkUASFactory.cc index b6fac140852b3d5379aa8160b868a3ab989f36af..a5b18330648e047403148b36da731a7116697e0a 100644 --- a/src/uas/QGCMAVLinkUASFactory.cc +++ b/src/uas/QGCMAVLinkUASFactory.cc @@ -132,6 +132,7 @@ UASInterface* QGCMAVLinkUASFactory::createUAS(MAVLinkProtocol* mavlink, LinkInte UASManager::instance()->addUAS(uas); worker->start(QThread::HighPriority); + connect(uas, SIGNAL(destroyed()), worker, SLOT(quit())); return uas; } diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc index 857e41dae07e70f257de7ec913ef9661eaa4fca8..20e29ff3b24962548190007efcc88a26e2414eaa 100644 --- a/src/uas/UAS.cc +++ b/src/uas/UAS.cc @@ -172,7 +172,6 @@ UAS::UAS(MAVLinkProtocol* protocol, int id) : UASInterface(), componentMulti[i] = false; } - // Store a list of available actions for this UAS. // Basically everything exposted as a SLOT with no return value or arguments. diff --git a/src/ui/MAVLinkDecoder.cc b/src/ui/MAVLinkDecoder.cc index 7f5ab0679e712cab30381dcb579a7c26a017ec09..e957d8e5538cd6f863839bb14dafcecff53d96f3 100644 --- a/src/ui/MAVLinkDecoder.cc +++ b/src/ui/MAVLinkDecoder.cc @@ -2,7 +2,7 @@ #include "UASManager.h" MAVLinkDecoder::MAVLinkDecoder(MAVLinkProtocol* protocol, QObject *parent) : - QThread(parent) + QThread() { // We're doing it wrong - because the Qt folks got the API wrong: // http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/