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. Convert QSerialPort Read To Int
Forum Updated to NodeBB v4.3 + New Features

Convert QSerialPort Read To Int

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.2k Views 3 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.
  • J Offline
    J Offline
    jorsborn
    wrote on 15 Feb 2017, 22:22 last edited by
    #1

    This is my first project in QT and I am a bit frustrated here. I am trying to discard all bytes from the read buffer of my QSerialPort object (handheldCOM) until I get a 0x41. I was trying to use the toInt method which should yield 65 for 0x41. I am doing something fundamentally wrong in my while because it zooms right past 0x41 (I can see this through my qDebug).

    //Discard all bytes until 0x41 is read
    while(handheldCOM->peek(1).toInt()!=65){
        qDebug() << "Toss out:" << handheldCOM->peek(1);
        handheldCOM->read(1);
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Feb 2017, 22:30 last edited by
      #2

      Hi and welcome to devnet,

      The usual pattern here would rather be to connect the readyRead signal to a parsing function and in there discard what you don't want.

      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
      0
      • J Offline
        J Offline
        jorsborn
        wrote on 15 Feb 2017, 22:38 last edited by jorsborn
        #3

        Thanks for the help! That's actually what I am doing. The snippet of code is from within my parsing function connected to readyRead.

        My packets always begin with 0x41 and there is potential for other garbage in the receive buffer. So, what I intend to do is peek at the buffer and discard if necessary until 0x41 is found. The remaining bytes will then be the packet of interest which will further be verified by a checksum. The problem is that ....

        while(handheldCOM->peek(1).toInt()!=65)
        

        Doesn't seem to evaluate as expected and I don't understand why.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Feb 2017, 22:45 last edited by
          #4

          Did you check the content you are peeking ?

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

          J 1 Reply Last reply 15 Feb 2017, 22:50
          0
          • S SGaist
            15 Feb 2017, 22:45

            Did you check the content you are peeking ?

            J Offline
            J Offline
            jorsborn
            wrote on 15 Feb 2017, 22:50 last edited by
            #5

            Yes, in the original snippet the debug statement outputs "Toss out: 'A'" so the data is there. It seems that toInt doesn't work as I believe it should.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Feb 2017, 22:54 last edited by
              #6

              What exactly are you receiving ? Something like AA or 0xAA ?

              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
              0
              • J Offline
                J Offline
                jorsborn
                wrote on 15 Feb 2017, 23:47 last edited by
                #7

                I am new to QT so I may not be using the best facilities to debug this situation. However, I've added a few more qDebug statements so that you can see what I am getting. Have a look at the screenshot to see what I am doing and what the output is. I don't understand why toInt doesn't work.

                I should also note that I have tried to change the while as follows below but I get a compile error "QByteArray::operator QNoImplicitBoolCast() const' is private".

                    //Discard all bytes until 0x41 is read
                    while(handheldCOM->peek(1).toHex()!=0x41){
                        qDebug() << "Toss out:" << handheldCOM->peek(1);
                        handheldCOM->read(1);
                    }
                

                alt text

                F 1 Reply Last reply 16 Feb 2017, 05:10
                0
                • J jorsborn
                  15 Feb 2017, 23:47

                  I am new to QT so I may not be using the best facilities to debug this situation. However, I've added a few more qDebug statements so that you can see what I am getting. Have a look at the screenshot to see what I am doing and what the output is. I don't understand why toInt doesn't work.

                  I should also note that I have tried to change the while as follows below but I get a compile error "QByteArray::operator QNoImplicitBoolCast() const' is private".

                      //Discard all bytes until 0x41 is read
                      while(handheldCOM->peek(1).toHex()!=0x41){
                          qDebug() << "Toss out:" << handheldCOM->peek(1);
                          handheldCOM->read(1);
                      }
                  

                  alt text

                  F Offline
                  F Offline
                  Flotisable
                  wrote on 16 Feb 2017, 05:10 last edited by
                  #8

                  @jorsborn
                  it looks like toInt work as the conversion of QString
                  so it only work when the QByteArray is a string that describe a number of base 2~36( according to the document )

                  and toHex convert the QByteArray by seeing its character as a hex coded byte

                  take "20" for example

                  in toInt , with base 4 "20" will be 8, with base 8 "20" will be 16

                  in "toHex", "20" will be "3230"( '2' is 50 with base 10, and 32 with base 16, '0' is 48 with base 10 and 30 with base 16 )

                  so toInt cannot convert 'A' which is not a 10 base number( by default, toInt convert number using base 10 )

                  but if you use toInt( nullptr , 16 ) , 'A' will be converted to 10

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 16 Feb 2017, 07:52 last edited by
                    #9

                    handheldCOM->peek(1) != QLatin1Char('A') might be simpler.

                    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

                    7/9

                    15 Feb 2017, 23:47

                    • Login

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