Go Down

Topic: Waiting for a byte to received in the Serial buffer before moving on (Read 1 time) previous topic - next topic

jerseyguy1996

I have tried:

Code: [Select]
while(!(Serial.available())){}

but the code seems to be rolling right over that.  There shouldn't be anything in the serial buffer at that point in the code.  On a sort of related note:

Code: [Select]
Serial.read();

should empty a byte from the buffer shouldn't it?
Arduino Uno;
Mega328

Tom Carpenter

if there is nothing in the buffer, Serial.read() returns 0xFF (255).

Code: [Select]
while(!Serial.available());
is indeed correct (what you wrote is equivalent). What it suggests is that there is already something in the buffer. Before that section of code, you could try this:

while(Serial.available()) Serial.read(); //Clear the buffer

That way when you get to your wait there is definately nothing already there.
~Tom~

PeterH


I have tried:

Code: [Select]
while(!(Serial.available())){}

but the code seems to be rolling right over that.  There shouldn't be anything in the serial buffer at that point in the code.


Perhaps there's a discrepancy between what you think 'should' be in the buffer and what's actually in the buffer. The code you posted will loop until something is available. If it exits the loop, you know a byte is available to be read.

It's not a particularly good way top handle serial port input,l but if you want your sketch to wait and do nothing until input arrives on the serial port, that while loop will do it.
I only provide help via the forum - please do not contact me for private consultancy.

jerseyguy1996





It's not a particularly good way top handle serial port input,l but if you want your sketch to wait and do nothing until input arrives on the serial port, that while loop will do it.


What would be a better way to wait for serial data to show up?  The objective is to have two arduino's talk to each other over bluetooth.  To coordinate their activities I want to make sure the master device waits until the slave device has finished executing a command before sending another command.  So it will send a command over bluetooth (by writing to the serial port) and then wait for the slave device to respond with a '1' (not ascii...just a byte) and then it will send the next command.
Arduino Uno;
Mega328

jerseyguy1996


if there is nothing in the buffer, Serial.read() returns 0xFF (255).

Code: [Select]
while(!Serial.available());
is indeed correct (what you wrote is equivalent). What it suggests is that there is already something in the buffer. Before that section of code, you could try this:

while(Serial.available()) Serial.read(); //Clear the buffer

That way when you get to your wait there is definately nothing already there.


I will give that a try.  Thanks!
Arduino Uno;
Mega328

jerseyguy1996

#5
Aug 05, 2012, 02:08 am Last Edit: Aug 05, 2012, 02:10 am by jerseyguy1996 Reason: 1


while(Serial.available()) Serial.read(); //Clear the buffer

That way when you get to your wait there is definately nothing already there.



This worked!  I am trying to figure out how there is anything in the Serial buffer though.  This is the code for the Arduino Master:

Code: [Select]
//uses the iteadstudio bluetooth master/slave shield v 2.2

//http://imall.iteadstudio.com/development-platform/arduino/shields/im120417010.html

void setup() {               
  // initialize the digital pin as an output.
 
  Serial.begin(38400); 
 
 
}


void loop()
{

  for(int i = 0; i < 11; i++)
  {
    //clear the serial buffer
    while(Serial.available()) {Serial.read();}
   
    //send the first command to the bluetooth shield
    Serial.write(i);
   
    //wait for a response from the slave device
    while(!(Serial.available())){}
   
    delay (1000);
  }
}


and this is the code for the arduino slave:

Code: [Select]
//used the iteadstudio bluetooth shield slave v 2.1

//http://imall.iteadstudio.com/development-platform/arduino/shields/im120417006.html

#define led 13


void setup() {
 // initialize serial:
 Serial.begin(38400);
 pinMode(led, OUTPUT);
 // reserve 200 bytes for the inputString:
 //inputString.reserve(50);
}

void loop()
{
 
 //wait for a command
 while(!(Serial.available()));
 
 //read the command
 int i = Serial.read();
 
 //blink the led
 for(int j = 0; j < i; j++)
 {
   digitalWrite(led,HIGH);
   delay(200);
   digitalWrite(led, LOW);
   delay(200);
   
 }
   
   //let the master know that you are ready for another command
   Serial.write(1);
}


Arduino Uno;
Mega328

P18F4550

#6
Aug 05, 2012, 03:03 am Last Edit: Aug 05, 2012, 03:07 am by P18F4550 Reason: 1
Code: [Select]
while(Serial.available()>0)
{
// do an action here

}

waits for one byte, >1 will wait for 2 and so on

Here is what i did to make two arduino's talk, the RX waits for 2 bytes then actions it
http://arduino.cc/forum/index.php/topic,114239.msg859538.html#msg859538

jerseyguy1996


Code: [Select]
while(Serial.available()>0)
{
// do an action here

}

waits for one byte, >1 will wait for 2 and so on

Here is what i did to make two arduino's talk, the RX waits for 2 bytes then actions it
http://arduino.cc/forum/index.php/topic,114239.msg859538.html#msg859538


Thanks for the guidance!  I will give that a try.  I need the system to be fairly robust because it will be controlling all of the systems on my saltwater reef tank.  I need to make sure that the command makes it through and if not it will try again a few times and finally notify me of any failure.
Arduino Uno;
Mega328

P18F4550

my program sends 2 bytes, 1st is pin number, the 2nd byte is state, a 3rd could be sent with a checksum which the RX will then send back to TX to confirm

Go Up