Make error signal depending on the Qt version

This commit is contained in:
Simon Stürz 2021-11-15 13:55:26 +01:00 committed by Michael Zanetti
parent 3c1fd0d7ca
commit 9878a0c18c
2 changed files with 21 additions and 12 deletions

View File

@ -20,18 +20,13 @@ OwletSerialTransport::OwletSerialTransport(const QString &serialPortName, uint b
m_serialPort->setFlowControl(QSerialPort::FlowControl::NoFlowControl);
connect(m_serialPort, &QSerialPort::readyRead, this, &OwletSerialTransport::onReadyRead);
typedef void (QSerialPort:: *errorSignal)(QSerialPort::SerialPortError);
connect(m_serialPort, static_cast<errorSignal>(&QSerialPort::error), this, [=](){
if (m_serialPort->error() != QSerialPort::NoError) {
qCWarning(dcOwlet()) << "Serial port error occured" << m_serialPort->error() << m_serialPort->errorString();
emit error();
m_reconnectTimer->start();
if (m_serialPort->isOpen()) {
m_serialPort->close();
}
emit connectedChanged(false);
}
});
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
// NOTE: Using QueuedConnection because in older Qt versions error is emitted before the port says its closed which might end up in a loop...
connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onError(QSerialPort::SerialPortError)), Qt::QueuedConnection);
#else
connect(m_serialPort, &QSerialPort::errorOccurred, this, &OwletSerialTransport::onError);
#endif
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setInterval(5000);
@ -167,3 +162,16 @@ void OwletSerialTransport::onReadyRead()
}
}
}
void OwletSerialTransport::onError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError != QSerialPort::NoError && serialPortError != QSerialPort::OpenError && m_serialPort->isOpen()) {
qCWarning(dcOwlet()) << "Serial port error occured" << serialPortError << m_serialPort->errorString();
emit error();
m_reconnectTimer->start();
if (m_serialPort->isOpen()) {
m_serialPort->close();
}
emit connectedChanged(false);
}
}

View File

@ -23,6 +23,7 @@ public slots:
private slots:
void onReadyRead();
void onError(QSerialPort::SerialPortError serialPortError);
private: