-1

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 */

    }
sam
  • 1
  • 3
  • 2
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jul 20 '22 at 12:30
  • I'm not sure I see the problem, since you have read the data into a `QByteArray` you have read the data bytes already. If you want to check the header is 0x5555 then all you need is `if (data[0] == 0x55 && data[1] == 0x55)`. What do you mean by the code doesn't 'give anything', what are you expecting to happen? I think you need to be clearer about the goal here. – john Jul 20 '22 at 12:32
  • @man Debuggers go with compilers. So how you use your debugger depends on which compiler you are using. – john Jul 20 '22 at 12:34
  • Just to note that `if(0x55==data.indexOf(START_WORD))` looks very odd. Why would you expect the header bytes to located at offset/index `0x55`? – G.M. Jul 20 '22 at 12:35

1 Answers1

-1

enter image description here

I have this as result. I want to check if all my data start with the header 0 5555, because some as in the attached image do not start with the right header, otherwise I do not want to take into account the data without header 05555

sam
  • 1
  • 3
  • Two things wrong here, please don't post images, cut and paste the text, second you should edit your question, not post answers to it. But in any case, my comment to your original question explains how you can check the header with a simple if statement. – john Jul 20 '22 at 12:51
  • I used your answer, I got an error : use of overloaded operator – sam Jul 20 '22 at 12:56
  • If you post the code you are using and the full error message I'll see if I can help with that. – john Jul 20 '22 at 17:13
  • 1
    Please remove this as this is not an answer. This is a Q/A site, not forum. – László Papp Jul 20 '22 at 17:24