Pages: [1]   Go Down
Author Topic: How to read 17bytes from a sensor via Serial?  (Read 541 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm attached a Sensor IR-Array Sensor. With the following code I'm reading every byte it sends.
Code:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

00 DF  and so on is just an example

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

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What sensor is it + links to full datasheet please.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

after its powered on it starts sending smiley-sad  I wish there would be a start command but there is no.


could that the right way?

Code:
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;
  
 }
 
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
could that the right way?
Code:
   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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its sending continuously with a gap of 388msec.

I will try the Serial.readBytes class.

( Damn I wish I had more knowledge smiley-sad  Im just a beginner  and I wanted to do this for my dads project.)
« Last Edit: January 15, 2013, 09:18:47 am by superkato » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its sending continuously with a gap of 388msec.

I will try the Serial.readBytes class.

( Damn I wish I had more knowledge smiley-sad  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.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how would I do that function ?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: