How to append the serial read data on to the QPlaintext on qt gui window
-
I am reading a data as serial.readall()
if(serial.waitForReadyRead(20000)){ //Data was returned // QString ser_Buffer = serial.readAll(); qDebug()<<"Response: "<<serial.readAll(); //ui->ResWindow_plainTextEdit->appendPlainText(serial.readAll()); //QTextCursor cursor(plainTextEdit->document()); QTextCursor cursor(ui->ResWindow_plainTextEdit->document()); cursor.movePosition(QTextCursor::End); //cursor.insertText(QString::fromUtf8(serialData)); cursor.insertText(QString::fromUtf8(serial.readAll())); // ui->ResWindow_plainTextEdit->appendPlainText(serial.readAll()); usleep(200000); }else{ //No data qDebug()<<"Time out"; } //I have finish alla operation serial.close(); }else{ qDebug()<<"Serial ttyS4 not opened. Error: "<<serial.errorString(); }
still not able to display on the QPlainText...
-
@veera said in How to append the serial read data on to the QPlaintext on qt gui window:
if(serial.waitForReadyRead(20000)){
That may be the cause of your problem.
You will most likely use the readyRead signal and connect it to your slot where you read all the data.
I have given you already the link to the terminal example in you other thread, please take that as reference.
-
after
qDebug()<<"Response: "<<serial.readAll();
,serial
will empty its buffer socursor.insertText(QString::fromUtf8(serial.readAll()));
appends nothing.const auto serialData = serial.readAll(); qDebug()<<"Response: "<<serialData; QTextCursor cursor(ui->ResWindow_plainTextEdit->document()); cursor.movePosition(QTextCursor::End); cursor.insertText(QString::fromUtf8(serialData));
-
yes its writing on plaintext window.......
qDebug()<<"Response: "<<serialData; print?
Response: "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
The portName is "ttyS4"
currentPortName "ttyS4"
Response: "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01" -
@veera
Hi,
Serial.readall() returns QByteArray. Have you tried this?QString res(serialData); ui->ResWindow_plainTextEdit->appendPlainText(res);
And one more question. Is that
if(serial.waitForReadyRead(20000))
condition running inside any loop?-Tirupathi
-
@veera said in How to append the serial read data on to the QPlaintext on qt gui window:
@VRonin
I am getting the response as a hex bytes only so how to display onto the QPlainText ..If you want to show the hex bytes in the text edit, you can use (modified @VRonin's example (the change is the toHex() in the first line):
const auto serialData = serial.readAll().toHex(); qDebug()<<"Response: "<<serialData; QTextCursor cursor(ui->ResWindow_plainTextEdit->document()); cursor.movePosition(QTextCursor::End); cursor.insertText(QString::fromUtf8(serialData));
Note that you could replace
QString::fromUtf8()
withQString::fromLatin1()
in that case. -
@veera
You'll have to create a formated QString out of your Qbytearray for that. @VRonin and @aha_1980 showed you how to do it, heres a 3rd Way(I prefere a seperation and a more readable String):const auto serialData = serial.readAll() qDebug()<<"Response: "<<serialData; QString formatedText; for(int i(0); i<serialData .size(); i++) formatedText.append(QString::number((serialData.at(i),16).toUpper().rightJustified(2,'0')+ QChar(0x20)); QTextCursor cursor(ui->ResWindow_plainTextEdit->document()); cursor.movePosition(QTextCursor::End); cursor.insertText(formatedText);
-
@J.Hilk: Note that for Qt 5.9 I added an overload of QByteArray::toHex(char separator) [1] where you can do something like
QByteArray hexData = data.toHex(' ');
directly :)