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. Reading from SerialPort and convert it to QString?!

Reading from SerialPort and convert it to QString?!

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 6.5k Views 2 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Are you sure there's no null character in the data you send ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • MucipM Offline
      MucipM Offline
      Mucip
      wrote on last edited by
      #3

      Hi,
      Thanks for fast return. :)

      May be?! I don't know. It can be "A 123456" or "A 123". Depends on Indicator. It can send 123 value or 123456?!
      The indicator sends 6 long int value.

      But I see it in Plain text. No problem?!... :(

      Regards,
      Mucip:)

      VRoninV 1 Reply Last reply
      0
      • MucipM Mucip

        Hi,
        Thanks for fast return. :)

        May be?! I don't know. It can be "A 123456" or "A 123". Depends on Indicator. It can send 123 value or 123456?!
        The indicator sends 6 long int value.

        But I see it in Plain text. No problem?!... :(

        Regards,
        Mucip:)

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

        @Mucip said in Reading from SerialPort and convert it to QString?!:

        But I see it in Plain text.

        How do you see it? in debug mode?

        "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
        • MucipM Offline
          MucipM Offline
          Mucip
          wrote on last edited by
          #5

          Hi,
          No. I already add the plaintextEditor on the form. and below code:

          ui->plainTextEdit->insertPlainText(QString(ba));

          I can see the results in the editor. No problem!... :(
          Like below;

          A 720
          A 720
          A 720

          Regards,
          Mucip:)

          J.HilkJ 1 Reply Last reply
          0
          • MucipM Mucip

            Hi,
            No. I already add the plaintextEditor on the form. and below code:

            ui->plainTextEdit->insertPlainText(QString(ba));

            I can see the results in the editor. No problem!... :(
            Like below;

            A 720
            A 720
            A 720

            Regards,
            Mucip:)

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

            @Mucip if you enter a Text via Qt-Designer, can you see that, whan the program is running?
            Can you highlight the Label/LineEdit and copy paste the text into a text document?


            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
            • MucipM Offline
              MucipM Offline
              Mucip
              wrote on last edited by
              #7

              Hi,
              I didn't understand? Please could you explain little bit more?...
              Regards,
              Mucip:)

              J.HilkJ 1 Reply Last reply
              0
              • MucipM Offline
                MucipM Offline
                Mucip
                wrote on last edited by
                #8

                Hi,
                I checked.
                When I highlight and copy from PlainTextEditor I can paste it on the TextEditor of windows.
                But when I highlight and copy it from LineEdit I can not paste it on the TextEditor of windows.

                I think between 2th and 7th bits contain NULL when the value less then 6 digits like "200".

                So what happend if the ByteArray has NULL and why PlainTextEditor shows the value (like A 720) well and Why LineEdit NOT?!...

                Regards,
                Mucip:)

                1 Reply Last reply
                0
                • MucipM Mucip

                  Hi,
                  I didn't understand? Please could you explain little bit more?...
                  Regards,
                  Mucip:)

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

                  @Mucip Well I just wanted to see, what is actually in that QByteArray

                  You could use this function to "see" the content of your data:

                  for(int i(0); i < ba.size();i++)
                          qDebug() <<  i << QChar(ba[i]) << static_cast<int>(ba[i]);
                  

                  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
                  • MucipM Offline
                    MucipM Offline
                    Mucip
                    wrote on last edited by
                    #10

                    @J.Hilk said in Reading from SerialPort and convert it to QString?!:

                    for(int i(0); i < ba.size();i++)
                    qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);

                    Hi,
                    I used it and result:
                    0 'A' 65
                    0 ' ' 32
                    0 ' ' 32
                    1 '1' 49
                    0 '4' 52
                    1 '5' 53
                    2 '0' 48
                    0 '\xd' 13

                    And Idicator shows 1450... This is correct! :)
                    But How can I get the complate output like "A1450"???

                    Regards,
                    Mucip:)

                    J.HilkJ 1 Reply Last reply
                    0
                    • aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @J.Hilk said in Reading from SerialPort and convert it to QString?!:

                      @Mucip Well I just wanted to see, what is actually in that QByteArray
                      You could use this function to "see" the content of your data:
                      for(int i(0); i < ba.size();i++)
                      qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);

                      Easier would be to just convert the byte array to hex:

                      qDebug() << ba.toHex();

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      0
                      • MucipM Mucip

                        @J.Hilk said in Reading from SerialPort and convert it to QString?!:

                        for(int i(0); i < ba.size();i++)
                        qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);

                        Hi,
                        I used it and result:
                        0 'A' 65
                        0 ' ' 32
                        0 ' ' 32
                        1 '1' 49
                        0 '4' 52
                        1 '5' 53
                        2 '0' 48
                        0 '\xd' 13

                        And Idicator shows 1450... This is correct! :)
                        But How can I get the complate output like "A1450"???

                        Regards,
                        Mucip:)

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

                        @Mucip said in Reading from SerialPort and convert it to QString?!:

                        0 '\xd' 13

                        I believe thats the culprit a carriage Return ist difficult to display in a QLabel and/or QLineEdit

                        Also, seeing your index starting from 0 multiple times the text of the QLabel and QLineEdit are constantly overwritten. You'll need to construct a String out of your different received QByteArrays.
                        I suggest something like this. If it always end with QChar(0x0D)

                        //Member String
                        QString str;
                        
                        void MainWindow::serialReceived()
                        {
                            QByteArray ba;
                            bool finished(false);
                            ba= serial->readAll();
                            for(int i(0); i < ba.size();i++){
                                 if(ba[i] != 0x0D)
                                     str.append(ba[i]);
                                 else finished = true;
                           }
                        
                            if(finished){
                               ui->label_2->setText(str);
                               ui->plainTextEdit->insertPlainText(str);
                               str.clear();
                            }
                        }
                        

                        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.

                        MucipM 1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

                          can you try:

                          void MainWindow::serialReceived()
                          {
                          QTextStream stream(serial);
                          const QString receivedStr = stream.readAll();
                          
                              ui->label_2->setText(receivedStr );
                             ui->plainTextEdit->insertPlainText(receivedStr );
                          
                          }
                          

                          Of course there is nothing here assuring you all data was received when the slot gets called

                          "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
                          • MucipM Offline
                            MucipM Offline
                            Mucip
                            wrote on last edited by
                            #14

                            Hi,
                            No. Unfotunatelly same thing. I can see the data in plainTextEdit but not in lineEdit.

                            Regards,
                            Mucip:)

                            VRoninV 1 Reply Last reply
                            0
                            • MucipM Mucip

                              Hi,
                              No. Unfotunatelly same thing. I can see the data in plainTextEdit but not in lineEdit.

                              Regards,
                              Mucip:)

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

                              @Mucip said in Reading from SerialPort and convert it to QString?!:

                              I can see the data in plainTextEdit but not in lineEdit

                              You are not setting the data in the lineedit, you are setting the string in a label and a plainTextEdit

                              "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
                              • J.HilkJ J.Hilk

                                @Mucip said in Reading from SerialPort and convert it to QString?!:

                                0 '\xd' 13

                                I believe thats the culprit a carriage Return ist difficult to display in a QLabel and/or QLineEdit

                                Also, seeing your index starting from 0 multiple times the text of the QLabel and QLineEdit are constantly overwritten. You'll need to construct a String out of your different received QByteArrays.
                                I suggest something like this. If it always end with QChar(0x0D)

                                //Member String
                                QString str;
                                
                                void MainWindow::serialReceived()
                                {
                                    QByteArray ba;
                                    bool finished(false);
                                    ba= serial->readAll();
                                    for(int i(0); i < ba.size();i++){
                                         if(ba[i] != 0x0D)
                                             str.append(ba[i]);
                                         else finished = true;
                                   }
                                
                                    if(finished){
                                       ui->label_2->setText(str);
                                       ui->plainTextEdit->insertPlainText(str);
                                       str.clear();
                                    }
                                }
                                
                                MucipM Offline
                                MucipM Offline
                                Mucip
                                wrote on last edited by
                                #16

                                @J.Hilk said in Reading from SerialPort and convert it to QString?!:

                                void MainWindow::serialReceived()
                                {
                                QByteArray ba;
                                bool finished(false);
                                ba= serial->readAll();
                                for(int i(0); i < ba.size();i++){
                                if(ba[i] != 0x0D)
                                str.append(ba[i]);
                                else finished = true;
                                }

                                Hi,
                                I solved the problem above like solution...
                                I checked the data one by one and add to QString.
                                Thanks.

                                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