USE THIS INSTEAD: CmdMessenger
Messenger library for Arduino by Thomas Ouellet Fredericks
Latest version of Messenger including Max5 and Processing examples
Messenger is a "toolkit" that facilitates the parsing of ASCII messages. Messenger buffers characters until it receives a carriage return (CR). It then considers the message complete and available. The message is split into many elements as defined by a separator. The default separator is the space character, but can be any character other than NULL, LF or CR.
-- or --
You can create an instance of Messenger by specifying a message separator. If you do not specify a separator, the space character will be selected.
Attaches a callback function that is executed once a message is completed. This is the preferred way of working with Messenger (see example at the bottom of this page).
Returns true if a message has been completed and is available. Once a message is completed, it will be split into elements that you should be read immediately with read or string methods because every call to process() erases the completed message if there are any leftover elements.
returns true if there are any elements available in the completed message. You must make a call to process() before using available().
You must make a call to process() to complete a message before trying to read it's elements. Every time you use a read method, the read element (even if it is not of the proper type) is removed from the completed message and Messenger will point to the next one. Use available() to check if their are any elements left to read.
Returns the element as an integer. Returns 0 if the element was not a number.
Returns the element as an long. Returns 0 if the element was not a number.
Returns the element as a character. If the character is part of a word, the whole word is removed from the completed message.
Copies the element as a string into the the array pointed by the target char array. maxSize must match the size of the target char array. The element is removed from the completed message.
Compares the element to the string toCheck.
Included in the download, you will find examples for Processing and Max5. Here is an example of how you would use Messenger in your Arduino code:
// This example sets all the values of the digital pins with a list through a callback function #include <Messenger.h> // Instantiate Messenger object with the default separator (the space character) Messenger message = Messenger(); // Create the callback function void messageReady() { int pin = 0; // Loop through all the available elements of the message while ( message.available() ) { // Set the pin as determined by the message digitalWrite( pin, message.readInt() ); pin=pin+1; } } void setup() { // Initiate Serial Communication Serial.begin(115200); // Attach the callback function to the Messenger message.attach(messageReady); } void loop() { // The following line is the most effective way of using Serial and Messenger's callback while ( Serial.available() ) message.process(Serial.read () ); }