From another Arduino Uno I am sending 5 bytes like this for example: . Only when first string is completed I get correct string which length is 5. But all the others strings are length of 7, somehow I get additional two chars in the begining of the received string.
Lines:
First, your post title is wrong. Second, the body of your post contains many errors. You are not having problems with strings. You are having problems due to misusing Strings.
A String is an object that wraps a NULL terminated array of chars. The wrapper offers little benefit, but causes many problems.
So NULL terminated array means that the NULL is a stop sign. Stop processing the data in the array when you get here, You failed to stop at the stop sign.
Not sure what your end product is going to be, but below is a method of capturing a string of characters sent from the serial monitor using a comma to indicate the end of the characters being sent.
//zoomkat 3-5-12 simple delimited ',' string
//from serial port input (via serial monitor)
//and print result out serial port
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}