From fe728b15434d9f960eca899efb1dfebd7b9c319d Mon Sep 17 00:00:00 2001 From: Portisch Date: Sun, 15 Dec 2019 13:43:50 +0100 Subject: [PATCH] Add RS232 timeout for asynchronous connection (#650) --- libsrc/leddevice/dev_serial/ProviderRs232.cpp | 25 +++++++++++++++---- libsrc/leddevice/dev_serial/ProviderRs232.h | 5 +++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/libsrc/leddevice/dev_serial/ProviderRs232.cpp b/libsrc/leddevice/dev_serial/ProviderRs232.cpp index f9ee4678..3e20fbc1 100644 --- a/libsrc/leddevice/dev_serial/ProviderRs232.cpp +++ b/libsrc/leddevice/dev_serial/ProviderRs232.cpp @@ -14,10 +14,10 @@ ProviderRs232::ProviderRs232() : _rs232Port(this) + , _writeTimeout(this) , _blockedForDelay(false) , _stateChanged(true) , _bytesToWrite(0) - , _bytesWritten(0) , _frameDropCounter(0) , _lastError(QSerialPort::NoError) , _preOpenDelayTimeOut(0) @@ -27,6 +27,9 @@ ProviderRs232::ProviderRs232() connect(&_rs232Port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(error(QSerialPort::SerialPortError))); connect(&_rs232Port, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64))); connect(&_rs232Port, SIGNAL(readyRead()), this, SLOT(readyRead())); + _writeTimeout.setInterval(5000); + _writeTimeout.setSingleShot(true); + connect(&_writeTimeout, SIGNAL(timeout()), this, SLOT(writeTimeout())); } bool ProviderRs232::init(const QJsonObject &deviceConfig) @@ -61,11 +64,11 @@ QString ProviderRs232::findSerialDevice() void ProviderRs232::bytesWritten(qint64 bytes) { - _bytesWritten += bytes; - if (_bytesWritten >= _bytesToWrite) + _bytesToWrite -= bytes; + if (_bytesToWrite <= 0) { - _bytesToWrite = 0; _blockedForDelay = false; + _writeTimeout.stop(); } } @@ -132,11 +135,18 @@ ProviderRs232::~ProviderRs232() void ProviderRs232::closeDevice() { + _writeTimeout.stop(); + if (_rs232Port.isOpen()) { _rs232Port.close(); Debug(_log,"Close UART: %s", _deviceName.toLocal8Bit().constData()); } + + _stateChanged = true; + _bytesToWrite = 0; + _blockedForDelay = false; + _deviceReady = false; } int ProviderRs232::open() @@ -225,7 +235,7 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t * data) QTimer::singleShot(500, this, SLOT(unblockAfterDelay())); return -1; } - QTimer::singleShot(5000, this, SLOT(unblockAfterDelay())); + _writeTimeout.start(); } else { @@ -235,6 +245,11 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t * data) return 0; } +void ProviderRs232::writeTimeout() +{ + Error(_log, "Timeout on write data to %s", _deviceName.toLocal8Bit().constData()); + closeDevice(); +} void ProviderRs232::unblockAfterDelay() { diff --git a/libsrc/leddevice/dev_serial/ProviderRs232.h b/libsrc/leddevice/dev_serial/ProviderRs232.h index a9aa3ca3..748b8678 100644 --- a/libsrc/leddevice/dev_serial/ProviderRs232.h +++ b/libsrc/leddevice/dev_serial/ProviderRs232.h @@ -45,6 +45,7 @@ private slots: int rewriteLeds(); /// Unblock the device after a connection delay + void writeTimeout(); void unblockAfterDelay(); void error(QSerialPort::SerialPortError error); void bytesWritten(qint64 bytes); @@ -84,12 +85,14 @@ protected: /// The RS232 serial-device QSerialPort _rs232Port; + /// A timeout timer for the asynchronous connection + QTimer _writeTimeout; + bool _blockedForDelay; bool _stateChanged; qint64 _bytesToWrite; - qint64 _bytesWritten; qint64 _frameDropCounter; QSerialPort::SerialPortError _lastError; qint64 _preOpenDelayTimeOut;