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 // if they need to be changed later, please
// destroy this and create a new one. // destroy this and create a new one.
port = new QSerialPort();
if (wfFormat == 1) { // Single waterfall packet if (wfFormat == 1) { // Single waterfall packet
combineWf = true; combineWf = true;
@ -55,6 +54,17 @@ commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QO
this->portName = portName; this->portName = portName;
this->PTTviaRTS = false; this->PTTviaRTS = false;
}
void commHandler::init()
{
if (port != Q_NULLPTR) {
delete port;
port = Q_NULLPTR;
isConnected = false;
}
port = new QSerialPort();
setupComm(); // basic parameters setupComm(); // basic parameters
openPort(); openPort();
// qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize(); // 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(); //qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
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)));
lastDataReceived = QTime::currentTime();
} }
commHandler::~commHandler() commHandler::~commHandler()
{ {
qInfo(logSerial()) << "Closing serial port: " << port->portName(); qInfo(logSerial()) << "Closing serial port: " << port->portName();
@ -89,18 +100,16 @@ void commHandler::receiveDataFromUserToRig(const QByteArray &data)
void commHandler::sendDataOut(const QByteArray &writeData) void commHandler::sendDataOut(const QByteArray &writeData)
{ {
mutex.lock();
// Recycle port to attempt reconnection. // Recycle port to attempt reconnection.
if (!this->isConnected || !port->isOpen() || lastDataReceived.msecsTo(QTime::currentTime()) > 2000) { if (!this->isConnected || !port->isOpen() || lastDataReceived.msecsTo(QTime::currentTime()) > 2000) {
qDebug(logSerial()) << "Serial port error? Attempting reconnect..."; qDebug(logSerial()) << "Serial port error? Attempting reconnect...";
closePort(); lastDataReceived = QTime::currentTime();
openPort(); QTimer::singleShot(500, this, SLOT(init()));
}
if (!this->isConnected) {
mutex.unlock();
return; return;
} }
mutex.lock();
qint64 bytesWritten; qint64 bytesWritten;
if(PTTviaRTS) if(PTTviaRTS)
@ -383,8 +392,7 @@ void commHandler::handleError(QSerialPort::SerialPortError err)
break; break;
default: default:
qDebug(logSerial()) << "Serial port" << port->portName() << "Error, attempting disconnect/reconnect"; qDebug(logSerial()) << "Serial port" << port->portName() << "Error, attempting disconnect/reconnect";
closePort(); QTimer::singleShot(500, this, SLOT(init()));
openPort();
break; break;
} }
} }

Wyświetl plik

@ -7,6 +7,7 @@
#include <QDataStream> #include <QDataStream>
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include <QTime> #include <QTime>
#include <QTimer>
// 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.
@ -27,6 +28,7 @@ public slots:
void setUseRTSforPTT(bool useRTS); void setUseRTSforPTT(bool useRTS);
void setRTS(bool rtsOn); void setRTS(bool rtsOn);
void handleError(QSerialPort::SerialPortError error); void handleError(QSerialPort::SerialPortError error);
void init();
private slots: private slots:
void receiveDataIn(); // from physical port void receiveDataIn(); // from physical port
@ -60,7 +62,7 @@ private:
unsigned char buffer[256]; unsigned char buffer[256];
QString portName; QString portName;
QSerialPort *port; QSerialPort *port=Q_NULLPTR;
qint32 baudrate; qint32 baudrate;
unsigned char stopbits; unsigned char stopbits;
bool rolledBack; bool rolledBack;