Hi
I'm new here and my english is horrible
I just want know how you can read the Serial.read() as a String
I would be very happy if someone has a example.
Thank you very much!
Hi
I'm new here and my english is horrible
I just want know how you can read the Serial.read() as a String
I would be very happy if someone has a example.
Thank you very much!
Hello and welcome
Here how to read as a string (sorry I don't use Strings) Read serial input startswith - #9 by guix - Programming Questions - Arduino Forum
ChuckNorris95:
HiI'm new here and my english is horrible
I just want know how you can read the Serial.read() as a String
I would be very happy if someone has a example.Thank you very much!
Here is a "readline" function that I use to get typed user input from the serial terminal. It returns the user's input as a string in a buffer.
// read a line from user into buffer, return char count
int readline(char *buf, int limit)
{
int x;
int ptr = 0;
while(1) {
if(Serial.available()) {
x = Serial.read();
if(x == 0x0D) { // cr == end of line
buf[ptr] = 0; // null terminate string
return ptr; // return char count
}
if(x == 0x08) { // backspace
if(ptr > 0) {
ptr--;
Serial.print((char) 0x08); // backup cursor
Serial.print((char) 0x20); // erase char
Serial.print((char) 0x08); // backup cursor
} else {
Serial.print((char) 0x07); // beep == home limit
}
} else {
if(ptr < (limit - 1)) {
Serial.print((char) x);
buf[ptr++] = x;
} else {
Serial.print((char) 0x07); // beep == end limit
}
}
}
}
}
To use it, you create a buffer, then call the function, specifying the buffer and the maximum allowable input characters (which of course cannot be more than the buffer size!):
int x;
char buffer[32];
Serial.print("Please enter your name: ");
x = readline(buffer, 32);
Serial.print("\r\nYou typed ");
Serial.print(buffer);
Serial.print(" and it contains ");
Serial.print(x, DEC);
Serial.print(" characters.\r\n");
Hope this helps.
ChuckNorris95:
I just want know how you can read the Serial.read() as a String
If you are referring to the String class, the answer is that you don't use it - currently, it exposes a memory management bug in the Arduino runtime library that can make your sketch unstable. You are advised to use 'c' strings (null-terminated char arrays) instead.
I just want know how you can read the Serial.read() as a String
The reading of the serial buffer is one character at a time, so the individual characters have to be captured and added to the previously captured characters to make a string. Below is a very simple example.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}