There have been a couple of posts on the forum concerning this already, but I couldn't find any truly explicit means of doing the following:
Serial.print(PSTR("Test!"));
Serial output:
Test!
I looked around at the PROGMEM documentation and found a couple of helpful tutorials around the net that resulted in a quick and dirty way to get something similar to the above example to work. It requires making a few simple changes to HardwareSerial.cpp and HardwareSerial.h. I'm sure this has been done before, but perhaps this will be beneficial to some.
Modify HardwareSerial.h to look like so (note, this just shows the class, not the entire .h file):
class HardwareSerial
{
private:
//uint8_t _uart;
void printNumber(unsigned long, uint8_t);
public:
HardwareSerial(uint8_t);
void begin(long);
uint8_t available(void);
int read(void);
void flush(void);
void print(char);
void print(const char[]);
void print(uint8_t);
void print(int);
void print(unsigned int);
void print(long);
void print(unsigned long);
void print(long, int);
void println(void);
void println(char);
void println(const char[]);
void println(uint8_t);
void println(int);
void println(unsigned int);
void println(long);
void println(unsigned long);
void println(long, int);
void printp(const char*); //add this guy
};
Basically, just add the public member function: void printp(const char*)
now, add the following function to HardwareSerial.cpp:
void HardwareSerial::printp(const char *data)
{
while(pgm_read_byte(data) != 0x00)
print(pgm_read_byte(data++));
}
Also, in HardwareSerial.h make sure to add:
#include <avr/pgmspace.h>
Now, in the main body of your sketches, if you want to use PSTR() to print to the serial monitor (hence saving RAM), you can do it like so:
Serial.printp(PSTR("Test!"));
This is no catch-all, as any programmer can probably see. Currently, it works well with inline strings and such, which is what hogs up the RAM on a lot of my programs. I wouldn't recommend it for anything else, though (because I doubt it works!). So, there you go, a quick and dirty way to get those inline strings you've moved to flash memory to print to the serial monitor. I would ultimately like to see a Serial.print() that can recognize this without the need to create another member function. Perhaps this has been done and I just can't find it?
Unfortunately, I tried a similar approach with SoftwareSerial to no avail. This would benefit me at least, as I print to LCD screens and the like often in my programs. Does anyone have any suggestions for this?
Thanks,
EH