Serial Monitor Input

I’ve been horsing around with the Serial Monitor and had some questions regarding inputting data from it. So I wrote the following program.

char Arr[20];
byte n;
byte m;

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

void loop() {
  while (Serial.available() == 0) {} //Wait for user to enter something
  delay(1);                         //n = 0 if this is not there
  n = Serial.available(); 

  m = Serial.readBytesUntil('/n', Arr, 20);  //fill array
  Arr[m] = '/0';

  for (int j = 0; j <= 19; j++) { // dispaly array contents
    Serial.print ("Array ");
    Serial.print (j);
    Serial.print (" is ");
    Serial.println (Arr[j]);
  }
}

// The Serial Monitor will send the users characters, plus a CRLF and then a Zero.

Seems that input from the Serial Monitor will show the Users Characters followed by a CRLF and then a Zero. Is this correct?

AND… I need a delay in order to obtain the correct number of characters from Serial.available.
Thanks Mike

Seems that input from the Serial Monitor will show the Users Characters followed by a CRLF and then a Zero. Is this correct?

What have you got the Line ending set to ? As to the zero, you are putting it into the array so no wonder it is there

I need a delay in order to obtain the correct number of characters from Serial.available.

Use an if instead of a while. Keep whizzing round loop() until you get the "end of input" character such as CR or LF and you don't need a delay(). Just collect each character as it arrives very slowly

I'm still rather new to Arduino. I did not realize there is a setting for line ending. Where would one find it? I'll try an If statement and see what happens. Do you have any reason for the difference? Thanks Mike

The serial input basics tutorial may have information of interest.

Line endings in serial monitor.
line ending.jpg

line ending.jpg

Thanks, I see that this can remove the CR, but where is the ending zero coming from? I tried using an IF statement rather than the WHILE. The IF seems to grab characters one by one, where as the WHILE waits for the entire string. Is that correct? Thanks Mike

Arr[m] = '\0';

Do you have any reason for the difference?

Yes. It does not waste time when the Arduino may be required to do something else. While loops, by their nature block operation of other code

where is the ending zero coming from?

 Arr[m] = '/0';

You can't simply replace the while with an if

Take a look at this Serial input basics - updated

Let us do some experiments to get the answers of your queries.

1. This is the layout (Fig-1) of Serial Monitor (SM) of Arduino IDE.


Figure-1:

2. You select 'Newline (LF)' option in the 'Line ending tab', enter C in the InputBox of SM and then click on the Send button. As a result, 0x43 (ASCII Code of character C) and 0x0A (ASCII Code of Newline character) will be travelling towards Arduino which we can catch and send them back to SM for validation.
Sketch:

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

void loop() 
{
  byte n = Serial.available();
  if (n != 0)
  {
    char x = Serial.read();
    Serial.print(x, HEX);  //shows: 43 and A (leading 0 is missing)
  }
}

3. Repeat Step-2 with option 'Carriage return (CR)' in the 'Line ending tab'.
The SM should show: 43 and A (leading 0 is missing)

4. Repeat Step-2 with option 'Both NL & CD' in the 'Line ending tab'.
The Sm shows: 43, 0D, and 0A.

5. Repeat Step-2 with option 'No line ending' in the 'Line ending tab'.
The Sm shows: 43

6. What codes/non-printable character is involved with this: \n.
It is the 'Newline/LF' character

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

void loop() 
{
  Serial.println();
  Serial.print('\n', HEX); //shows: 0A; Newline charcater (LF)
  delay(2000);
}

7. Working principle of:

byte m = Serial.readBytesUntil('\n', Arr, 5);

(1) You select 'Newline' option in the 'Line ending tab', enter ABCDE in the InputBox of SM and then click on the Send button. ABCDE will be stored in the array named Arr; code for \n will not be stored in Arr. m is equal to 5 indicating number of charcaters received. The function will terminate as 5 charcaters have been received; it will not wait to check if \n has arrived or not.

(2) You select 'Newline' option in the 'Line ending tab', enter ABC in the InputBox of SM and then click on the Send button. ABC will be stored in the array named Arr. m will be equal to 3 indicating the number of charcaters received. As declared amount (5) charcaters have not arrived/received, the function will wait until it finds the terminating character \n; once \n is detected, the function will terminate. The code for \n will not be stored in Arr.

(3) If you don't enter any character in the InputBox within a 1-sec (the default timeout period), the function will automatically terminate with m = 0.

Thanks, the ‘/0’ was left over from something else and I missed it.

So the IF is like an interrupt input, whereas the WHILE is like a polled input?

Thanks Mike.

So the IF is like an interrupt input, whereas the WHILE is like a polled input?

Both are implemented using polling but using if allows you to do other things in the gaps between characters being received whereas while blocks code operation unless you put code into the while loop that has nothing to do with it

I was making a comparison to something I understand. I suppose I should have said similar or like. Thanks, I think I have an understand of this piece. Mike