Pages: [1]   Go Down
Author Topic: Does Serial.print affects serial recieve buffer?  (Read 693 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if available() gets the number of bytes arrived and stored in the serial recieve buffer, how come the result of the code bellow is just printing.
(when i lunch the program, the result on the serial monitor is: printprintprintprintprint....)

Quote
char inByte = 0;

void setup (){
  Serial.begin(9600);
}

void loop ()
{
   Serial.print("print");
   while (Serial.available()>0){
     inByte = Serial.read();
     Serial.print(inByte);
   }
}


What should i do program to stop after serial.print("print")
(print and then wait till you recieve at least 1 byte)

thank you very much
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are sending nothing to the serial port, the while loop has no affect, so your program is equivalent to:
Code:
void setup (){
  Serial.begin(9600);
}

void loop ()
{
   Serial.print("print");
}
Which should make it pretty obvious why it keeps printing "print" over and over.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are sending nothing to the serial port, the while loop has no affect, so your program is equivalent to:
Code:
void setup (){
  Serial.begin(9600);
}

void loop ()
{
   Serial.print("print");
}
Which should make it pretty obvious why it keeps printing "print" over and over.

Do you maybe know, what should i do program to stop after print() and wait for some incoming byte from serial monitor and then go on?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you maybe know, what should i do program to stop after print() and wait for some incoming byte from serial monitor and then go on?

change your while statement.

Edit:  Incorrect code removed.  Thanks Paul
« Last Edit: April 07, 2012, 08:10:47 pm by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Edison Member
*
Karma: 29
Posts: 2361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if available() gets the number of bytes arrived and stored in the serial recieve buffer, how come the result of the code bellow is just printing.
(when i lunch the program, the result on the serial monitor is: printprintprintprintprint....)

Quote
char inByte = 0;

void setup (){
  Serial.begin(9600);
}

void loop ()
{
   Serial.print("print");
   while (Serial.available()>0){
     inByte = Serial.read();
     Serial.print(inByte);
   }
}


What should i do program to stop after serial.print("print")
(print and then wait till you recieve at least 1 byte)

thank you very much


 char inByte;
 
if (Serial.available())

     inByte = Serial.read();
     Serial.print(inByte);   
}

///if there's nothing there, it will loop and start again, it does not 'wait' it simply gives yes/no answer and then you get the data.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tnx, but

void loop(){
    Serial.print("text");
    if (Serial.available()){
         inByte = Serial.read();
         Serial.print(inByte);
    }
}

also prints "text" over and over again

« Last Edit: April 07, 2012, 10:43:02 am by difenbahij » Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char inByte = 0;

void setup (){
  Serial.begin(9600);
}

void loop ()
{
   Serial.print("print ");
   while (Serial.available()==0)
      ;  // Do nothing
     inByte = Serial.read();
     Serial.println(inByte);
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   while (Serial.available()<=0){  // waits until a byte is sent
     inByte = Serial.read();
     Serial.print(inByte);
   }
Come on, James. You know better than that. While there is nothing to read, read it?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Come on, James. You know better than that. While there is nothing to read, read it?
Well gee, when you put it that way, the whole loop looks kinda silly, doesn't it?  smiley-wink
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pages: [1]   Go Up
Jump to: