Commit beba8fd1 authored by Lorenz Meier's avatar Lorenz Meier

Comms: Re-establish the link reliably if just cycling through bootloader

parent 5bb736b6
...@@ -46,7 +46,7 @@ Q_DECLARE_METATYPE(mavlink_message_t) ...@@ -46,7 +46,7 @@ Q_DECLARE_METATYPE(mavlink_message_t)
MAVLinkProtocol::MAVLinkProtocol() : MAVLinkProtocol::MAVLinkProtocol() :
heartbeatTimer(new QTimer(this)), heartbeatTimer(new QTimer(this)),
heartbeatRate(MAVLINK_HEARTBEAT_DEFAULT_RATE), heartbeatRate(MAVLINK_HEARTBEAT_DEFAULT_RATE),
m_heartbeatsEnabled(false), m_heartbeatsEnabled(true),
m_multiplexingEnabled(false), m_multiplexingEnabled(false),
m_authEnabled(false), m_authEnabled(false),
m_loggingEnabled(false), m_loggingEnabled(false),
......
...@@ -194,43 +194,54 @@ void SerialLink::run() ...@@ -194,43 +194,54 @@ void SerialLink::run()
err = m_port->errorString(); err = m_port->errorString();
} }
emit communicationError(getName(),"Error connecting: " + err); emit communicationError(getName(),"Error connecting: " + err);
// disconnect(); // This tidies up and sends the necessary signals
return; return;
} }
qDebug() << "connected";
// Qt way to make clear what a while(1) loop does
qint64 msecs = QDateTime::currentMSecsSinceEpoch(); qint64 msecs = QDateTime::currentMSecsSinceEpoch();
qint64 initialmsecs = QDateTime::currentMSecsSinceEpoch(); qint64 initialmsecs = QDateTime::currentMSecsSinceEpoch();
quint64 bytes = 0; quint64 bytes = 0;
// bool triedreset = false;
// bool triedDTR = false;
qint64 timeout = 5000; qint64 timeout = 5000;
int linkErrorCount = 0; int linkErrorCount = 0;
// Qt way to make clear what a while(1) loop does
forever { forever {
{ {
QMutexLocker locker(&this->m_stoppMutex); QMutexLocker locker(&this->m_stoppMutex);
if(m_stopp) { if (m_stopp) {
m_stopp = false; m_stopp = false;
break; // exit the thread break; // exit the thread
} }
if (m_reqReset) {
m_reqReset = false;
emit communicationUpdate(getName(),"Reset requested via DTR signal");
m_port->setDataTerminalReady(true);
msleep(250);
m_port->setDataTerminalReady(false);
}
} }
// If there are too many errors on this link, disconnect. // If there are too many errors on this link, disconnect.
if (isConnected() && (linkErrorCount > 1000)) { if (isConnected() && (linkErrorCount > 100)) {
qDebug() << "linkErrorCount too high: disconnecting!"; qDebug() << "linkErrorCount too high: re-connecting!";
linkErrorCount = 0; linkErrorCount = 0;
emit communicationUpdate(getName(), tr("Disconnecting on too many link errors")); emit communicationUpdate(getName(), tr("Reconnecting on too many link errors"));
disconnect();
if (m_port) {
m_port->close();
delete m_port;
m_port = NULL;
emit disconnected();
emit connected(false);
}
QGC::SLEEP::msleep(500);
unsigned tries = 0;
const unsigned tries_max = 15;
while (!hardwareConnect(type) && tries < tries_max) {
tries++;
QGC::SLEEP::msleep(500);
}
// Give up
if (tries == tries_max) {
break;
}
} }
// Write all our buffered data out the serial port. // Write all our buffered data out the serial port.
...@@ -301,31 +312,13 @@ void SerialLink::run() ...@@ -301,31 +312,13 @@ void SerialLink::run()
//TODO ^^ //TODO ^^
timeout = 30000; timeout = 30000;
} }
// if (!triedDTR && triedreset) {
// triedDTR = true;
// emit communicationUpdate(getName(),"No data to receive on COM port. Attempting to reset via DTR signal");
// qDebug() << "No data!!! Attempting reset via DTR.";
// m_port->setDataTerminalReady(true);
// msleep(250);
// m_port->setDataTerminalReady(false);
// }
// else if (!triedreset) {
// qDebug() << "No data!!! Attempting reset via reboot command.";
// emit communicationUpdate(getName(),"No data to receive on COM port. Assuming possible terminal mode, attempting to reset via \"reboot\" command");
// m_port->write("reboot\r\n",8);
// triedreset = true;
// }
// else {
// emit communicationUpdate(getName(),"No data to receive on COM port....");
// qDebug() << "No data!!!";
// }
} }
} }
//MG::SLEEP::msleep(SerialLink::poll_interval); //MG::SLEEP::msleep(SerialLink::poll_interval);
QGC::SLEEP::msleep(2); QGC::SLEEP::msleep(2);
} // end of forever } // end of forever
if (m_port) { // [TODO][BB] Not sure we need to close the port here if (m_port) {
qDebug() << "Closing Port #"<< __LINE__ << m_port->portName(); qDebug() << "Closing Port #"<< __LINE__ << m_port->portName();
m_port->close(); m_port->close();
delete m_port; delete m_port;
...@@ -345,7 +338,6 @@ void SerialLink::writeBytes(const char* data, qint64 size) ...@@ -345,7 +338,6 @@ void SerialLink::writeBytes(const char* data, qint64 size)
m_transmitBuffer.append(byteArray); m_transmitBuffer.append(byteArray);
m_writeMutex.unlock(); m_writeMutex.unlock();
} else { } else {
disconnect();
// Error occured // Error occured
emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName())); emit communicationError(getName(), tr("Could not send data - link %1 is disconnected!").arg(getName()));
} }
...@@ -399,10 +391,6 @@ qint64 SerialLink::bytesAvailable() ...@@ -399,10 +391,6 @@ qint64 SerialLink::bytesAvailable()
**/ **/
bool SerialLink::disconnect() bool SerialLink::disconnect()
{ {
qDebug() << "disconnect";
if (m_port)
qDebug() << m_port->portName();
if (isRunning()) if (isRunning())
{ {
{ {
...@@ -411,8 +399,6 @@ bool SerialLink::disconnect() ...@@ -411,8 +399,6 @@ bool SerialLink::disconnect()
} }
wait(); // This will terminate the thread and close the serial port wait(); // This will terminate the thread and close the serial port
emit disconnected(); // [TODO] There are signals from QSerialPort we should use
emit connected(false);
return true; return true;
} }
...@@ -429,6 +415,7 @@ bool SerialLink::disconnect() ...@@ -429,6 +415,7 @@ bool SerialLink::disconnect()
**/ **/
bool SerialLink::connect() bool SerialLink::connect()
{ {
qDebug() << "CONNECT CALLED";
if (isRunning()) if (isRunning())
disconnect(); disconnect();
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment