Use qtimer to signal a reconnect

merge-requests/9/merge
Phil Taylor 2022-05-14 11:35:41 +01:00
rodzic 6078a31a3c
commit b049704dd2
2 zmienionych plików z 22 dodań i 12 usunięć

Wyświetl plik

@ -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;
}
}

Wyświetl plik

@ -7,6 +7,7 @@
#include <QDataStream>
#include <QtSerialPort/QSerialPort>
#include <QTime>
#include <QTimer>
// 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;