How to: Tokenization (Splitting) QBytearray data?
-
Greetings,
I am writing the Qt app, which receives the data over a Bluetooth connection. The data format is as following
sensor_1,sensor_2,Sensor_3,\r\n. How the received data In QBytearray can be split into relevant variables without losing the data being received continuously.Best regards,
Ahsan -
Greetings,
I am writing the Qt app, which receives the data over a Bluetooth connection. The data format is as following
sensor_1,sensor_2,Sensor_3,\r\n. How the received data In QBytearray can be split into relevant variables without losing the data being received continuously.Best regards,
Ahsan@ahsan737
You can split aQByteArrayinto a list on a character via QList<QByteArray> QByteArray::split(char sep) const. Or just use int QByteArray::indexOf(char ch, int from = 0) const. (Or convert toQStringand use corresponding methods, if appropriate.) Buffer received data as necessary. This will have no impact on the continuous arrival of data. -
Hi,
Cumulate the data until you have a full frame and then extract it and parse it.
-
I've tried this code to buffer incoming data, but it shows the error
Segmentation Fault.
How can I eliminate this error?incoming data: "315,317,1926,\r\n316,3"data is continuously being received, but currently, Bluetooth
<characteristic changed>has received this chunk.void MainWindow::dataReceived(QByteArray data) { buffer.setBuffer(&data); buffer.open(QIODevice::ReadWrite); while (buffer.canReadLine()) { QByteArray line = buffer.readLine().trimmed(); //reading data from bluetooth channel QString list= QString::fromUtf8(line.constData(), line.length()); //reading incoming ASCII data ui->receivedTextEdit->append(list); ui->receivedTextEdit->append("\n"); } } -
I've tried this code to buffer incoming data, but it shows the error
Segmentation Fault.
How can I eliminate this error?incoming data: "315,317,1926,\r\n316,3"data is continuously being received, but currently, Bluetooth
<characteristic changed>has received this chunk.void MainWindow::dataReceived(QByteArray data) { buffer.setBuffer(&data); buffer.open(QIODevice::ReadWrite); while (buffer.canReadLine()) { QByteArray line = buffer.readLine().trimmed(); //reading data from bluetooth channel QString list= QString::fromUtf8(line.constData(), line.length()); //reading incoming ASCII data ui->receivedTextEdit->append(list); ui->receivedTextEdit->append("\n"); } } -
@ahsan737 said in How to: Tokenization (Splitting) QBytearray data?:
but it shows the error Segmentation Fault.
Run from debugger and look at the stack trace window.
-
@ahsan737
Who knows. This code no longer is the same as what you posted earlier. You don't show anything aboutbufferis. I don't know about the scope of yourdata. You do not verify the return result frombuffer.open(). And so on.You can see for yourself from the stack trace that it's having trouble seeking for the `readLine(). You can use debugger to have a look at what's in the variables.
-
@ahsan737
Who knows. This code no longer is the same as what you posted earlier. You don't show anything aboutbufferis. I don't know about the scope of yourdata. You do not verify the return result frombuffer.open(). And so on.You can see for yourself from the stack trace that it's having trouble seeking for the `readLine(). You can use debugger to have a look at what's in the variables.