Receive data with Xbee

Hi all,

I have a problem with my program.

My project have multiple transmitters and one receiver. The transmitters sends data that are detect with a accelerometer. I used Zigbee for this communcation.

The code for sending this values:

Serial.write(payload.id);

Serial.write(payload.teller[0]);
Serial.write(payload.teller[1]);

Serial.write(payload.accxg[0]);
Serial.write(payload.accxg[1]);

Serial.write(payload.accyg[0]);
Serial.write(payload.accyg[1]);

Serial.write(payload.acczg[0]);
Serial.write(payload.acczg[1]);

When I receive now this data then have I this code but this don't working well:

payload.id = Serial.read();
delay(1);

payload.teller[0] = Serial.read();
delay(1);
payload.teller[1] = Serial.read();
delay(1);

payload.accxg[0] = Serial.read();
delay(1);
payload.accxg[1] = Serial.read();
delay(1);

payload.accyg[0] = Serial.read();
delay(1);
payload.accyg[1] = Serial.read();
delay(1);

payload.acczg[0] = Serial.read();
delay(1);
payload.acczg[1] = Serial.read();
delay(1);

Is there a better/other way for receiving this data.

And a big problem is that this modules must received within 10ms.

Sorry for my english, its not so good...

Thanks for help

Is there a better/other way for receiving this data.

Yes. Using delay() in the hope that serial data will have arrived by then will NOT work.

Either do nothing until all 9 bytes have arrived, or keep track of how many bytes have been available() and have been read(), storing each byte in the appropriate place.

Thanks for help,

I make now an array for place the received data in a variable.
this is now the code:

if ( Serial.available() > 0) {

i = 0;

for(i=0; i<15; i++){
result = Serial.read();

  • delay(1);*

  • }*

  • Serial.println(result[0]); *
    }
    }

But how can I know wich data I just received at the right time?
*So I can place the right data at the right place. *
Thanks

But how can I know wich data I just received at the right time?
So I can place the right data at the right place.

First, if there is at least one byte of data available, it is NOT OK to read all 15 of them.

What constitutes a packet?

When are you planning to read the stickies at the top of the forum? (So you learn how to post code correctly.)

Ok,

  • First I send a ID value that gives an indication wich module sends.
  • Then I send a counter.
  • The last 3 values are the results from the accelerometer.

Because the counter value and the results of the accelerometer are greater then one byte. I split them in high byte and low byte. This can you find in this code:

        payload.id = id; //ID
        
        payload.teller[0] = highByte(teller);  //High Byte counter
        payload.teller[1] = lowByte(teller);   //Low Byte counter

        payload.accxg[0] = highByte(accxg); //High Byte X accelerometer
        payload.accxg[1] = lowByte(accxg);  //Low Byte X accelerometer

        payload.accyg[0] = highByte(accyg); //High Byte Y accelerometer
        payload.accyg[1] = lowByte(accyg);  //Low Byte Y accelerometer
        
        payload.acczg[0] = highByte(acczg); //High Byte Z accelerometer
        payload.acczg[1] = lowByte(acczg);  //Low Byte Z accelerometer

So I send some values/results in 2 packets of 1 byte.

How can I receive this values in right order?

Thanks!

So I send some values/results in 2 packets of 1 byte.

Nonsense. Your payload object (struct ?) contains at least 9 bytes.

Determining where a packet that consists entirely of binary data starts and ends is extremely difficult. The only really successful way involves sending more than twice as much data as the packet contains.

It is FAR simpler to send ASCII data, with known start and end markers that do not appear in the data. Since the data consists entirely of the characters '0' through '9', the minus sign, '-', and the decimal point, '.', it is easy to choose start and end markers that are not in that set.

Sending "<12, 453, 12.567, 13.776, -45.78>", makes it extremely simple to determine where a packet starts and end, and where the tokens in the packet start and end. Each token can be converted to a value using atof() or atoi(), depending on the position of the token in the packet.

How can I receive this values in right order?

Give up on the binary data concept.

No sorry, I used the array's as payload.accyg[0]... just as variables.

I have tried before for sending ASCII data. But this is not so fast for sending data after 50ms or is this not true?

If this is possible, can you me explain how you did it?

Thanks for response!!

But this is not so fast for sending data

Sending ASCII data means sending more data than sending binary data without sync markers. How much slower the process is depends on how you are sending the data. Baud rate, how many sync markers, etc.