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. C++ How can I parse the data sent from the serial port?
QtWS25 Last Chance

C++ How can I parse the data sent from the serial port?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.0k 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.
  • mangekoyuM Offline
    mangekoyuM Offline
    mangekoyu
    wrote on last edited by
    #1

    I am trying to read ID over serial port. When I send my WT+12300 command via serial port. I am getting ID data. But it comes all the time and it doesn't come as parsed. "WT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2, I get it as 3,4,5,6,ASDSA,1,\r\n... and it keeps going. I just want to take the ASDSA part and display it with a qlabel. How can I do that?
    here is function

    void productDetail::on_pushButton_clicked()
    {
    
        if (serial.begin(QString("COM6"), 9600, 8, 0, 1, 0, false)  )
     {
    
          QString sendWC= "WR+11300";
          serial.send(sendWC);
           QString serialID = serial.getString();
           serialID = serialID.simplified();
           ui->idLabel->setText(serialID);
           qDebug()<< serialID;
    
      }
    
    JonBJ 1 Reply Last reply
    0
    • mangekoyuM mangekoyu

      I am trying to read ID over serial port. When I send my WT+12300 command via serial port. I am getting ID data. But it comes all the time and it doesn't come as parsed. "WT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2, I get it as 3,4,5,6,ASDSA,1,\r\n... and it keeps going. I just want to take the ASDSA part and display it with a qlabel. How can I do that?
      here is function

      void productDetail::on_pushButton_clicked()
      {
      
          if (serial.begin(QString("COM6"), 9600, 8, 0, 1, 0, false)  )
       {
      
            QString sendWC= "WR+11300";
            serial.send(sendWC);
             QString serialID = serial.getString();
             serialID = serialID.simplified();
             ui->idLabel->setText(serialID);
             qDebug()<< serialID;
      
        }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @mangekoyu said in C++ How can I parse the data sent from the serial port?:

      I just want to take the ASDSA part

      We don't what the ASDA "part" might mean to you....
      In any case, use either QString.indexOf() or QString::match() to find whatever it is you want in the string.

      mangekoyuM 1 Reply Last reply
      0
      • JonBJ JonB

        @mangekoyu said in C++ How can I parse the data sent from the serial port?:

        I just want to take the ASDSA part

        We don't what the ASDA "part" might mean to you....
        In any case, use either QString.indexOf() or QString::match() to find whatever it is you want in the string.

        mangekoyuM Offline
        mangekoyuM Offline
        mangekoyu
        wrote on last edited by
        #3

        @JonB I can print whatever I want on that part. To try, I randomly defined ASDSA to ID. my goal is to get the id part there. Could you please write an example?

        JonBJ 1 Reply Last reply
        0
        • mangekoyuM mangekoyu

          @JonB I can print whatever I want on that part. To try, I randomly defined ASDSA to ID. my goal is to get the id part there. Could you please write an example?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @mangekoyu
          You do not define what you mean by "take the ASDSA part", maybe you mean the number which comes as the next item after ASDSA,, who knows, I'm not going to guess..... In any case use either of the methods I suggested.

          mangekoyuM 1 Reply Last reply
          0
          • JonBJ JonB

            @mangekoyu
            You do not define what you mean by "take the ASDSA part", maybe you mean the number which comes as the next item after ASDSA,, who knows, I'm not going to guess..... In any case use either of the methods I suggested.

            mangekoyuM Offline
            mangekoyuM Offline
            mangekoyu
            wrote on last edited by mangekoyu
            #5

            @JonB I just want to take the 'ASDAS' part from the data I received and print it on a label. The only ASDAS part I need from the data I received.

            JonBJ 2 Replies Last reply
            0
            • mangekoyuM mangekoyu

              @JonB I just want to take the 'ASDAS' part from the data I received and print it on a label. The only ASDAS part I need from the data I received.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @mangekoyu
              I don't know what "the 'ASDAS' part" actually is, I have asked you twice, given you an example of what you might mean, and still no answer. So I leave that to you, I'm not going to ask again. If all you want is ASDA on a label go label->setText("ASDA").

              1 Reply Last reply
              0
              • mangekoyuM mangekoyu

                @JonB I just want to take the 'ASDAS' part from the data I received and print it on a label. The only ASDAS part I need from the data I received.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @mangekoyu
                Someone has now suggested you mean "The 8th comma-separated item., counting from 0, on each line". They are obviously a better mind-reader than I, since I did not get that.....

                So.... sounds like you want to

                • First, QString::split() your input string on \r\n, to get a list of lines.
                • Then, split again on , to get a list of "items", and you want [7].
                mangekoyuM 1 Reply Last reply
                2
                • JonBJ JonB

                  @mangekoyu
                  Someone has now suggested you mean "The 8th comma-separated item., counting from 0, on each line". They are obviously a better mind-reader than I, since I did not get that.....

                  So.... sounds like you want to

                  • First, QString::split() your input string on \r\n, to get a list of lines.
                  • Then, split again on , to get a list of "items", and you want [7].
                  mangekoyuM Offline
                  mangekoyuM Offline
                  mangekoyu
                  wrote on last edited by
                  #8

                  @JonB Thank You for help. It worked.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #9

                    An easier method:

                    QString serial="WT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2,3,4,5,6,ASDSA,1,\r\nWT+,ID,2";
                    QString id = serial.section(',', 7,7);
                    qDebug()<<id;  // id = ASDSA
                    
                    1 Reply Last reply
                    1
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #10

                      and it is still wrong because you cannot make assumptions about the content of input streams with regard to completeness. you have to read in and accumulate data until you have enough to parse, then parse what is available and discard what has been successfully processed. Maybe your serial IO is line buffered...maybe it is not. Don't make the assumption that any single serial read contains complete parseable data.

                      1 Reply Last reply
                      1

                      • Login

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