Serial.print Incomplete String?

Hello, i’m new to Arduino, thank you for create this, it is fun and easy to control the hardware.

now i face the problem that serial.print Incomplete String? my board is Arduino Nano (unofficial), my code is below:

String input_string = "";
bool stringComplete = false;

void setup(){
  Serial.begin(9600); //Set the Baud rate
  input_string.reserve(200);
}

void loop(){

  if (stringComplete) {

    Serial.print("input_string: ");
    Serial.println(input_string);
    input_string = "";
    stringComplete = false;
   
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the input_string:
    if (inChar != '.') {
      input_string += inChar;
    }
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '.') {
      stringComplete = true;
    }
  }
}

this is based on ‘sample’, ‘communication’, ‘SerialEvent’.

on Serial Monitor, then i input ‘a,b,c,d.’, sometimes the arduino print

09:05:31.175 -> input_string: a,b,c,d ← right

but sometimes the arduino print:

09:05:31.175 -> input_string: b,c,d ← Incomplete, this is easy to happen when i open the serial monitor and input ‘a,b,c,d.’ for FIRST TIME.

i don’t know why, any comment is welcome, thanks.

I usually add a count down delay on start to my sketches

  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.println(i);  delay(500);
  }

Then enter data after that. Some boards start as soon as the upload finishes, other restart on the monitor opening.

Also check out my Arduino Software Solutions
for a whole range of solutions to reading text from Serial with the pros and cons of each.
Includes non-blocking reads and non-blocking flushing examples

You’re typing characters before the Arduino has finished restarting when the serial monitor opens.

Practically speaking, most Arduino boards will restart when the serial monitor opens independently of a program upload. This is certainly true for any ATmega328 based boards like the Uno and Nano.

If the board has a dedicated usb to serial bridge chip, like the 16U2 of the Uno/Mega or the CH340 used in most clones, the board will be reset on serial monitor open. The native usb chips like the Leonardo and the newer ARM processors will not reset.

Additional discussion here:

https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/

And here:

https://arduino.stackexchange.com/questions/439/why-does-starting-the-serial-monitor-restart-the-sketch

@weigangjiang , your topic has been moved to a section of the forum where it will get a better exposure.

the countdown looks does not has relation with problem, but it’s amazing and problem fixed. i use

 Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.println(" ");
  }

then in receive part, use
trim(return_string)
to get correct string. thank you!

thank you for explain this, very clear.

thank you for your good intention.

Note - using "String"s (capital “S”) on Arduinox - or indeed any processor with similarly limited memory - is extremely unwise and will frequently lead to puzzling crashes after operating for some time as String “debris” accumulates and chokes the RAM.

Unfortunately the rather unhelpful “tutorials” on the Arduino site include “String” functions without the warning that they are generally not usable. :roll_eyes:

OK, some will say they are usable, but there are a set of rules you would need to follow with extreme care. Your code which progressively adds to a String, fails the rule.

No need to use ‘extreme’ care when using a few small Strings on small memory Uno/Mega,

String (with capital S) is very ‘safe’ on small memory processors like Uno and Mega, because on those processors the malloc() always keeps 128 bytes available for stack use.
So if / when you run out of memory for String use, your program does not ‘crash’. You just don’t get the extra text in the String you were expecting.
Because the program does not crash you can easily add debug prints to see what is happening.

See my tutorial on Taming Arduino Strings
See this example ‘out-of-memory’ sketch

It is the low level c-string methods and char[.] ‘alternatives’ to Strings that need to be programmed with ‘extreme’ care to avoid buffer overflows, off-by-one indexing, missing terminators, etc.
If you don’t use ‘extreme’ care with those methods your sketch just crashes, which makes debugging very difficult.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.