Pages: [1]   Go Down
Author Topic: How to read an array of bytes through Serial.read(  (Read 13272 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
  
   I am new to arduino.I am trying to communicate two arduino modules using Xbee modules. From one arduino am sending an array of 9 bytes, wih syntax:

Serial.write(RFout,9);  //RFout is an array with 9 bytes


On the other hand, the other arduino trying to recieve this information by using the below syntax:


int availableBytes = Serial.available();
delay(2000);
if (Serial.available() > 0) {
  
   for(int n=0; n<availableBytes; n++);
  {
    RFin_bytes[n] = Serial.read();
  }


But, it seems to be not working.Could you people help me out in solving this problem.
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12175
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doesn't available() just return the number of bytes available now, which might only be 1.  You need an outer loop to keep trying to read till all the 9 bytes have been acquired?  Something like:
Code:
 int bytes_read = 0 ;
  while (bytes_read < 9)
  {
     if (Serial.available() > 0)
     {
        ...... read a byte and store it ....
        bytes_read ++;
     }
  }

Or you could wait for available() to return at least 9 before reading all 9 - the hardware buffer is 128 bytes.
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12175
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've just noticed your code is buggy anyway as you assume the two calls to available() return the same value, which is not guaranteed.
Logged

[ I won't respond to messages, use the forum please ]

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

Sorry, i didn't understand the second reply. So could you please help me in storing the 9 bytes of incoming string.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int availableBytes = Serial.available();
delay(2000);

Two seconds is a very long time to wait.
If your serial line is running constantly at 9600 baud, the serial receiver buffer would overflow in less than a fifth of a second.
Logged

Per Arduino ad Astra

Weinsberg, Germany
Offline Offline
God Member
*****
Karma: 3
Posts: 773
A Coder's Tale
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why make it so complicated?
How about:

Code:
while (Serial.available()<9) {} // Wait 'till there are 9 Bytes waiting
for(int n=0; n<9; n++)
   RFin_bytes[n] = Serial.read(); // Then: Get them.
« Last Edit: March 16, 2010, 10:39:55 am by Joghurt » Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One thing always to keep in mind when writing this kind of code is to be able to handle things when something goes wrong -- like what if you never get the 9 bytes you are expecting? I doubt you would want your code to lock up.

I'd probably do something like this:
Code:
#define MAX_MILLIS_TO_WAIT 1000  //or whatever
unsigned long starttime;

starttime = millis();

while ( (Serial.available()<9) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) )
{      
      // hang in this loop until we either get 9 bytes of data or 1 second
      // has gone by
}
if(Serial.available() < 9)
{
            // the data didn't come in - handle that problem here
      Serial.println("ERROR - Didn't get 9 bytes of data!");
}
else
{
      for(int n=0; n<9; n++)
         RFin_bytes[n] = Serial.read(); // Then: Get them.
}
Logged

Weinsberg, Germany
Offline Offline
God Member
*****
Karma: 3
Posts: 773
A Coder's Tale
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't forget to flush the buffer on error. smiley-wink
« Last Edit: March 17, 2010, 03:14:20 am by Joghurt » Logged

Pages: [1]   Go Up
Jump to: