Pages: [1]   Go Down
Author Topic: communication example - Reading a serial ASCII-encoded string does not work  (Read 1733 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Quote
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.

Quote
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.

Quote
Can you mix the two forms of working?
Can YOU post some code that you think illustrates the problem?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8940
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: