Go Down

Topic: How to read an array of bytes through Serial.read( (Read 15275 times) previous topic - next topic

karan gampa

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.

MarkT

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: [Select]

 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.
[ I won't respond to messages, use the forum please ]

MarkT

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.
[ I won't respond to messages, use the forum please ]

karan gampa

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

Groove

Code: [Select]
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.
Per Arduino ad Astra

Joghurt

#5
Mar 16, 2010, 04:38 pm Last Edit: Mar 16, 2010, 04:39 pm by Joghurt Reason: 1
Why make it so complicated?
How about:

Code: [Select]

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.

RoyK

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: [Select]

#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.
}

Joghurt

#7
Mar 17, 2010, 09:14 am Last Edit: Mar 17, 2010, 09:14 am by Joghurt Reason: 1
Don't forget to flush the buffer on error. ;)

Go Up