QSerialPort: ASSERT failure in QList<T>::operator[]: "index out of range"
-
Hello
I have an application that must read data from serial port, communicating with mbed device. The QSerialPort is running in separate thread, and the reading of the incoming data happens in this slot:
@void SerialPortT::readyRead()
{
if(port->bytesAvailable()) {
QByteArray data = port->readAll();if(!data.isEmpty()) { char *temp = data.data(); for(int i = 0; temp[i] != '\0'; i++) { switch(STATE) { case WAIT_START: if(temp[i] == START_MSG) { STATE = IN_MESSAGE; break; } break; case IN_MESSAGE: if(temp[i] == END_MSG) { STATE = WAIT_START; QStringList incomingData = receivedData.split(' '); emit newData(incomingData); break; } else if(isdigit(temp[i]) || isspace(temp[i]) ) { receivedData.append(temp[i]); } break; default: break; } } } }
}@
The program runs for a while and then crashes with the message in the topic. I am commenting the parsing part of the slot for the testing and just displaying the received QByteString, so the parsing is not actually the reason why this happens. I have used assert messages before and after the readAll() method, and it looks that the crash happens while running this method; the after assert message is not printed.
Thanks
-
Hi,
Are you sure that you get a '\0' each time ?
-
Actually, for the tests I comment out the parsing part and use qDebug() to print out the received byte array, provided bytesAvailable() si greater than zero and the byte array is not empty.
However, the error happens only when I use the port in a separate thread, When I run the QSerialPort in the main thread, it works fine (at least for an hour). -
Since you are using it in another thread, can you show the code ?
-
Sure, but the code is what I have showed here - it subclasses QThread. I am opening the port in the run() method and then entering the exec() loop; the QSerialPort's readyRead() signal is connected to a slot that does the same as the code I have posted. Here is the run()
@void SerialPortT::run()
{
port = new QSerialPort(portInfo, 0);if(port->open(QIODevice::ReadOnly)) { if(! port->setBaudRate(baudRate)) { qDebug() << "Baud Rate Error::" << port->errorString(); } if(!port->setParity(parity)) { qDebug() << "Parity Error:" << port->errorString(); } if(!port->setDataBits(dataBits)) { qDebug() << "Data Bits Error::" << port->errorString(); } if(!port->setStopBits(stopBits)) { qDebug() << "Stop Bits Error:" << port->errorString(); } connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(portError(QSerialPort::SerialPortError))); connect(port, SIGNAL(readyRead()), this, SLOT(readyRead())); emit portOpenOK(); qDebug() << "Port: " << port->portName() << "Mode: " << port->openMode(); } else { emit portOpenFail(); } exec();
}@
portInfo is QSerialPlotInfo object. That is actually the whole thread except the constructor and destructor.
So far my appliation is running fine with QCustomPlot and the QSerialPort in the same thread. I think I will leave it like this. -
You should rather implement the worker object paradigm.
Also, did you run your application using the debugger to see exactly where it fails ?