Go Down

Topic: Serial.available() vs. Serial.print() (Read 14 times) previous topic - next topic

Frogeraie

I have two boards communicating serially through Xbees. On board #1 I have the following code:
Code: [Select]
int inByte = 0;         // incoming serial byte
int outputPin = 13;
void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 pinMode(outputPin, OUTPUT);
 }

void loop()
{
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   if (inByte == 'E') {
   digitalWrite(outputPin, HIGH);
   }else if (inByte == 'F') {
   digitalWrite(outputPin, LOW);
   }
  }else{
  Serial.print('H');
 delay(1000);
 Serial.print('L');
 delay(1000);            
 }
}

On board #2 I have the following code:
Code: [Select]
int inByte = 0;         // incoming serial byte
int outputPin = 13;
void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 pinMode(outputPin, OUTPUT);
 }

void loop()
{
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   if (inByte == 'H') {
   digitalWrite(outputPin, HIGH);
   }else if (inByte == 'L') {
   digitalWrite(outputPin, LOW);
   }
  }else{
  Serial.print('E');
 delay(2000);
 Serial.print('F');
 delay(2000);            
 }
}

The leds on pin 13 are blinking at the right frequency when delays are short, 100 - 200 msec. For values around 1 sec, as in the case above, only one led blinks - #1 - at about a 2000 msec pulse, #2 is constantly lit.
Maybe my aproach is not the best one to have a board both receiving and sending serial data?
Any suggestions?
Thanks!

PaulS

#1
Jan 23, 2011, 03:10 pm Last Edit: Jan 23, 2011, 03:12 pm by PaulS Reason: 1
Serial data transmission is relatively slow, particularly at 9600 baud. Since the two Arduinos are directly connected, they can communicate MUCH faster than that. Try increasing the baud rate, to the maximum supported speed, 115200. That's 12 times as fast, so will take 1/12th as long.

Also, one of your Arduinos is waiting half as long as the other. The faster one will eventually overflow the serial buffer on the slower one.

Frogeraie

Don't you think it could help to introduce a Serial.flush() somewhere to prevent the overflowing of the serial buffer on the slower Arduino?
I will try to increase the speed.
Thanks.

PaulS

Quote
Don't you think it could help to introduce a Serial.flush() somewhere to prevent the overflowing of the serial buffer on the slower Arduino?

No, I don't. I figure that is the sender thinks the data is important enough to send that it is important enough to receive and process. Whatever needs to be done to ensure that that can happen should be done.

If the receiver can not be made fast enough, the sender and receiver need to implement handshaking, so that the sender doesn't send data until the receiver is ready to process it.

If that can't happen, there is no benefit to calling flush() to dump random amounts of data as opposed to dumping random amounts of data because the receive buffer is full.

I suspect that this is more than just theoretical work on your part. If you fill us in on what that larger picture looks like, perhaps we could be more helpful.

Frogeraie

I set the speed on both Xbees and Arduinos to 115200, there was no change: I could see on the serial monitors (I have the boards hooked up to two different PCs) both sequences of EFEFEF and HLHLHL generated at apparently the right frequencies but only the led on board #1 pulses. If I change the delays on board #2 from 2000 to 500, then it is the led #2 which is blinking. This confirms what you said before:
Quote
The faster one will eventually overflow the serial buffer on the slower one.
.
Now my question is: how to manage not to get this buffer to be overflown?

Go Up