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. QSerialPort read string with "readAll()"
Forum Updated to NodeBB v4.3 + New Features

QSerialPort read string with "readAll()"

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportqiodevicereadlinereadyread
11 Posts 2 Posters 22.9k Views 1 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 1 Mar 2016, 10:46 last edited by
    #2

    Hi and welcome ;)

    If you look at Terminal Example you see how to connect up
    the readyRead signal and use readall()

    Normally when you do serial reading. you
    will read into a buffer (append to it) until u have the expected number of bytes or a
    "end of transmission" char is seen. like newline.
    Then a copy of buffer is send to processing and the buffer is cleared and it starts over.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Basti46
      wrote on 1 Mar 2016, 11:17 last edited by
      #3

      Thanks for your answer!
      I tried to adjust my code and got this here:

      header: QByteArray charBuffer;
      ....
      void MainWindow::readData()
      {
          QByteArray data = serial->readAll();
          console->putData(data,false);
          charBuffer.append(data);
          if (data.contains("\n")) //read into a structure until newline received.
          {
              QString myString(charBuffer);            
              if(myString.startsWith("SensorUpdate"))
              emit sensorData(myString);              
              charBuffer = "";                       
          }
      }
      

      But it still doesn't work :(

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 1 Mar 2016, 11:21 last edited by
        #4

        hi have you checked that input actually do contain "\n" ?
        Also which part is not working ?
        Does it enter
        if (data.contains("\n")) //read into a structure until newline received.
        {
        ...

        or not?

        Code does look fine :)

        B 1 Reply Last reply 1 Mar 2016, 11:38
        1
        • M mrjj
          1 Mar 2016, 11:21

          hi have you checked that input actually do contain "\n" ?
          Also which part is not working ?
          Does it enter
          if (data.contains("\n")) //read into a structure until newline received.
          {
          ...

          or not?

          Code does look fine :)

          B Offline
          B Offline
          Basti46
          wrote on 1 Mar 2016, 11:38 last edited by
          #5

          @mrjj The data i receive is correct. I used the debugger and got these results:
          data = "\r\nSensorupdate ,var1,var2,var3"
          myString ="\r\nSensorupdate ,var1,var2,var3";

          But the if statement does not work. The string is not emitted. I tried to use several if statements ("contains("\r\nSensorupdate","containts("Sensorupdate"),startsWirth("\r\nSensorupdate"). All of them dont seem to work.

          I tried my original code again to make sure nothing changed and yes it still works with the readLine() function... Darnnnn!

          M 1 Reply Last reply 1 Mar 2016, 11:51
          0
          • B Basti46
            1 Mar 2016, 11:38

            @mrjj The data i receive is correct. I used the debugger and got these results:
            data = "\r\nSensorupdate ,var1,var2,var3"
            myString ="\r\nSensorupdate ,var1,var2,var3";

            But the if statement does not work. The string is not emitted. I tried to use several if statements ("contains("\r\nSensorupdate","containts("Sensorupdate"),startsWirth("\r\nSensorupdate"). All of them dont seem to work.

            I tried my original code again to make sure nothing changed and yes it still works with the readLine() function... Darnnnn!

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 1 Mar 2016, 11:51 last edited by
            #6

            @Basti46
            Ok try with indexof
            http://doc.qt.io/qt-5/qstring.html#indexOf
            and Qt::CaseInsensitive

            i do wonder why containts("Sensorupdate") dont see it.

            Seems your \n comes first in data ?

            B 1 Reply Last reply 1 Mar 2016, 12:19
            1
            • M mrjj
              1 Mar 2016, 11:51

              @Basti46
              Ok try with indexof
              http://doc.qt.io/qt-5/qstring.html#indexOf
              and Qt::CaseInsensitive

              i do wonder why containts("Sensorupdate") dont see it.

              Seems your \n comes first in data ?

              B Offline
              B Offline
              Basti46
              wrote on 1 Mar 2016, 12:19 last edited by
              #7

              @mrjj
              Sooooo i think i found the problems!

              data from serialPort Example:
              does work
              "Sensorupdate ,var1,var2\r\n";
              does not work yet
              "\r\n"Sensorupdate ,var1,var2";
              does not work
              "Sensorupdate ,Sensor1,var2\r\n";
              "Sensorupdate ,Sensor2,var2\r\n";

              If i get more than one line at each run my gui is not able to handle the data. The readAll function is too fast and gives too much data too handle at a time. I need to handle each line one by one but the readAll function gives me several lines. Soo a solution would be to store the data into a file and read it from there. But maybe there is something else.
              Thanks @mrjj for ur time :D

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 1 Mar 2016, 12:23 last edited by mrjj 3 Jan 2016, 12:24
                #8

                hi
                in you GUI
                you could split the QString into a list
                http://doc.qt.io/qt-5/qstring.html#split
                split on \n
                Then loop the list and handle each as you normally would.
                So would not matter if u get more than one in as input.

                B 2 Replies Last reply 1 Mar 2016, 12:27
                2
                • M mrjj
                  1 Mar 2016, 12:23

                  hi
                  in you GUI
                  you could split the QString into a list
                  http://doc.qt.io/qt-5/qstring.html#split
                  split on \n
                  Then loop the list and handle each as you normally would.
                  So would not matter if u get more than one in as input.

                  B Offline
                  B Offline
                  Basti46
                  wrote on 1 Mar 2016, 12:27 last edited by
                  #9

                  @mrjj Right! I did not check if my Sensorupdate Class got the data so maybe there is the data but my current code was not able to use it! I will update soon

                  1 Reply Last reply
                  1
                  • M mrjj
                    1 Mar 2016, 12:23

                    hi
                    in you GUI
                    you could split the QString into a list
                    http://doc.qt.io/qt-5/qstring.html#split
                    split on \n
                    Then loop the list and handle each as you normally would.
                    So would not matter if u get more than one in as input.

                    B Offline
                    B Offline
                    Basti46
                    wrote on 1 Mar 2016, 13:39 last edited by
                    #10

                    @mrjj
                    Thank you sooo much!! It finally worked.

                    void SensorWidget::fillTable(QString serialString)
                    {
                       tempList = serialString.split("\r\n");
                    
                       for(int i=0;i<tempList.size();i++)
                       {
                           //filll table code
                       }
                    }
                    

                    This is what i had to do.

                    M 1 Reply Last reply 1 Mar 2016, 13:44
                    1
                    • B Basti46
                      1 Mar 2016, 13:39

                      @mrjj
                      Thank you sooo much!! It finally worked.

                      void SensorWidget::fillTable(QString serialString)
                      {
                         tempList = serialString.split("\r\n");
                      
                         for(int i=0;i<tempList.size();i++)
                         {
                             //filll table code
                         }
                      }
                      

                      This is what i had to do.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 1 Mar 2016, 13:44 last edited by
                      #11

                      @Basti46
                      Good work!
                      :)

                      1 Reply Last reply
                      0

                      11/11

                      1 Mar 2016, 13:44

                      • Login

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