From b049704dd2cdcbaf78ec3ed897fd8635aee804a5 Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Sat, 14 May 2022 11:35:41 +0100 Subject: [PATCH] Use qtimer to signal a reconnect --- commhandler.cpp | 30 +++++++++++++++++++----------- commhandler.h | 4 +++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/commhandler.cpp b/commhandler.cpp index 800fe4e..5e592f0 100644 --- a/commhandler.cpp +++ b/commhandler.cpp @@ -40,7 +40,6 @@ commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QO // if they need to be changed later, please // destroy this and create a new one. - port = new QSerialPort(); if (wfFormat == 1) { // Single waterfall packet combineWf = true; @@ -55,6 +54,17 @@ commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QO this->portName = portName; this->PTTviaRTS = false; +} + +void commHandler::init() +{ + if (port != Q_NULLPTR) { + delete port; + port = Q_NULLPTR; + isConnected = false; + } + + port = new QSerialPort(); setupComm(); // basic parameters openPort(); // qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize(); @@ -62,9 +72,10 @@ commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QO //qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize(); connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn())); + connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); + lastDataReceived = QTime::currentTime(); } - commHandler::~commHandler() { qInfo(logSerial()) << "Closing serial port: " << port->portName(); @@ -89,18 +100,16 @@ void commHandler::receiveDataFromUserToRig(const QByteArray &data) void commHandler::sendDataOut(const QByteArray &writeData) { - mutex.lock(); // Recycle port to attempt reconnection. if (!this->isConnected || !port->isOpen() || lastDataReceived.msecsTo(QTime::currentTime()) > 2000) { qDebug(logSerial()) << "Serial port error? Attempting reconnect..."; - closePort(); - openPort(); - } - - if (!this->isConnected) { - mutex.unlock(); + lastDataReceived = QTime::currentTime(); + QTimer::singleShot(500, this, SLOT(init())); return; } + + mutex.lock(); + qint64 bytesWritten; if(PTTviaRTS) @@ -383,8 +392,7 @@ void commHandler::handleError(QSerialPort::SerialPortError err) break; default: qDebug(logSerial()) << "Serial port" << port->portName() << "Error, attempting disconnect/reconnect"; - closePort(); - openPort(); + QTimer::singleShot(500, this, SLOT(init())); break; } } diff --git a/commhandler.h b/commhandler.h index 38c2d5c..acfc8be 100644 --- a/commhandler.h +++ b/commhandler.h @@ -7,6 +7,7 @@ #include #include #include +#include // This class abstracts the comm port in a useful way and connects to // the command creator and command parser. @@ -27,6 +28,7 @@ public slots: void setUseRTSforPTT(bool useRTS); void setRTS(bool rtsOn); void handleError(QSerialPort::SerialPortError error); + void init(); private slots: void receiveDataIn(); // from physical port @@ -60,7 +62,7 @@ private: unsigned char buffer[256]; QString portName; - QSerialPort *port; + QSerialPort *port=Q_NULLPTR; qint32 baudrate; unsigned char stopbits; bool rolledBack;