Example doesn't execute properly

It’s probably been reported a couple of dozen times already but I didn’t see one. Example code and probably a dozen examples of code fails to input from the serial port properly. Looks like the program is reading the serial port so fast on three different boards I have tested it with, it gets up to three strings from the follow sketch when I input/send
The quick brown fox

/*
  String length() 
 
 Examples of how to use length() in a String. 
 Open the Serial Monitor and start sending characters to see the results.
 
 created 1 Aug 2010
 by Tom Igoe
 
 http://arduino.cc/en/Tutorial/StringLengthTrim
 
 This example code is in the public domain.
 */
String txtMsg = "";                         // a string for incoming text
int lastStringLength = txtMsg.length();     // previous length of the String

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // send an intro:
  Serial.println("\n\nString  length():");
  Serial.println();
}

void loop() {
  // add any incoming characters to the String:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    txtMsg += inChar;
  } 

  // print the message and a notice if it's changed:
  if (txtMsg.length() != lastStringLength) {
    Serial.println(txtMsg);
    Serial.println(txtMsg.length());
    // if the String's longer than 140 characters, complain:
    if (txtMsg.length() < 140) {
      Serial.println("That's a perfectly acceptable text message");
    } 
    else {
      Serial.println("That's too long for a text message."); 
    }
    // note the length for next time through the loop:
    lastStringLength = txtMsg.length();
  }
}

Somewhere on the net I found a single example that suggested a solution and seems to work fine when inserted in the example. I have to use delay(2) to get it to run right.

/*
  String length() 
 
 Examples of how to use length() in a String. 
 Open the Serial Monitor and start sending characters to see the results.
 
 created 1 Aug 2010
 by Tom Igoe
 
 http://arduino.cc/en/Tutorial/StringLengthTrim
 
 This example code is in the public domain.
 */
String txtMsg = "";                         // a string for incoming text
int lastStringLength = txtMsg.length();     // previous length of the String

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // send an intro:
  Serial.println("\n\nString  length():");
  Serial.println();
}

void loop() {
  // add any incoming characters to the String:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    delay(2);
    txtMsg += inChar;
  } 

  // print the message and a notice if it's changed:
  if (txtMsg.length() != lastStringLength) {
    Serial.println(txtMsg);
    Serial.println(txtMsg.length());
    // if the String's longer than 140 characters, complain:
    if (txtMsg.length() < 140) {
      Serial.println("That's a perfectly acceptable text message");
    } 
    else {
      Serial.println("That's too long for a text message."); 
    }
    // note the length for next time through the loop:
    lastStringLength = txtMsg.length();
  }
}

ricortes:
Example code and probably a dozen examples of code fails to input from the serial port properly.

Works well for me.

Looks like the program is reading the serial port so fast on three different boards I have tested it with, it gets up to three strings from the follow sketch when I input/send
The quick brown fox

The code is designed to work that way.

Somewhere on the net I found a single example that suggested a solution…

A solution is not necessary. The code is working as designed.

I have to use delay(2) to get it to run right.

Using a delay to get serial communications working is never a good idea.

I’m not saying it doesn’t work for most people. I’ve found it doesn’t work for me and at least two others. q.s. to bug.

The code is not the problem, it is pretty standard C. There were a number of other code snippets and techniques I tried that produced the same results.

A solution is not necessary for a bug? I beg to differ. I haven’t characterized it, could be anything from the USB to ttl converter I am using to the Arduino runtime. Don’t know, sort of care, since others are having the same problem. That you can’t reproduce it kind of narrows down the possibilities of what it could be but it doesn’t eliminate the bug. What happens if for instance it is the USB chips that are just finding their way into manufacturing?

We agree using a delay is only a crutch for now. I would rather not have to do it. As far as the existence of the problem, I’ll attempt to attach a screen shot, just the bad output rather then the one with the delay that cures it since you have a working system to see that. If the attachment fails, I’ll upload it to the web and provide a link in a follow up message.

arduinobug.JPG

What part of...

The code is designed to work that way.

...do you not understand?

That is not bad output and it does not demonstrate a hardware or software bug. It may not be what you are expecting, but that is exactly the kind of output that the program, as written, will produce. If you want it to output only one complete line at a time then you'll have to modify the code to accumulate a line up to a linefeed and then output the line, clear the string and start again.

Pete

Kind of frustrating to talk with people w/o a broader range of experience outside of Arduino. Even worse to get thinly veiled insults from the same.

One of the problems is the code is what in practice everywhere outside of Arduino is usually called something like "a hot read." The Arduino serial monitor AFAIK is not capable of a hot read. Another small problem is the a comment refers to text where it should say character.

If you want to see how the code should really run, I would suggest you get a terminal program like Putty.exe and watch how it behaves running the program with the Arduino. I would further suggest the Arduino serial monitor be changed such that it would be compatible with virtually every other terminal program.

Anyone that wants to, I would suggest running the code in both the serial monitor and using Putty. You can simulate a hot read with the monitor by typing one character at a time and hitting the send button. It is nothing like what you will see if try to run the code in the serial monitor. All problems disappear when you switch to Putty and the program runs as expected. If you are having trouble getting you code to work the way you expect, it may not be you.

I can't put it any clearer then people are expecting a hot read/terminal behavior. If you don't understand that, there is nothing I can do for you. If you want to keep the Arduino way of doing things, that is your prerogative.

I am also getting problems when I replace the example code with my arguably faster code. I start picking up extraneous characters that look like an umlaut Y and getting a double increment of the index. I would post a screen shot of that, but I am sure you would just dismiss without looking at it and hurl some uncalled for slights. I think I am done here. Sheesh!

Kind of frustrating to talk with people w/o a broader range of experience outside of Arduino.

One way or another I’ve been programming all sorts of computers for over 45 years. I have lost count of the number of different computers/processors I have used.
If that’s not broad enough for you, what is?

Even worse to get thinly veiled insults from the same.

Yeah, back at ya.

One of the problems is the code is what in practice everywhere outside of Arduino is usually called something like “a hot read.”

Everywhere? References?

I start picking up extraneous characters that look like an umlaut Y

A very well known coding error that numerous newbies (and many non-newbies) have run into. If you use Serial.read() when there’s no character already waiting in the buffer, then it will return -1. If you print that to the serial monitor as a character, you get the umlaut Y.

I think I am done here.

Agreed. There’s no point continuing unless you are willing to get off your high horse and accept that perhaps you don’t know everything, especially when it comes to how to code for the Arduino.

Pete

@ricortes, try the examples in serial input basics.

...R

ricortes: Kind of frustrating to talk with people w/o a broader range of experience outside of Arduino. Even worse to get thinly veiled insults from the same.

One of the problems is the code is what in practice everywhere outside of Arduino is usually called something like "a hot read." The Arduino serial monitor AFAIK is not capable of a hot read.

This is exactly the sort of thing I enjoy on a Sunday evening. Complete garbage.

What makes you think we haven't experience outside Arduino?

Look, buddy, I've been programming for, like, 40 years and I have never heard of a "hot read" before. I Googled it in case it was some new, hip, term, but basically got a lot of results about "hot newsreaders". Even trying to narrow it down didn't help.

Serial input is asynchronous. It doesn't come "in a batch" (hot or not). Depending on timing, what you observed is exactly what you expect. Changing programs may change the timing. You need to look for a delimiter, like a newline.

This is basic stuff.

Please provide a link to this "hot read" concept of which you are so keen.

[quote author=Nick Gammon link=msg=2215232 date=1430640972] I've been programming for, like, 40 years [/quote]

I was wrong, sorry.

45 years.

And no, it wasn't all on the Arduino.