Serial Monkey

SerialGeorgeThe Serial Protocol Simulator is one of the tools in MegunoLink, our free utility for communicating with Arduino’s. MegunoLink can also monitor serial dataplot data received from the Arduino, upload compiled programs andmore.

MegunoLink’s Serial Protocol Simulator is implemented by a clever little monkey named George. The simulator simplifies testing control of serial devices. If the serial device isn’t available, or a rogue command sent by the Arduino would create a catastrophe, just connect to the serial port with MegunoLink. Configure the communication protocol on the Serial Monkey tab and George will send out programmed responses every time he detects the appropriate pattern in the serial stream. The protocol is saved with your project, so it is easy to reuse. And you can have several copies of MegunoLink running, each providing responses on a different serial port: connect it to every port on the Arduino Mega if you need to. You can easily see what George is up to: each response sent is added to the list on the Serial Monkey tab.

Pattern Configuration

Use the controls on the Serial Monkey page to create the patterns George will detect in the serial stream and the responses he will send in reply. Each pattern also has a description. The description can be any text to help you remember what the pattern is for. This description is added to the list box each time George replies to a pattern he has detected.

Use the Add, Edit Delete and Duplicate buttons to create, alter and remove patterns. Each pattern can be enabled or displayed using the adjacent check box. If the box is ticked, George will look for and respond to the pattern; when unchecked the pattern will be ignored.


Pattern Format

For the most part patterns are matched and sent exactly as is. Special characters, such as new-lines, can be matched using the following escape sequences:

CharactersMatchesCharacter Code
\rCarriage Return0x0d
\\Forward Slash0x5c
\xNNHex Code0xNN

Starting and Stopping Pattern Matching

George will only look for patterns and send responses when the check box labelled “Enabled” on the Serial Monkey page is checked. Of course the serial port must be connected and open too!

Example: Simulating an Onkyo Receiver

This simplified example uses an Arduino Mega to turn an Onkyo receiver on and off. The Arduino is connected to the receiver via serial port 1 and will send a power command to the receiver when the user pushes a button on an infrared remote control. Pushing 1 will make the Arduino turn the receiver on; pushing 0 will have it turn the receiver off.

Talking to the Receiver

The command to turn the receiver on is: !1PWR01\r; and off: !1PWR00\r. It will reply in kind sending !PWR01\r when the receiver turns on and !PWR00\r when it turns off. Interestingly the receiver also sends its power message when it is turned on by its own remote so the Arduino could detect this and, for example, turn on a TV as well. But let’s not get ahead of ourselves!

Unfortunately the receiver is a long way from the development computer. So we’re going to test the Arduino program by attaching the development computer to the serial port that would go to the receiver. Then MegunoLink will detect the power messages sent by the Arduino and reply in place of the receiver.

To make things more interesting, I’ve connected a uLCD-28PT(GFX) display from 4D Systems to serial port 3. The display is running a simple terminal program, displaying any text it receives. When the Arduino detects a reply to the power command it will send a message to the display so we can see if the receiver turned on or off.

Here’s the hardware mock-up:


Arduino Code

This code does two things: waits for an infrared signal then sends a serial command to port 1 to turn the receiver on or off (lines 35–52) and waits for a reply from the receiver and reports the response to the display attached to serial port 3 (lines 60–85).

Using Serial Monkey to Fake the Receiver Response

We can test this code without the receiver by attaching a computer running MegunoLinkto serial port 1. Use the Add button on the Serial Monkey page to add the two patterns:

Power ON!1PWR01\r!1PWR01\r
Power OFF!1PWR00\r!1PWR00\r

You’ll find these patterns already set up in the MegunoLink Project.xal file included in the download.

Enable the Serial Monkey by checking the box labelled Enable near the bottom of the window, select the appropriate COM port and press the Connect button to start a live connection. Each time you press the ‘1’ or ‘0’ button on the remote control, the Arduino will send a power command to MegunoLink, MegunoLink will reply and the Ardunio will report the change in power state to the display.



You can see the serial commands as the come in from the Ardunio on the Monitor page:


Now the program is ready for testing on the receiver itself, but that’s a story for another day.