String var never having char appended?

Hi all. Working on a system in the early stages but I need to be able to send commands via the Serial prompt and have the unit echo the command and perform the relevant command after that. Simple enough for now, but I have a weird issue with the echo part. It seems to be whenever it receives a new character, it erases and replaces the contents of the string with the new character. I seem to rememeber this method working once.

If I add Serial.print(inStr); anywhere in the while, I get a result like:

testingRS232:

But I never get any output after “RS232:”, and I assume it replaces the contents of inStr with a ‘\n’ char, even if I tell it not to add the ‘/n’ character. Am I missing something? I’ve google for a while now and tried various suggestions but nothing that is simple enough or even works.

Using Arduino IDE 1.6.7 on 64-bit Windows 10

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()) serRec();
}

void serRec() {
  String inStr = "";
  while (Serial.available()) {
    char inChar = Serial.read();
    if (inChar == '\n') {
      Serial.print("RS232: ");
      Serial.println(inStr);
    } else {
      inStr += inChar;
    }
  }
}

Every time you call SerRec, you reset inStr to empty.
is that what you want to do?

AWOL:
Every time you call SerRec, you reset inStr to empty.
is that what you want to do?

Yeah. serRec (serialReceive) is only using variables it needs when a command is issued. The data isn't used anywhere else.

EDIT: facepalm I see what you meant. I take it if I want to keep it on that section till the message is received, I have to keep running it until I see '\n'?

So why are you asking the question?
Every time you receive a character, you set inStr to empty.
If the character you receive is a newline, you print inStr, which, as we've established, is empty.
What's the problem?

I saw what you meant before. I forgot that Serial is slower than the arduino :blush:

  1. String buffer is 1 character long
  2. You adding char to String - may work but...
  3. You final print could / should be after the Serial buffer is empty AKA Serial not available.
  4. Add few Serial prints to verify all of the above.
  5. Use pointer to store characters into String , as in generic name for data, not char.
    Jim

[quote author=Vaclavjulyjim link=msg=2971362 date=1477177880]

  1. String buffer is 1 character long
  2. You adding char to String - may work but...
    ...
  3. Use pointer to store characters into String , as in generic name for data, not char.
    Jim
    [/quote] Utter nonsense, verging on gibberish.
    Ignore

Better to Remain Silent and Be Thought a Fool than to Speak and Remove All Doubt

True dat.