CmdMessenger Replaces Messenger
CmdMessenger is a messaging library for the Arduino Platform (and .NET/Mono platform). It uses the serial port as transport layer. To use CmdMessenger, we define a list of command identifiers, then attach callback / handler functions for received messages.
The message format is:
The library gives the following functionality:
With version 3 also comes a full implementation of the library in C#, which runs both in Mono (http://monodevelop.com/Download) and Visual Studio (http://www.microsoft.com/visualstudio/eng#downloads) This enables full 2-way communication between the Arduino controller and the PC.
Download the library here:
And find more background and and example of the functionality here:
A Python interface to this library is also available:
The library needs to be instantiated with the serial port:
If desired, we can change the field_separator, command_separator and escape_character
For readability we can turn on adding new lines at the end of every character
Incoming serial data should be processed in the Loop() function by calling
When sending a command without parameters, the format is
When sending a command with an argument
If we want to send a command, and wait for a response, use:
Note that all commands other than the acknowledge command will be ignored (until time out)
In order to send multiple arguments, we can split the send command up:
Between sendCmdStart and sendCmdEnd, we can send as many arguments as we want using sendCmdArg. Arguments may have diffent data types
Floats are similarly send as ASCII digits, defaulting to two decimal places. If we want to send over more decimal places we can do this by
If we want to format am argument with printf like syntax we can use
Please note that formatted floating point variables are currently not supported on the Arduino platform.
If we want to string that may contain the , or ; character, we can escape it before sending. These characters can now be sent safely. The string needs to be unescaped upon reading
In order to send binary arguments we use
where T is the type of the argument to send. It is prudent to use explicit casting for binary types. Unlike plain text sending is, the parameter types must match precisely between sending and receiving sides.
Attach callback functions to a message ID using
Attach a callback function for messages that have no explicitly attached function using
In your callback function you can read out the parameters that where send together with the command ID
To check if there is (another) argument is available, use
If so, you can read the arguments with the following functions
Note that readStringArg behaves a bit differently, in that the string is only available as long as no new command has been read. To have a persistent string argument, we can use the following function to make a copy
In order to compare a string with the current argument
To read a binary argument use the function
This function can also be used to unescape strings
[Arduino IDE Version 1.0.5 or later](http://www.arduino.cc/en/Main/Software). Earlier versions of the Arduino IDE may work but have not been tested.
Get to know the library, by trying the examples,from simple to complex:
The 1st example will make the PC toggle the integrated led on the arduino board.
This example expands the previous Receive example. The Arduino will now send back a status.
This example expands the previous SendandReceive example. The Arduino will now receive multiple and sent multiple float values.
This example expands the previous SendandReceiveArguments example. The Arduino will receive and send multiple binary values.
All samples are heavily documented and should be self explanatory. 1. Open the Example sketch in the Arduino IDE and compile and upload it to your board. 2. Open de CmdMessenger.sln solution in Visual Studio or Mono Develop/Xamarin Studio 3. Set example project with same name as the Arduino sketch as startup project, and run 4. Enjoy!
An example for use with Max5 / MaxMSP was included up until version 2. (it can still be found here https://github.com/dreamcat4/CmdMessenger). Since we have not been able to check it wil Max/MaxMSP, the example was removed.
(defaults to ';' and ',')
CmdMessenger is provided Copyright © 2013 under MIT License.