Python based Arduino serial utility

I was looking at this link on the todbot blog. It is a utility C program that sends some serial data to your Arduino. I decided to customize this concept for my own use and also to make it a little simpler to read, use, or re-use. I was going to write this in C mostly b/c I wanted the user to be able to use UNIX pipes to send data over the line and I wanted it to be as little terminal typing as possible. Here is the desired result for the user’s CLI experience:

    $ arduino-serial < data.txt

and not something like this:

    $ python arduino-serial.py -b 9600
       -p /usr/dev/tty.usbserial-A4000QZG
       -f /usr/ben/Documents/data.txt
    # you get the idea

So, like I was saying, I originally thought I should do this in C; but, then I realized I could easily achieve these results and have a much shorter program even if I used python. Here is the program in tarred and gzipped. I’m not really going to explain how the program works b/c it is so simple, but I will explain how I put it together.
**Before I get started, you should know that I created this for my MacBook Pro (OS X 10.4.11, Python 2.5) and this program should work with most BSDish systems but probably not Windows. Also, you need pyserial. Download it and do the usual install commands after you unarchive it:

    $ python setup.py build
    $ python setup.py install

Moving on now…..
First, I created a symlink to the arduino serial port called usbserial.arduino by opening up my Terminal application and dropping in this:

    $ sudo ln -s /dev/tty.usbserial-A4000QZG /dev/tty.arduino

My postfix is A4000QZG but yours will be different, if you don’t know it, run this command:

    $ ls /dev/tty.usbserial*

This path is set as a constant in my program so if you create this symlink, you won’t have to change it.

    ARDUINO_PATH = "/dev/tty.arduino"

Next thing you want to do is copy this file to your /usr/bin directory, you will probably need sudo:

    $ sudo cp ./arduino-serial.py /usr/bin/arduino-serial

This assumes, of course, you are currently in the directory that contains the arduino-serial.py file. Keep in mind that this also strips off the .py extension. I think as long as you have the correct shebang in there “#!/usr/bin/env python”, you should be fine. Now you have to give the file the proper permissions:

    $ chmod 755 /usr/bin/arduino-serial

After this, everything should be running swimmingly. You can just send an ASCII text file byte by byte doing something like show above:

    $ arduino-serial < data.txt

You can run this from any directory in your terminal because the program was put in “/usr/bin/”. You can verify its status as a global program by typing:

    $ which arduino-serial

There are a few ways to use this program:
You can use the ‘-b’ option to set the baud rate. If you don’t enter this option, it is default=9600. This is a common baud rate for me but if you have a different preference, you can change this in the program in the ‘default’ parameter of this function:

    parser.add_option("-b", "--baud", dest="baud",
                      default=9600, help="serial baud rate")

There is also the -d option for ‘data’. You can use that to send just one character or sequence of characters.
In conclusion, this could probably best be used to test your serial reading code on your arduino or maybe even send it configuration files or possibly hex binaries.

1 comment so far ↓

#1 brock craft » Serial to Arduino step-by-step on 03.23.11 at 4:39 pm

[...] my list. However, as with such things, there is no need to re-invent the wheel! Fortunately, Ben (over here), has written a nice little python script to handle just such events. So, I’m now snagging [...]