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); }
-
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.
-
Did you check the content you are peeking ?
-
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); }
-
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); }
@jorsborn
it looks liketoInt
work as the conversion ofQString
so it only work when theQByteArray
is a string that describe a number of base 2~36( according to the document )and
toHex
convert theQByteArray
by seeing its character as a hex coded bytetake
"20"
for examplein
toInt
, with base 4"20"
will be8
, with base 8"20"
will be16
in
"toHex"
,"20"
will be"3230"
('2'
is50
with base 10, and32
with base 16,'0'
is48
with base 10 and30
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 to10
-
handheldCOM->peek(1) != QLatin1Char('A')
might be simpler.