how to read the data bytes of a packet?
-
I would like to extract each time the data bytes of my IMU data which are in the following form:
"5555 41321e7fea02a2efb400010005fffe00d6fff90cbb261926192619006692340000bf5f"
I would like to make sure that the data received header 0x5555 , because The packet header is always the bit pattern 0x5555.
I tried with this code but it doesn't give anything, I don't really know how to read the bytes of a packet ( sorry I am a beginner ) .
void Imu::pollSerialPort()
{
static const unsigned char START_BYTES[2] = {0x55};static const QByteArray START_WORD((char*)START_BYTES,2);
int numToPop=0;static QTime startTime = QTime::currentTime();
QByteArray data= serialPort->readAll().toHex();
qDebug() << "Serial received " << data;// find header
for(numToPop=0; numToPop+1<data.size() ;numToPop+=1)
{
if(0x55==data.indexOf(START_WORD))break;
else
log_warning("imu","dropping %d bytes before header recovery");
/* header was not found */}
-
@manel-sam said in how to read the data bytes of a packet?:
QByteArray data= serialPort->readAll().toHex();
No need to convert to string.
Seems to work:
char hd[]={0x55,0x55}; QByteArray dataHD=data.left(2); if(dataHD.compare(hd)) { qDebug()<<"OK"; }
-
@manel-sam
In addition to what @mpergand has written, you should not write your code the way you have to assumeserialPort->readAll()
will return all the bytes sent to you. It only retrieves whatever is available at the instant it is called. Sooner or later your code approach will go wrong. You will need to rewrite to save/buffer the bytes received. -
@manel-sam said in how to read the data bytes of a packet?:
but when the header is different from 0*5555
Then 'OK' is not printed to your debug console.
-
@manel-sam
Please take your own time to read docs at int QByteArray::compare(). @mpergand made a slip, it should beif (dataHD.compare(hd) == 0) qDebug()<<"OK";
but you can look that up yourself. -
@Christian-Ehrlicher it's printed