Parsing in serial communication

Hi
I am using arduino mega. I receive six byte packet from PC via serial port. My first byte is header, second byte is command code, next four bytes would be of float data, then one byte for op code and one byte for checksum. \n is delimiter for each packet. How can I separate these different data types as all are digits so I cannot use Serial.parsefloat() function
Thanx
Mahi

How can I separate these different data types as all are digits so I cannot use Serial.parsefloat() function

If they are all digits, you have a real problem. Use delimiters between values if the data IS ASCII. It sounds, though, like the data isn’t ASCII.

Show your code.

RaiMahi: Hi I am using arduino mega. I receive six byte packet from PC via serial port.

Your "six byte packet actually is 8 or 9 bytes, depending on the way of counting.

Let's count as xou describe the packet: My first byte is header, second byte is command code, now the packet has 2 bytes

next four bytes would be of float data, four more bytes, now the packet is 6 bytes already

then one byte for op code Now it is 7 bytes and one byte for checksum. Now it is 8 bytes

\n is delimiter for each packet. So it is 9 bytes in the packet including final '\n' and 8 bytes without, correct?

Use a struct

struct DATAPACKET
{
  byte header;
  byte command;
  float value;
  byte opcode;
  byte cs;
  byte end;
};

Once you have your NINE bytes, you can use a simple cast

void setup()
{
  // setup serial communication
  Serial.begin(9600);

  // received data
  byte serData[9] = {1, 2, 86, 14, 73, 64, 7, 8, '\n'};
  // cast to DATAPACKET
  DATAPACKET *data = (DATAPACKET*)serData;

  Serial.print("header:  "); Serial.println(data->header, HEX);
  Serial.print("command: "); Serial.println(data->command, HEX);
  Serial.print("value:   "); Serial.println(data->value);
  Serial.print("opcode:  "); Serial.println(data->opcode, HEX);
  Serial.print("cs:      "); Serial.println(data->cs, HEX);
  Serial.print("end:     "); Serial.println(data->end, HEX);
}

sterretje: Once you have your NINE bytes, you can use a simple cast

That's interesting and useful.

In the past I have used a union along with a struct -but your system looks simpler - except that it may duplicate the memory requirement.

The examples in Serial Input Basics can be used to collect the data prior to the cast.

...R

Robin2: That's interesting and useful.

In the past I have used a union along with a struct -but your system looks simpler - except that it may duplicate the memory requirement.

It's just a pointer; should only be 2 bytes extra.

sterretje: It's just a pointer; should only be 2 bytes extra.

Of course. It does not copy the data. I had missed that.

...R