Convert QSerialPort Read To Int



  • 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);
    }
    

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    Did you check the content you are peeking ?



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


  • Lifetime Qt Champion

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



  • 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



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


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.