How to read 17bytes from a sensor via Serial?

Hi,

I'm attached a Sensor IR-Array Sensor. With the following code I'm reading every byte it sends.

int incomingByte = 0;   // for incoming serial data
void setup() {
        Serial.begin(11500);     // opens serial port for console 
        Serial.print("Output active");
        Serial1.begin(19200);     // opens serial port to sensor
        Serial.print("Sensor active");
}

void loop() {

        if (Serial1.available() > 0) {
              
                Serial1.read();
 Serial.println(incomingByte , HEX);
   
        }
}

now the sensor has a output format of total 17bytes. (please take look at the Picture ).

How can I read this 17bytes correctly from the beginning, so I don't grabing the data from the middle?

Please Help.

superkato

How can I read this 17bytes correctly from the beginning, so I don't grabing the data from the middle?

Read two bytes. See if the first one is 0x00 and if the second one is 0xDF. If not, move byte to to byte 1, and read another byte. Repeat until the two bytes are 0x00 and 0xDF.

Then, read the next 15 bytes, as they become available.

Of course, as you read a value, you need to assign it to a variable. Discarding it (as you are doing) doesn't make sense.

00 DF and so on is just an example

the values could be 0x6c 0x2F 0x04 0x2F

What sensor is it + links to full datasheet please.

its a custom sensor IR array, there is no official spec yet.

its a custom sensor IR array, there is no official spec yet.

What causes it to start sending serial data? Does it send data only in response to a "Hey, there" kind of command?

after its powered on it starts sending :frowning: I wish there would be a start command but there is no.

could that the right way?

byte SerialData[17]; // 17byte for my Sensor Datagram
byte SensorData;
int index = 0; // Index into array; where to store the character
int T1 = 0;
int T2 = 0;
int T3 = 0;
int T4 = 0;
int T5 = 0;
int T6 = 0;
int T7 = 0;
int T8 = 0;

void setup() {
  // initialize serial communications
  Serial.begin(115200); //console
  Serial1.begin(19200); //sensor
}

void loop()
{
   while(Serial.available() > 0) // Don't read unless
								
   {
      for(index = 0; index == 16; index++) //0 to 16 is 17
      {
        SensorData = Serial.read();
        SerialData[index] = SensorData;
      }
   }
   
   byte Pix1_0 = int(SerialData[0]);
   byte Pix1_1 = int(SerialData[1]);
   byte Pix2_0 = int(SerialData[2]);
   byte Pix2_1 = int(SerialData[3]);
   byte Pix3_0 = int(SerialData[4]);
   byte Pix3_1 = int(SerialData[5]);
   byte Pix4_0 = int(SerialData[6]);
   byte Pix4_1 = int(SerialData[7]);
   byte Pix5_0 = int(SerialData[8]);
   byte Pix5_1 = int(SerialData[9]);
   byte Pix6_0 = int(SerialData[10]);
   byte Pix6_1 = int(SerialData[11]);
   byte Pix7_0 = int(SerialData[12]);
   byte Pix7_1 = int(SerialData[13]);
   byte Pix8_0 = int(SerialData[14]);
   byte Pix8_1 = int(SerialData[15]);
   byte Checksum = int(SerialData[16]);
   
 //calculate Temperature Pixel 1
 
 T1 = 256*Pix1_0+Pix1_1;
 T1 = T1/10;
 
 //calculate Temperature Pixel 2
 T2 = 256*Pix2_0+Pix2_1;
 T2 = T2/10;
  
 }

could that the right way?

   while(Serial.available() > 0) // Don't read unless							
   {
      for(index = 0; index == 16; index++) //0 to 16 is 17
      {
        SensorData = Serial.read();
        SerialData[index] = SensorData;
      }
   }

If there is at least one byte available, read all 17 of them. Hardly seems useful to store 16 -1s in the array.

That won't read a complete packet, anyway. It will simply, assuming that there are 17 or more bytes available, grab 17 bytes. Not necessarily the correct 17 to make a packet.

How often does the device send data? Is is a continuous stream, or is there a long (relatively) gap between the spaced parts that make up a packet?

its sending continuously with a gap of 388msec.

I will try the Serial.readBytes class.

( Damn I wish I had more knowledge :frowning: Im just a beginner and I wanted to do this for my dads project.)

its sending continuously with a gap of 388msec.

So, you could read and discard serial data until the buffer is empty. Record that happens. When there is serial data again, record when that happens. If the time between events is greater than 360 milliseconds, you are at the start of a new packet.

superkato:
its sending continuously with a gap of 388msec.

I will try the Serial.readBytes class.

( Damn I wish I had more knowledge :frowning: Im just a beginner and I wanted to do this for my dads project.)

As PaulS says to wait for the 388ms delay, record 17 bytes then confirm by re-calculating the 16 byte checksum and comparing to be sure you have read valid data.

how would I do that function ?

It appears with your sensor the end of transmission marker is basically the 388msec. delay. Below is a simple setup that uses a small delay to allow the input buffer to be filled until the sensor delay occurrs.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}