How to append the serial read data on to the QPlaintext on qt gui window



  • Hi All,
    I am successfully reading the data from the qtserial port need to display onto the QPlainText response window how to append ??



  • Assuming the data you receive is UTF-8 formatted and stored in a QByteArray named serialData

    QTextCursor cursor(plainTextEdit->document());
    cursor.movePosition(QTextCursor::End);
    cursor.insertText(QString::fromUtf8(serialData));
    


  • @VRonin ,
    Okay thanks ...I will try now ...



  • 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...


  • Qt Champions 2018

    @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 so cursor.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));
    


  • @VRonin

    i got this
    E�
    E�
    E�
    E�
    �
    E�
    E�
    E�
    E�
    �



  • What does qDebug()<<"Response: "<<serialData; print?



  • 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"



  • @VRonin
    I am getting the response as a hex bytes only so how to display onto the QPlainText ..



  • @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


  • Qt Champions 2018

    @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() with QString::fromLatin1() in that case.



  • @veera said in How to append the serial read data on to the QPlaintext on qt gui window:

    Response: "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"

    \x01 is not printable. What did you expect to see?



  • okay fine ...but response is coming what i am expecting i am writing some data for serial port like
    unsigned char rteenab[] = {0X01,0X0E,0X01,0X45,0XD1}; i am trying to print input and output onto the QPlaintext ......



  • @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);
    

  • Qt Champions 2018

    @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 :)

    [1] http://doc.qt.io/qt-5/qbytearray.html#toHex-1



  • @aha_1980 oO, I should read the change logs more thoroughly! Makes my life easier ;-) thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.