Need help finding error using the serial monitor

This routine should print out what ever is sent using the serial monitor. i.e. if you enter “abc”
you should see “abc” printed but it doesn’t happen. I guess I do not undersand the serial monitor.

Also, isn’t there a better way to show code than what I did below?

Thanks

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

void loop() {
int lng;
char sendbuffer[50];
int sendlength;

if (Serial.available() > 0) { // if there is something in the send buffer, then get it
lng = 0;
if (Serial.available() > 0) {
while (Serial.available() > 0) {
sendbuffer[lng++] = Serial.read(); //get a character and put in buffer
sendlength = lng;
}
sendbuffer[lng - 1] = 0; // put in a string terminator of 0
}
Serial.read(); // clear the serial monitor just in case extra characters.
Serial.read(); // same as above
Serial.print("sendbuffer → ");
Serial.println(sendbuffer);
}
}

are you ever hitting the serial monitor's "send" button?

barryjo: Also, isn't there a better way to show code than what I did below?

You want to echo back all received characters back to the serial monitor?

Try this:

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

void loop() 
{
  if (Serial.available()) Serial.write(Serial.read());
}

If you create a loop function which runs several tenthousand times per second, there will be rare cases when you have one(!) character available in the serial receive buffer. And in most cases the receive buffer is simply empty.

There will NEVER be any "extra character" in the input buffer.

The time needed to fill one single character into the input buffer is much longer (hundreds of times longer) than the time needed to execute the code in the loop function.

I found a fix but do not like it. If I put a "delay(1);" statement as shown below.

I do not know why this is needed

if (Serial.available() > 0) { while (Serial.available() > 0) { sendbuffer[lng++] = Serial.read(); //get a character and put in buffer sendlength = lng; delay(1); // I added this and the routine works.

} sendbuffer[lng - 1] = 0; // put in a string terminator of 0 } S

barryjo: I found a fix but do not like it. If I put a "delay(1);" statement as shown below.

I do not know why this is needed

if (Serial.available() > 0) { while (Serial.available() > 0) { sendbuffer[lng++] = Serial.read(); //get a character and put in buffer sendlength = lng; delay(1); // I added this and the routine works.

} sendbuffer[lng - 1] = 0; // put in a string terminator of 0 } S

This makes your code receive more than one character at a time, after the first character is available in the input buffer and your input buffer processing begins.

At a baud rate (bit rate, BAUD) of 9800 you can transfer 980 bytes per second, that is (roughly) one byte per millisecond.

At a baud rate of 115200 you can transfer 11520 bytes (characters) per second. This is (roughly) eleven and a half byte in one single millisecond.

Sowhen you wait one millisecond after the Serial input buffer is not empty and contains one byte, this delay(1) provides some time so that you can receive another 11 bytes in the input bufer ), so that you can have a maximum of 12 characters in the input buffer before your code starts reading out and processing all of them running once through the code in the loop.

If your code runs faster, you will only process either zero (in most cases) or one (rare cases) character from the serial input buffer, because your processing is so fast that you never will have more than maximum one character in the input buffer. Instead the input buffer is empty almost every time you process the incoming bytes from the (empty) input buffer.

That's the case with the example code I sent yesterday: It processes one character at a time, every time when the input buffer is available (not empty).

It took me a while to understand what you were saying but I finally got it. Basically you are saying that at low baud rates, the code will see that Serial.available() is >0 and then perhaps take one character and then process it before the next character has arrived. This condition is worse at lower baud rates than at high baud rates.

It might be better at low baud rates to read from the buffer until you see the end of line character sort of like below.. Hmmm, I wonder if there is a better way.

If (Serial.available>0){

d = Serial.read();

while d != '\r') {

if (d != -1) sendbuffer[i++] = d; // need this to filter Serial.read() when there is no character ready }

}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

When you are writing a program to receive serial data your program must be written recognizing that it is not in control of the process. It can only deal with whatever is sent to it at the time chosen by the sender.

...R