Idle-Flush gefixt
This commit is contained in:
@@ -5,8 +5,12 @@
|
||||
SerialWorker::SerialWorker(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_port(new QSerialPort(this))
|
||||
, m_idleFlushTimer(new QTimer(this))
|
||||
, m_reconnectTimer(new QTimer(this))
|
||||
{
|
||||
m_idleFlushTimer->setSingleShot(true);
|
||||
connect(m_idleFlushTimer, &QTimer::timeout, this, &SerialWorker::flushScanTail);
|
||||
|
||||
m_reconnectTimer->setSingleShot(true);
|
||||
connect(m_reconnectTimer, &QTimer::timeout, this, &SerialWorker::tryReconnect);
|
||||
|
||||
@@ -53,6 +57,7 @@ void SerialWorker::openPort(const QString &portName, qint32 baudRate,
|
||||
scheduleReconnect();
|
||||
return;
|
||||
}
|
||||
m_idleFlushTimer->stop();
|
||||
m_buffer.clear();
|
||||
m_scanTail.clear();
|
||||
m_reconnectAttempt = 0;
|
||||
@@ -63,6 +68,7 @@ void SerialWorker::closePort()
|
||||
{
|
||||
m_userDisconnected = true;
|
||||
m_reconnectTimer->stop();
|
||||
m_idleFlushTimer->stop();
|
||||
if (m_port && m_port->isOpen()) {
|
||||
m_port->close();
|
||||
emit portClosed();
|
||||
@@ -149,6 +155,7 @@ void SerialWorker::tryReconnect()
|
||||
m_port->setFlowControl(m_flowControl);
|
||||
|
||||
if (m_port->open(QIODevice::ReadOnly)) {
|
||||
m_idleFlushTimer->stop();
|
||||
m_buffer.clear();
|
||||
m_scanTail.clear();
|
||||
m_reconnectAttempt = 0;
|
||||
@@ -202,9 +209,36 @@ void SerialWorker::processRawData(const QByteArray &data)
|
||||
if (sawClear)
|
||||
emit clearScreen();
|
||||
|
||||
// (Re)arm the idle-flush timer whenever bytes are still being withheld
|
||||
// in m_scanTail, so that a burst which ends right at a chunk boundary
|
||||
// doesn't leave its last line(s) stuck there forever. If nothing is
|
||||
// being withheld, make sure any pending flush is cancelled.
|
||||
if (!m_scanTail.isEmpty())
|
||||
m_idleFlushTimer->start(kIdleFlushMs);
|
||||
else
|
||||
m_idleFlushTimer->stop();
|
||||
|
||||
if (toProcess.isEmpty())
|
||||
return;
|
||||
|
||||
appendToLineBuffer(toProcess);
|
||||
}
|
||||
|
||||
void SerialWorker::flushScanTail()
|
||||
{
|
||||
// Nothing new arrived within kIdleFlushMs of the last chunk that left
|
||||
// data in m_scanTail: treat it as final and process it as-is, rather
|
||||
// than waiting indefinitely for more bytes that may never come.
|
||||
if (m_scanTail.isEmpty())
|
||||
return;
|
||||
|
||||
QByteArray toProcess = m_scanTail;
|
||||
m_scanTail.clear();
|
||||
appendToLineBuffer(toProcess);
|
||||
}
|
||||
|
||||
void SerialWorker::appendToLineBuffer(const QByteArray &toProcess)
|
||||
{
|
||||
// Strip remaining (non-clear) ANSI escape sequences for clean display.
|
||||
static const QRegularExpression ansiRe(
|
||||
"\x1b(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~])");
|
||||
|
||||
Reference in New Issue
Block a user