Add timeout if no data received on serial port for 2s

merge-requests/9/merge
Phil Taylor 2022-05-14 11:24:09 +01:00
rodzic f003a8a1b8
commit 6078a31a3c
2 zmienionych plików z 6 dodań i 1 usunięć

Wyświetl plik

@ -30,6 +30,7 @@ commHandler::commHandler(QObject* parent) : QObject(parent)
connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn())); connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn()));
connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
lastDataReceived = QTime::currentTime();
} }
commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QObject* parent) : QObject(parent) commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QObject* parent) : QObject(parent)
@ -90,7 +91,8 @@ void commHandler::sendDataOut(const QByteArray &writeData)
{ {
mutex.lock(); mutex.lock();
// Recycle port to attempt reconnection. // Recycle port to attempt reconnection.
if (!this->isConnected || !port->isOpen()) { if (!this->isConnected || !port->isOpen() || lastDataReceived.msecsTo(QTime::currentTime()) > 2000) {
qDebug(logSerial()) << "Serial port error? Attempting reconnect...";
closePort(); closePort();
openPort(); openPort();
} }
@ -164,6 +166,7 @@ void commHandler::receiveDataIn()
// because we know what constitutes a valid "frame" of data. // because we know what constitutes a valid "frame" of data.
// new code: // new code:
lastDataReceived = QTime::currentTime();
port->startTransaction(); port->startTransaction();
inPortData = port->readAll(); inPortData = port->readAll();

Wyświetl plik

@ -6,6 +6,7 @@
#include <QMutex> #include <QMutex>
#include <QDataStream> #include <QDataStream>
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include <QTime>
// This class abstracts the comm port in a useful way and connects to // This class abstracts the comm port in a useful way and connects to
// the command creator and command parser. // the command creator and command parser.
@ -83,6 +84,7 @@ private:
quint8 spectrumInformation; quint8 spectrumInformation;
quint8 spectrumOutOfRange; quint8 spectrumOutOfRange;
quint8 lastSpectrum = 0; quint8 lastSpectrum = 0;
QTime lastDataReceived;
}; };
#endif // COMMHANDLER_H #endif // COMMHANDLER_H