Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to append the serial read data on to the QPlaintext on qt gui window
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    veera
    wrote on last edited by VRonin
    #4

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

    VRoninV 1 Reply Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #5

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

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • V veera

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

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #6

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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • V Offline
          V Offline
          veera
          wrote on last edited by
          #7

          @VRonin

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

          VRoninV 1 Reply Last reply
          0
          • V veera

            @VRonin

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #8

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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • V Offline
              V Offline
              veera
              wrote on last edited by
              #9

              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"

              VRoninV 1 Reply Last reply
              0
              • V Offline
                V Offline
                veera
                wrote on last edited by
                #10

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

                aha_1980A 1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tirupathi Korla
                  wrote on last edited by Tirupathi Korla
                  #11

                  @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

                  1 Reply Last reply
                  0
                  • V veera

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

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

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

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    0
                    • V veera

                      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"

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #13

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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        veera
                        wrote on last edited by
                        #14

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

                        J.HilkJ 1 Reply Last reply
                        0
                        • V veera

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

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

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

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          aha_1980A 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @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);
                            
                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @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

                            Qt has to stay free or it will die.

                            J.HilkJ 1 Reply Last reply
                            4
                            • aha_1980A aha_1980

                              @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

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by J.Hilk
                              #17

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


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved