Serial.getline()

I checked Serial for a method similar to getline to input a string not just a char. As I see it’s not implemented. I wrote a function but the only issue is that it cannot detect ENTER key (I’m using ’ ’ instead). Any ideas?

String serialInput() {
String inputWord = "";
char inputChar='\0';

Serial.println("");
Serial.print("INPUT?");
do {
    while (!Serial.available()) {
        Particle.process();
    }
    Serial.print(inputChar);
    inputWord += inputChar;
    inputChar = Serial.read();
} while (inputChar!=' ');
Serial.println("");
return inputWord;
}

just off the top of my head… the enter key will either give 0x0A or 0x0D thats new line and carriage return respectively. depends on the terminal you use. i think some (arduino) give both. so check for one or the other and than check again for the other just in case it stays in the buffer and makes your loop exit straight away on the next run through. i think peek is the best option to check without removing from the buffer…

dont forget to terminate your strings too

You could also try Serial.readString(), Serial.readStringUntil() or Serial.readByte(), Serial.readByteUntil() :wink:

These are methods of the Stream class from which Serial is derived.

You might want to call Serial.setTimeout() if the user may be slow (>1sec) in responding :sunglasses:, since timed read closes your input window after 1000ms by default.