unsigned int echoNumber = 0; for (unsigned int echoNumber = 0; echoNumber<500; ++echoNumber) { QByteArray echoString = QString("ECHO_NUMBER_%1").arg(echoNumber).toLocal8Bit(); QByteArray cmdString = "ECHO " + echoString + "\r"; qint64 bytesWritten = m_impl->serialPort->write(cmdString); if (bytesWritten == -1) { LOG("spamEcho").error() << QString("writeError error %1").arg(m_impl->serialPort->error()); } else if (bytesWritten != cmdString.size()) { LOG("spamEcho").error() << QString("writeError (write all) error %1").arg(m_impl->serialPort->error()); } else if (!m_impl->serialPort->waitForBytesWritten(5000)) { LOG("spamEcho").error() << QString("write timed out error %1").arg(m_impl->serialPort->error()); } QByteArray readData; while (readData.size() < echoString.size()+5 && m_impl->serialPort->waitForReadyRead(300)) { QByteArray newData = m_impl->serialPort->readAll(); LOG("spamEcho").error() << "current read : " << QString(newData); readData.append(newData); } if (m_impl->serialPort->error()) { LOG("spamEcho").error() << QString("Read Error : %1").arg(m_impl->serialPort->error()); } if (readData[readData.size() - 1] != '\r' || readData.left(readData.size() - 5) != echoString) { LOG("spamEcho").error() << "Received " << QString(readData) << " instead of " << QString(echoString); } else { LOG("spamEcho").info() << "OK : received: " << QString(readData); } }