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()"
QtWS25 Last Chance

QSerialPort read string with "readAll()"

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportqiodevicereadlinereadyread
11 Posts 2 Posters 22.6k 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.
  • Basti46B Offline
    Basti46B Offline
    Basti46
    wrote on last edited by
    #1

    Hi guys,
    this is my first post and i am still a newbie. So far my application works just fine. But there is one problem. We need to read the data from the serialport. This data may contain only bytes so reading with the readLine() function could give us false or no data. Here is the code now so you can understand my problem.

    void MainWindow::readData()
    {
      //get data from serialport
      while(serial->canReadLine())
      {
           QByteArray data = serial->readLine();
           
           emit serialPortData(data, false);
    
           QString myString(data);
           if(myString.startsWith("SensorUpdate"))
           emit sensorData(myString) ;
      }
    
    }
    

    I am reading data and if it starts with a string ( some values ) i will send those values to another class where it will update a QTableWidget. But this "startsWith()" only works if i read line by line from the serial port. i want to use the readAll function and maybe store the chars until a new line is found or something like that. Can you help me?

    Thanks!

    tl;dr use readAll() to read line by line

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      • Basti46B Offline
        Basti46B Offline
        Basti46
        wrote on 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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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 :)

          Basti46B 1 Reply Last reply
          1
          • mrjjM mrjj

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

            Basti46B Offline
            Basti46B Offline
            Basti46
            wrote on 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!

            mrjjM 1 Reply Last reply
            0
            • Basti46B Basti46

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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 ?

              Basti46B 1 Reply Last reply
              1
              • mrjjM mrjj

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

                Basti46B Offline
                Basti46B Offline
                Basti46
                wrote on 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
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #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.

                  Basti46B 2 Replies Last reply
                  2
                  • mrjjM mrjj

                    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.

                    Basti46B Offline
                    Basti46B Offline
                    Basti46
                    wrote on 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
                    • mrjjM mrjj

                      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.

                      Basti46B Offline
                      Basti46B Offline
                      Basti46
                      wrote on 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.

                      mrjjM 1 Reply Last reply
                      1
                      • Basti46B Basti46

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Basti46
                        Good work!
                        :)

                        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