Solved Trying to get usable information from QByteArray data
-
So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.
I do
QSerialPort* serial; //assume this is initialized and opened already QByteArray data = serial->readAll(); terminal->putData(data); //this makes the terminal read it out to me
I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.See:
QString newData = QString(data); //data from before qDebug() << newData;
This gives me the string but every character (sometimes every other character) is on a new line?
Output on terminal (What my debug should say):
Output at debug console:
Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.
Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.
-
[edit]http://doc.qt.io/qt-5/qstring.html#QString-8 and [/edit]
https://wiki.qt.io/Using_QByteArray may help -
I've been looking at those pages and others for hours and I understand it, but it doesn't make sense in practice. I've already converted the QByteArray to a QString, but it's in a weird format that I can't find any information on anywhere.
-
https://en.wikipedia.org/wiki/Null-terminated_string[edit] looks like it's just \n's nevermind me.
Just me eyeballing that - '\0' looks like you have multiple c style strings going on in that stream/buffer.Also \n
https://en.wikipedia.org/wiki/Escape_sequences_in_C
also "For example, \n is an escape sequence that denotes a newline character." -
@CynicalPenguin What does
qDebug() << data;
output?
See here: http://doc.qt.io/qt-5/qstring.html#QString-8
"Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8()".If the data you get is Latin1 you should use http://doc.qt.io/qt-5/qstring.html#fromLatin1-1:
QString newData = QString::fromLatin1(data);
-
@CynicalPenguin
if you're having trouble with QByteArray, you can easily convert it into an char array and work with that:QByteArray ba(someData); char *data = ba.data(); while (*data) { cout << "[" << *data << "]" << endl; ++data; }
-
@CynicalPenguin
The first thing you should do is look at what the raw data actually is byte-by-byte in theQByteArray
, before you try to convert it to a string. For example, if perchance it has a byte of value\00
in every other byte, it's likely UTF-16 rather than UTF-8 ("This gives me the string but every character (sometimes every other character) is on a new line") ? ... Once you know for sure what's in there you'll know what you need to do with it to get it into aQString
. -
@CynicalPenguin said in Trying to get usable information from QByteArray data:
=> 99,90 \n
=> 20,109,24,99,90
=> 20,109,24,99,90
this actually looks like you're sending the data from the other end in a loop, and don't wait for everything to arriveYou seem to terminate your data with a '\n' -char,
so try the following:if(serial->canReadLine()){ QByteArray data = serial->readLine(); QString myData = QString(data); }
Edit: fixed the example, the ';' made a lot of it useless.
-
Hi all. I thought the thread died so I didn't come back to it. I've solved (sorta) the problem with this code:
serialData = serial->read(serial->bytesAvailable()); //QByteArray defined in header serialBuffer += serialData; //QString defined in header if(serialBuffer.endsWith("\n")) { qDebug() << serial->bytesAvailable(); QStringList serialList = serialBuffer.split(",", QString::SkipEmptyParts); gsTerminal->putData(serialBuffer.toLocal8Bit()); //gsTerminal is a class derived from QPlainTextEdit //all putData(const QByteArray &data) does is inserts the data into the terminal qDebug() << serialList; serialBuffer.clear(); }
I did this two different ways and found this one was probably the most efficient out of the two I was trying. What was happening was for some reason, using the same data, when I did this:
serialData = serial->readAll(); gsTerminal->putData(serialData);
This added the data to my QPlainTextEdit class with no problem. BUT when I did this in the same line using the SAME data
QString s = QString(serialData); qDebug() << s;
So I found out what was happening was the bytes were coming a bit inconsistently. More specifically, the putData added all of the data in that order and formatted all of the data to look like it was preformatted, whereas qDebug() would take whatever was available and put it out to its own line. This is what confused me and threw me off. I'm going to try some of the things mentioned in the responses and report back to see if they work (because they're almost 100% more efficient than what I'm doing I believe).
-
@J.Hilk said in Trying to get usable information from QByteArray data:
@CynicalPenguin said in Trying to get usable information from QByteArray data:
=> 99,90 \n
=> 20,109,24,99,90
=> 20,109,24,99,90
this actually looks like you're sending the data from the other end in a loop, and don't wait for everything to arriveYou seem to terminate your data with a '\n' -char,
so try the following:if(serial->canReadLine());{ QByteArray data = serial->readLine(); QString myData = QString(data); }
This actually worked perfectly... I was trying to work out a fix for hours and eventually found one, but this is MUCH better than mine. Thanks