Pages: [1]   Go Down
Author Topic: Waiting for a byte to received in the Serial buffer before moving on  (Read 952 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried:

Code:
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:
Serial.read();

should empty a byte from the buffer shouldn't it?
Logged

Arduino Uno;
Mega328

Leeds, UK
Offline Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

~Tom~

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried:

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




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

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!
Logged

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
//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:
//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);
}


« Last Edit: August 04, 2012, 07:10:23 pm by jerseyguy1996 » Logged

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
« Last Edit: August 04, 2012, 08:07:57 pm by P18F4550 » Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: