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;
}
}
}
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?