If all you need is to assemble a string that looks like <0,255>, you can use the Stream or sprintf function. Don't use String class. It's the source of grief.
I have been writing some simple code for a Nextion HMI Display and stumbled onto this problem myself. Since this was one of the first results a quick googling found, I thought I would add my solution here.
When you are trying to write a String object which say you had passed into a method/function. The write method for this isn't implemented.
As written above in other comments are that the write command will take a single Char character though. size_t write(const char *str);
So what I did was make a little snippet method which I can call when ever I like to send a String using the write method.
It simply takes the String .... I called it stringData. And uses a for loop of the length of the String you sent in and 1 by 1 sends it to the desired Serial.write() as a single Char.
Very light weight and does the trick. Hope this helps others who stumbles on this problem in the future and ends up here from Google.
Enjoy.
void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++)
{
Serial.write(stringData[i]); // Push each char 1 by 1 on each loop pass
}
}// end writeString
RamjetX:
It simply takes the String .... I called it stringData. And uses a for loop of the length of the String you sent in and 1 by 1 sends it to the desired Serial.write() as a single Char.
Very light weight and does the trick. Hope this helps others who stumbles on this problem in the future and ends up here from Google.
RamjetX
Funny. I've come to here trying to find a faster way to write to the serial port, without the overheads of a char by char loop.
You didn't define what MyString is. If it's a big-S String then this won't work. If it's a char array that's bigger than the actual string stored in it then you're sending the remaining space in the array, which may be filled with garbage.
Additionally, functions like strlen() which find the length of the string stored in a char array must also loop to examine each character in the string.