Gyroscope sensor UM6 communication : no header packet "snp" found

Hi every one,

I use UM6 (gyro) with Arduino mega ADK. I like to read a packet from UM6 sensor, so I start with reading the header snp.
I use serial port1. the UM6 datasheet is here : http://www.chrobotics.com/docs/UM6_datasheet.pdf
My code return “n wasn’t found” , so I can’t find n just after the s caracter.This is my code :
//UM6 is in broadcast mode with baudrate 19200
//this proram try to find a correct header “snp”
byte data[63];
int n = 0;

void setup(){
Serial.begin(19200);
Serial1.begin(19200);
}

void loop()
{
n = Serial1.available();
if (n == 63){
//s’il existe des données venant de l’UM6, on les copie dans data
for (byte i=0; i < n; i++){
data = Serial1.read();
}
//on teste s’il existe s n p dans ce tableau data
for (byte i=0; i < n-1; i++){
if (data == ‘s’)
{
if (data[i+1] == ‘n’)
{
if (data[i+2] == ‘p’)
{
Serial.println (“snp is found”);
}
else Serial.println (“p wasn’t found”);
}
else Serial.println (“n wasn’t found”);
}
}
//le test de snp est terminé
}
}

First of all, use code tags to post your code - the forum software has mangled it.

Then I'd suggest that you null terminate your data array once you've read it and print what it contains to serial to help you debug.

Thanks for your reply, In fact my problem that I can’t found ‘s’ ‘n’ ‘p’. when I find ‘n’ , Idon’t found ‘p’
and when I found ‘s’ , next I don’t find ‘n’ …

Indeed. But if you display what you did actually get, perhaps you can figure out what is wrong.

   n = Serial1.available();
   if (n == 63){

Do nothing until the buffer is just about full. Why?

I just wait the buffer to be full, to be sure that I have a complet packet This code is just to check packets received from the UM6 wich run in broadcasting mode

I just wait the buffer to be full, to be sure that I have a complet packet

Malarkey. You could have the second half of one packet and the first half of another. Waiting until the buffer is (nearly) full is NOT how to read serial data.

yes, in fact the UM6 sensor send many packets and when the buffer is full, oldest byte is replaced and so on ... --> when my program read a byte after byte, may the buffer is changed with new packets and never I get "snp" I need to read buffer in fast way to be sure that I read the buffer in continuos way

So my problem is how to read the serial data in a correct frequency !