communication example - Reading a serial ASCII-encoded string does not work

I have been asked to start a new thread here rather than on GITHUB

The above example has the following line

if (Serial.read() == '\n')
This does not work no matter which option for line termination is chosen at the bottom of the serial monitor.
The different options give:

No line ending sends NO char (as you would expect)
Newline sends 10 only (as you would expect)
Carriage return sends 13 only (as you would expect)
NL and CR sends 13 only (as you would NOT expect)

If you replace Serial.read() with char(Serial.read()) or int(Serial.read())
then Newline works.

HOWEVER the same statement DOES work in the Serial Event example.

There are two things that are different between the two instances

1 The if statement is in the serialEvent() function which does not seem a likely cause.

2 In the first program the test follows Serial.parseInt()

Can you mix the two forms of working?

Alan

NL and CR sends 13 only (as you would NOT expect)

Not for me, it doesn't. I get 10 and 13 (carriage return and line feed), as expected.

If you replace Serial.read() with char(Serial.read()) or int(Serial.read())
then Newline works.

Those abominations are provided for people that don't understand casts, and really should not be needed.

Can you mix the two forms of working?

Can YOU post some code that you think illustrates the problem?

Serial.parseInt() will throw away any characters that are not '0'-'9' or '-' until it hits a digit or a timeout. That includes a '\n'. If the parseInt returns a value of 0 it may indicate that no digits were found and it may have gobbled up the line ending.