Go Down

Topic: Does Serial.print affects serial recieve buffer? (Read 799 times) previous topic - next topic

difenbahij

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

PaulS

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

void loop ()
{
   Serial.print("print");
}

Which should make it pretty obvious why it keeps printing "print" over and over.

difenbahij


If you are sending nothing to the serial port, the while loop has no affect, so your program is equivalent to:
Code: [Select]
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?

James C4S

#3
Apr 07, 2012, 05:23 pm Last Edit: Apr 08, 2012, 03:10 am by James C4S Reason: 1

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
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

cjdelphi


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.

difenbahij

#5
Apr 07, 2012, 05:38 pm Last Edit: Apr 07, 2012, 05:43 pm by difenbahij Reason: 1
tnx, but

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

also prints "text" over and over again


wildbill

Code: [Select]

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


PaulS

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

James C4S


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?  ;)
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up