Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Convert QSerialPort Read To Int

    General and Desktop
    3
    9
    1478
    Loading More Posts
    • 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
      jorsborn last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • J
          jorsborn last edited by jorsborn

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • J
              jorsborn @SGaist last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • J
                  jorsborn last edited by

                  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

                  Flotisable 1 Reply Last reply Reply Quote 0
                  • Flotisable
                    Flotisable @jorsborn last edited by

                    @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 Reply Quote 2
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 2
                      • First post
                        Last post