As I have been playing with the Arduino hardware platform, I have become more and more convinced of its usefulness. I generally appreciate all efforts to standardize in hardware and am excited about the large community this platform has received.
I quickly realized that were I to use this for my projects I would need an efficient storage of bytes that I couldn’t find in the community libraries as well as a buffered serial write which also hadn’t been implemented to my knowledge. I created these libraries and decided to share them here if anyone would be interested in using them.
These are the properties of the SerialManager library:
- Provides a buffered write method that I have not yet found in other libraries (processor can work on other things while serial port pushes out a packet of data)
- Provides a built in checksum calculation which can reduce errors in transmission and can be tedious to manage
- Provides built-in packet header search and hides it in the library code so the user code becomes more readable (can be disabled for sending raw packets)
This project is now hosted on Google Code
https://code.google.com/p/arduino-buffered-serial/
Example usage
#include <SerialManager.h> #include <ByteBuffer.h> SerialManager serial = SerialManager(256, 256); ByteBuffer send_buffer; int SEND_MODE = 0; void setup() { // initialize the serial communication: serial.init(0, 9600); serial.setPacketHandler(handlePacket); // Initialize the send buffer that we will use to send data send_buffer.init(30); } void loop() { serial.update(); // If we are not busy sending then lets send something if( !serial.isBusySending() ){ // Send some dummy data send_buffer.clear(); send_buffer.put(17); send_buffer.putInt(300); send_buffer.putLong(-100000); send_buffer.putFloat(3.14); // We can either send a packet with a header and checksum (niiice) if(SEND_MODE == 0) serial.sendSerialPacket( &send_buffer ); // Or if we are sending to say a device that uses a custom protocol, // we can send a raw byte buffer to it if(SEND_MODE == 1) serial.sendRawSerial( &send_buffer ); // Or if we just want to simply send one byte we can do that, if(SEND_MODE == 2) serial.sendSerialByte(16); } } void handlePacket(ByteBuffer* packet){ // Here we could do anything we want to the data but for now we will just send it back send_buffer.clear(); while( packet->getSize() > 0 ) send_buffer.put( packet->get() ); serial.sendSerialPacket( &send_buffer ); }

The library does not seem to work
Im running an ArduinoMega (Atmega 1280 chip)
The error msg I got was from within the library file
SerialManager.cpp: In member function ‘void SerialManager::update()’:
SerialManager.cpp:124: error: ‘UDRE1′ was not declared in this scope
SerialManager.cpp:126: error: ‘UDRE2′ was not declared in this scope
SerialManager.cpp:128: error: ‘UDRE3′ was not declared in this scope
the line of code which gave the problem is the following
#if defined(__AVR_ATmega1280__)
else if( _serial_port == 1 )
if(((UCSRA1) & (1 <get() );
else if( _serial_port == 2 )
if(((UCSRA2) & (1 <get() );
else if( _serial_port == 3 )
if(((UCSRA3) & (1 <get() );
#endif
Any help would be appreciated
Hi Esa
I apologize for the error, I didn’t have an ArduinoMega to test it on so I couldn’t find the bug when I made the library. In any case, it was a matter of changing the constant names UCSRA1, UCSRA2 and UCSRA3 to UCSR1A, UCSR2A and UCSR3A.
I have updated the library with this bug fix. Please let me know if you run into further trouble
Still isn’t working with Arduino Mega. First it is depends on not-supplied ByteBuffer. I’ve found one here:
http://code.google.com/p/brewtroller/source/browse/trunk/I2CLCD/?r=635
After that it compiles with these errors:
C:\Documents and Settings\Administrator\My Documents\Arduino\libraries\SerialManager\SerialManager.cpp: In member function ‘void SerialManager::update()’:
C:\Documents and Settings\Administrator\My Documents\Arduino\libraries\SerialManager\SerialManager.cpp:125: error: ‘UDRE1′ was not declared in this scope
C:\Documents and Settings\Administrator\My Documents\Arduino\libraries\SerialManager\SerialManager.cpp:127: error: ‘UDRE2′ was not declared in this scope
C:\Documents and Settings\Administrator\My Documents\Arduino\libraries\SerialManager\SerialManager.cpp:129: error: ‘UDRE3′ was not declared in this scope
Cheers.
Hi itod
Thanks for the pointer, so you pointed out two problems you had.
The first one I believe was just a simple oversight on your part, if you look closer at this page under the Download heading, it points to the ByteBuffer library (and please use this version as it is probably more up to date than the one you found where someone has downloaded an old version of it and put on google code)
The second issue was a real bug and came from the fact that I had some redundant #define statements for the Atmega1280 in my SerialManager.h file. I have now removed them and the library compiles for Arduino Mega.
Let me know if there are any further problems
Do you have an example using buffered receive? Thanks, Scott
Hey there,
I am curious is this might help me with some serial communication troubles I am having using arduino mega’s and xbee pro 900 wireless.
I am trying to read step and direction inputs into one arduino (for a total of 5 pairs of inputs) and send those over xbee to a second arduino mega which then outputs the step/direction signals to 5 stepper motors.
I have the basic communication link working but the actual output is not controlling the motors right and so I am thinking that maybe the way my serial data is being sent and received may be causing the data to get corrupted, dropped or just exceeding the transfer rate.
Any thoughts?