string problem with software serial write

Hi,

I'm facing a simple but frustrating problem with software serial writing. This code works:

SoftwareSerial BTSerial(10, 11);
...
BTSerial.write("test");

This code DOES not work:

SoftwareSerial BTSerial(10, 11);
String p = "test";
...
BTSerial.write(p);

What's the problem? I get the following error:

no matching function for call to 'SoftwareSerial::write(String&)'.

Please read Nick Gammon's post at the top of this Forum on the proper way to post source code here, which includes posting all of the code. It makes it easier for us to understand what you're doing and to test the code. Without it, we are just guessing.

Looks like the documentation (http://www.arduino.cc/en/Reference/SoftwareSerial) references the Serial documentation which uses .write() only for single characters, null terminated character strings, and character arrays with a character count. This implies you should use .print() for all other data types.

As always, John is right. Also, if you take the String class version and compile it, it uses 4050 bytes. The program below does the same thing, but only uses 2640 bytes. Moral: forget about the String class and start using the str*() and mem*() family of C functions.

#include <SoftwareSerial.h>

#define rxPin 0
#define txPin 1

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

 // String p = "test";        // 4050 bytes
 // mySerial.print(p);

  char p[] = "test";          // 2640 bytes
  mySerial.write(p);
  
}

void loop() {
  // ...
}

For a starting point, try:

http://www.cplusplus.com/reference/cstring/

#define rxPin 0
#define txPin 1

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

This is just plain stupid. If you are going to use the hardware serial pins, use the hardware serial instance.

I was doing it that way because I had no other serial device available and wanted to show the impact of using the String class and trying to stay within the framework the OP provided. It was simply an effort to show the difference. I think your comment is uncalled for and, hence, equally stupid.

I think your comment is uncalled for and, hence, equally stupid.

You are entitled to your opinion. No matter how wrong it is. 8)