Messenger library

Ok, so you are sending messages from tera term pro to Serial1.

You know the link works because you have tested it with some code like:

while  (Serial1.available() ) Serial.print(;

that lets the Arduino IDE’s Serial Monitor echo whatever you send from tera term pro.

Are you sure that tera term pro is sending carriage returns? Carriage returns must be sent to terminate a message.

Please test the following code. Send either a “0” or a “1” followed by a carriage return to either Serial or Serial1. A 1 will turn on the LED at pin 13, a 0 will turn it off. Also, all data received by Serial will be echoed to Serial1, and the opposite is also true.

#include <Messenger.h>

Messenger message = Messenger();
Messenger message1 = Messenger();

void messageReady() {
       while ( message.available() ) {
      // Set the pin as determined by the message
         digitalWrite( 13, message.readInt() );

void messageReady1() {
       while ( message1.available() ) {
      // Set the pin as determined by the message
         digitalWrite( 13, message1.readInt() );

void setup() {
  // Initiate Serial Communication
  // Attach the callback function to the Messenger
  message.attach(messageReady); // Serial
  message1.attach(messageReady1); //Serial1

void loop() {
  int data;
  // Serial1
  while ( Serial1.available() ) {
    data = ();
    Serial.print(data,BYTE); // Echo data on other port
    message1.process(data); // Process data
  // Serial
  while ( Serial.available() ) {
    data = ();
    Serial1.print(data,BYTE); // Echo data on other port
    message.process( data ); // Process data

Please report any and all error messages that this code generates. Please also include what is received by both serial monitors.

I tried the code that you posted. The results were: - No error message

  • So i had 2 monitors open. One was monitoring "Serial"(USB) and the other was monitoring "Serial1". When i type into "Serial" it turned the LED on and off the way it was supposed to. and the "Serial1" monitor would also echo back whatever i typed with absolutely no problems. however when i type into "Serial1" nothing happens to the LED and Nothing is echoed back on the "Serial" monitor


to me this confirms that the problem is not with Messenger but with your input/output of Serial1.

The echo is not part of Messenger and should work without it. Check your connections and make sure that whatever you send to Serial1 is echoed to Serial. Once you have a solid bi-directional communication between both (that both monitors echo the other), you can try to add Messenger.

Hi.. TOF!!!!!!!

Thanks a lot for your help.. it turns out that the MAX233 was just working one way the other direction was faulty.. i replaced the chip and it works awesomeee!!!

Thanks again for the help and thanks to whoever made the library..

Regards, Maysam

thanks for the lib, the example supplied with ver 1.4 is for outputing values from the arduino which if i understand not what messanger is about, right?

"Messenger only provides methods for incoming messages. The internal Serial.print() functions work just fine for outgoing messages."

You are right. Messenger only provides methods for incomming messages. The examples demonstrates how to receive messages with Messenger's messageCompleted() callback function but also demonstrates how to send messages so they can be parsed with the matching Messenger libraries for Max or Processing.

i might be missing something there, the comment of the example im refering to is

This example parses a message that contains the values
 of an Arduino's analog inputs and displays them as rectangles.
The message must be composed of the readings of the 6 inputs. 
Each readingis separated by a space (' ') and terminated 
by a carriage return and line feed

is there somehwere an example with processing>arduino communication. i tried using the simpleMessage examples but that didnt work. those messanger support the simpleMess protocol

 r a -> read analog pins
 r d -> read digital pins
 w d [pin] [value] -> write digital pin
 w a [pin] [value] -> write analog pin

as a matter of fact i thinkn all this software protocols between arduino and the computer are bad education. i searched for serial.flush() and was directed to, i think this is the direction should go. tommorow i have to explain conditions to a seeduino mega and a processingxp

this looks so shinny, waht does it do?

Beside the 2 following improvements:

  1. define the variables as unsigned char

     unsigned char rx_buffer_head = 0;
     unsigned char rx_buffer_tail = 0;

  2. use the following code to wrap the values

     rx_buffer_tail = rx_buffer_tail + 1;
     rx_buffer_tail %= RX_BUFFER_SIZE;

  3. Tweak buffer size for your application.

what else can be done to improved the performance of Hardware Serial?  I am stuck using 4800 baud 



Hi Yair,

I will try to understand your posts as best as I can. The Processing example supplied with the Messenger library works very with the Arduino example provided.

I might be missing something there, the comment of the example im refering to is [...] is there somehwere an example with processing>arduino communication. i tried using the simpleMessage examples but that didnt work. those messanger support the simpleMess protocol

As stated in Messenger's documentation: All of your host software code that you designed for SimpleMessageSystem should still be compatible with Messenger. You will simply have to modify your Arduino code.

So you need to modify the Arduino code to match the software code.

I think the simplest solution for you would be to use Firmata (try my version here)

Also check this post out.

im using a mega, which is unsupported (the changelog tells it does, everyone else it doesnt).
thanks for your support, i will have to warp my head around bytes eventually. i think its for the best.
i used some code from the previous link i mentioned and from this megaServo example.


you can try the following code (I can not test it):

#include <MegaServo.h>
#include <Messenger.h>

#define NBR_SERVOS     12        // the number of servos, up to 48 for Mega, 12 for other boards

MegaServo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

Messenger message = Messenger(); 

// Define messenger function
void messageCompleted() {
   int i = 0;
  while ( message.available() && i < NBR_SERVOS ) {

void setup()
  for(int i=0; i < NBR_SERVOS; i++)
    myServos[i].attach(FIRST_SERVO_PIN +i);  

void loop()
  while ( Serial.available( ) ) message.process( ) );

From you host software, send something like the following ascii message (they are not bytes but string representations of the numbers):
“123 84 39 128 274 50 100 543 83 43 100 101”

Where each number corresponds to the position for the servo at that index (first number for first servo, etc). Do not forget to terminate the message with a carriage return!


its me again.. i mhaving a different problem this time.. I am trying to control a couple servos through processing.. i send an ascii string as follows to the arduino "int1 int2 int3 int4 int5 int6" and then a carriage return. i receive them and store them in an array i display the array on the LCD.. the trouble is that every so often the first elements and other elements get mixed up and that cause my servos to twitch.. do you know of a fool proof method of receivin an array of integers that would guarantee that the first number received is stored in the first element in the array.??

Thanks Swalehm

Swamlehm, i suggest you take a look at this code here. its not using any lib for communication but rather let you take care of protocol. it might seem a bit to "cody" at first but it lets you set a value to an array index with a simple "90a will write a value of 90 to the first servo, 180b will write 180 to the second servo" i guess you can also adapt messanger to use this.

if you want to continue sending an array its best not to swamp the buffer with to many calls (slow the pace, simple) or devise a start/stop bit rutine to handle only valid packets (hardish).

swalehm, please post your code. There can be many reasons for this behavior and there are many ways you can implement a simple integrity check. Please aslo post the software and OS you are using.

Fantastic library! I'm using it to exchange data with a servocontroller class. In order to set encoder ratio, from impulses to real world coordinates, I added this simple extension method to the class.

in messenger.cpp

double Messenger::readDouble() {
      if (next()) {
            dumped = 1;
            return atof(current); // atof for double instead of atoi for int variables
      return 0;

in messenger.h

  double readDouble();

Thanks for your great job!


just wondering if you could tell me the paths for the files in the zip into my Processing sketchbook library and any other?

would like to switch to this

thanks jeremy

Great library, thanks very much for sharing this...

I've added a few things in my own class CmdMessenger which extend from Messenger. Had to make a few small changes to the origional Messenger which I've included here along with my CmdMessenger class if its useful to anyone else.

Basicly with CmdMessenger sub class you can attach more than 1 messenger handler, which get called according to the first string (cmd) in a message. If a valid message for the command hasn't been attached then the single default message handler of Messenger is called.

The zip file contains the orgional Messenger with my small changes, the new CmdMessenger class and two sample programs (one runs on Arduino the other on a TouchShieldSlide) connected via hardware Serial1. If your using softwareSerial, which dosn't have the Serial.available() method then you will need to change the feedinSerialData() mehod.


Not very experienced with Java. I am a maxmsp whiz. This library seems like what I want (firmata is too bloated), but maybe it can't do this?

I just want to route messages, like in the example but parse numbers that follow the message name. ie send a value over max-messanger like this:

dimmer1 $1 dimmer2 $1

Sorry if this is obvious.

I started using the library with the message callback function similar to the one from the examples (CheckString):

void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
    if ( message.checkString("on") ) {
    } else if ( message.checkString("off") ) {

I found out that there is one danger with this. It you sent any message that doesn't match the strings which you are checking against, you get stuck in an endless while loop, because the message is never removed from the stack.

This problem became apparent when i was using 2 modules communicating with XBee on the same network. They both listen to different messages and so every message would get either one of hem to hang.

I solved this by simply adding an


at the end of the function. This way if the message doesn't match with any of the strings, it is removed and the loop continues with the next.

It would be helpful if this caveat is mentioned in the examples. Maybe it would be elegant to have an explicit method for skipping an element in the message stack?

I'm trying to use the Messenger library in Processing.

First of all I'm missing the string functions, I guess they're just not implemented yet?

So I want to match strings, or at least combinations of ascii characters manually. It seems so simple but I cannot figure it out.

I want to match something like "BFOO". The only way to access these characters seems to be readChar(). The first call returns 'B', but then the rest seems to be thrown away, and I don't know how to get to the remaining characters.

The reference says:

char readChar()

Returns the element as a character. If the character is part of a word, the whole word is removed from the completed message.

So this seems to be correct, but I don't understand the idea behind it. How is that useful, and how can I access those other characters?