Friday, May 31, 2013

BufferSerial: let Arduino Stream writes go in a buffer

When you have existing code that uses an Arduino Stream like "Serial" or "SoftwareSerial" data is written part by part on the serial device. Sometimes you want to collect different parts first, maybe adding something like a checksum and only then send everything on one go. By using the BufferSerial construct you can just keep the existing code, pass it the BufferSerial instead of your normal Serial and afterward use the content of the buffer, and send it as one big blob.

 A simple example:
uint8_t mybuffer[32];
BufferSerial bs(mybuffer, 32);
bs.write(' ');
bs.print(127, HEX);


The code for BufferSerial is really simple and can be found at: .


Matthijs Kooijman said...

Hmm, is this BufferSerial still related to anything Serial? Shouldn't it just be called BufferStream instead?

Unknown said...

Yes it should, but I figured most users of Arduino don't even know about the underlying Stream class hence the BufferSerial name.