Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions libsrc/leddevice/dev_serial/ProviderRs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
{
Expand Down
5 changes: 4 additions & 1 deletion libsrc/leddevice/dev_serial/ProviderRs232.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,12 +85,14 @@ private slots:
/// 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;
Expand Down