Unverified Commit 666b27fb authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #8885 from DonLakeFlyer/CherryPickBootloaderCrash

Fix Bootloader crash
parents f0568496 5b22bf87
...@@ -51,33 +51,27 @@ bool Bootloader::_write(QSerialPort* port, const uint8_t byte) ...@@ -51,33 +51,27 @@ bool Bootloader::_write(QSerialPort* port, const uint8_t byte)
return _write(port, buf, 1); return _write(port, buf, 1);
} }
bool Bootloader::_read(QSerialPort* port, uint8_t* data, qint64 maxSize, int readTimeout) bool Bootloader::_read(QSerialPort* port, uint8_t* data, qint64 cBytesExpected, int readTimeout)
{ {
qint64 bytesAlreadyRead = 0; QElapsedTimer timeout;
while (bytesAlreadyRead < maxSize) { timeout.start();
QElapsedTimer timeout; while (port->bytesAvailable() < cBytesExpected) {
timeout.start(); if (timeout.elapsed() > readTimeout) {
while (port->bytesAvailable() < 1) { _errorString = tr("Timeout waiting for bytes to be available");
if (timeout.elapsed() > readTimeout) {
_errorString = tr("Timeout waiting for bytes to be available");
return false;
}
port->waitForReadyRead(100);
}
qint64 bytesRead;
bytesRead = port->read((char*)&data[bytesAlreadyRead], maxSize);
if (bytesRead == -1) {
_errorString = tr("Read failed: error: %1").arg(port->errorString());
return false; return false;
} else {
Q_ASSERT(bytesRead != 0);
bytesAlreadyRead += bytesRead;
} }
port->waitForReadyRead(100);
} }
qint64 bytesRead;
bytesRead = port->read((char *)data, cBytesExpected);
if (bytesRead != cBytesExpected) {
_errorString = tr("Read failed: error: %1").arg(port->errorString());
return false;
}
return true; return true;
} }
......
...@@ -96,7 +96,7 @@ private: ...@@ -96,7 +96,7 @@ private:
bool _write(QSerialPort* port, const uint8_t* data, qint64 maxSize); bool _write(QSerialPort* port, const uint8_t* data, qint64 maxSize);
bool _write(QSerialPort* port, const uint8_t byte); bool _write(QSerialPort* port, const uint8_t byte);
bool _read(QSerialPort* port, uint8_t* data, qint64 maxSize, int readTimeout = _readTimout); bool _read(QSerialPort* port, uint8_t* data, qint64 cBytesExpected, int readTimeout = _readTimout);
bool _sendCommand(QSerialPort* port, uint8_t cmd, int responseTimeout = _responseTimeout); bool _sendCommand(QSerialPort* port, uint8_t cmd, int responseTimeout = _responseTimeout);
bool _getCommandResponse(QSerialPort* port, const int responseTimeout = _responseTimeout); bool _getCommandResponse(QSerialPort* port, const int responseTimeout = _responseTimeout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment