serial.Write() to send a string

I thought serial.Write() can send string. But I get: "no matching function for call to HardwareSerial::write(String&)"

void loop(){
  int value0 = analogRead(0);
  int value1 = analogRead(1);
  int value2 = analogRead(2);
  int value3 = analogRead(3);
  int value4 = analogRead(4);
  int value5 = analogRead(5);
  
  String marker1 = "<";
  String marker2 = ",";
  String marker3 = ">"; 
  String str0, str1, str2, str3, str4, str5;
  
  str0 = marker1 + "0" + marker2 + value0 + marker3;
  str1 = marker1 + "1" + marker2 + value1 + marker3;
  str2 = marker1 + "2" + marker2 + value2 + marker3;
  str3 = marker1 + "3" + marker2 + value3 + marker3;
  str4 = marker1 + "4" + marker2 + value4 + marker3;
  str5 = marker1 + "5" + marker2 + value5 + marker3;
  
  Serial.write(str0);
  Serial.write(str1);
  Serial.write(str2);
  Serial.write(str3);
  Serial.write(str4);
  Serial.write(str5);  
}

I thought serial.Write() can send string.

It can.

But I get: "no matching function for call to HardwareSerial::write(String&)"

A String is not a string!

There is nothing you are doing in that code that needs a String.

I need to serial.Write something like <0,255>

I thought Serial.write() only writes bytes and you use Serial.print() to write strings, integers, etc...

Serial.write() can write individual bytes or arrays of bytes (like character arrays).

Serial.print() converts ints, floats, etc. to strings and calls Serial.write() to send the string.

I need to serial.Write something like <0,255>

Serial.write("<0,");
Serial.print(value0);
Serial.write(">");

Serial.write comes in three versions:

size_t write(uint8_t);
size_t write(const char *str);
size_t write(const uint8_t *buffer, size_t size);

This allows single bytes, a character pointer (pointing to to null-terminated array of bytes), or byte pointer with a specified byte count.

To print out a String you have to use Serial.print() or Serial.println().

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.

size_t write(uint8_t);
size_t write(const char *str);
size_t write(const uint8_t *buffer, size_t size);

The first two are source of constant grief too (0 as argument is ambiguous between them). The last is not implemented in serial, I think.

Here is a tutorial for sprintf:

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

3 Likes

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.

would

char* ptr1 = MyString;
Serial.write(ptr1, sizeof(MyString) * sizeof(char));

work?

If 'MyString' is a char[] array, then this will work:

Serial.write(MyString, sizeof(MyString));

If it's a null-terminated array and you don't want to send the null, then this:

Serial.write(MyString, strlen(MyString));

But guess what? Either way the write function will use a char-by-char loop to accomplish the task.

flowirin:
char* ptr1 = MyString;
Serial.write(ptr1, sizeof(MyString) * sizeof(char));

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.

Remember all these standard functions (and more!) are available for you to use in the Arduino.