Problem with qdebug
-
Hello, guys.
I'm speaking with one device via serial port. Every 60ms i ask special variable for current data and use qdebug to visualize this data.
@QByteArray answer;
answer=serial->readAll();
qDebug() << answer.simplified();@Thats what i see in terminal window
@"an"
"t.holding 1"
""
"a"
"nt.holding 1"
""
"ant.hold"
"ing 1"
"a"
"nt"
".holding 1"
"ant.hol"
"d"
"ing 1"
"an"
"t.holding 1"
""
"an"
"t"
".holding 1" @I suppose something with ASCII characters '\n' or others but i used simplified() function. Also Window's HyperTerminal shows data without trash.
-
You are showing only three lines of code, but is there a loop around those lines?
Readall suggests that you are reading everything at once, but is not very likely. I assume that you are reading each loop what you seeing on screen. In your example, the loop is 20 times executed.
Each call to qDebug() does introduce a line break at the end of the call. E.g. changing your code to
@
QByteArray answer;
answer=serial->readAll();
qDebug() << answer.simplified();
qDebug() << answer.simplified();
qDebug() << answer.simplified();
@would yield
@
"an"
"an"
"an"
"t.holding 1"
"t.holding 1"
"t.holding 1"
""
""
""
"a"
"a"
"a"
.
.
.
@ -
This is the loop.
@
connect(serial, SIGNAL(readyRead()), SLOT(detect()));void MainWindow::detect()
{
answer=serial->readAll();
qDebug() << answer.simplified();}@
And
@timer2->start(60);
connect(timer1, SIGNAL(timeout()), this, SLOT(ready()));void MainWindow::ready()
{
serial->write(command.at(1).toLocal8Bit().constData());
}
@If i understood you correctly i shouldn't use this way of reading?
-
No problem with your reading.
It is only the interpretation of what you see when using qDebug() I guess.The signal readyRead is triggered when there is something to read. However, for no class it is possible to know what and how much information is coming. So the trigger may just one or some more bytes and the slot routine will be called. ReadAll will have only the chance to read what is available at that time and exit. The next trigger will some more bytes and so on.
For the example you have given, you have apparently 20 times the slot is called.The only thing trowing your understanding off track is that you line breaks where you do not expect one. However, those line breaks are simply coming from qDebug(). Each time you use qDebug at the end of the line a line break will be introduced.
@
int a = 1;
int b = 2;
int c = 3;
qDebug() << a;
qDebug() << b;
qDebug() << c;
qDebug() << a << b << c;
@will display
@
1
2
3
123
@As the name suggests it has been designed for debugging. So the formatting is not always ideal as with other methods.
-
The whole things are coming through the serial port.
What you need to do accumulating those pieces in one QByteArray and scan this.
For instance you can use a member instance of QByteArray in your MainWindow class. I guess that is answer.
@
void MainWindow::detect()
{
QByteArray tmp = serial->readAll();
answer.append (tmp);
qDebug() << answer.simplified();
}
@You should see now that the information is accumulated. The rest is standard C++ handling. Checkout the "documentation of QByteArray":http://qt-project.org/doc/qt-5/QByteArray.html One of the "indexOf":http://qt-project.org/doc/qt-5/qbytearray.html#indexOf methods is a good candidate for your needs.