Example doesn't execute properly


it's been reported couple of dozen times didn't see one. example code , dozen examples of code fails input serial port properly. looks program reading serial port fast on 3 different boards have tested with, gets 3 strings follow sketch when input/send
the quick brown fox
code: [select]
/*
  string length()
 
 examples of how use length() in string.
 open serial monitor , start sending characters see results.
 
 created 1 aug 2010
 by tom igoe
 
 http://arduino.cc/en/tutorial/stringlengthtrim
 
 this example code in public domain.
 */
string txtmsg = "";                         // string incoming text
int laststringlength = txtmsg.length();     // previous length of string

void setup() {
  // open serial communications , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }

  // send intro:
  serial.println("\n\nstring  length():");
  serial.println();
}

void loop() {
  // add incoming characters string:
  while (serial.available() > 0) {
    char inchar = serial.read();
    txtmsg += inchar;
  }

  // print message , notice if it's changed:
  if (txtmsg.length() != laststringlength) {
    serial.println(txtmsg);
    serial.println(txtmsg.length());
    // if string's longer 140 characters, complain:
    if (txtmsg.length() < 140) {
      serial.println("that's acceptable text message");
    }
    else {
      serial.println("that's long text message.");
    }
    // note length next time through loop:
    laststringlength = txtmsg.length();
  }
}


somewhere on net found single example suggested solution , seems work fine when inserted in example. have use delay(2) run right.

code: [select]
/*
  string length()
 
 examples of how use length() in string.
 open serial monitor , start sending characters see results.
 
 created 1 aug 2010
 by tom igoe
 
 http://arduino.cc/en/tutorial/stringlengthtrim
 
 this example code in public domain.
 */
string txtmsg = "";                         // string incoming text
int laststringlength = txtmsg.length();     // previous length of string

void setup() {
  // open serial communications , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }

  // send intro:
  serial.println("\n\nstring  length():");
  serial.println();
}

void loop() {
  // add incoming characters string:
  while (serial.available() > 0) {
    char inchar = serial.read();
    delay(2);
    txtmsg += inchar;
  }

  // print message , notice if it's changed:
  if (txtmsg.length() != laststringlength) {
    serial.println(txtmsg);
    serial.println(txtmsg.length());
    // if string's longer 140 characters, complain:
    if (txtmsg.length() < 140) {
      serial.println("that's acceptable text message");
    }
    else {
      serial.println("that's long text message.");
    }
    // note length next time through loop:
    laststringlength = txtmsg.length();
  }
}

example code , dozen examples of code fails input serial port properly.
works me.

quote
looks program reading serial port fast on 3 different boards have tested with, gets 3 strings follow sketch when input/send
the quick brown fox
the code designed work way.

quote
somewhere on net found single example suggested solution...
a solution not necessary.  code working designed.

quote
i have use delay(2) run right.
using delay serial communications working never idea.



Arduino Forum > Using Arduino > Programming Questions > Example doesn't execute properly


arduino

Comments