In late 2006 I wrote “arduino-serial“, mostly for myself, to help with stuff I was working on at the time. It was a very simple & small, cross-platform tool written in basic C for reading/writing serial ports.
Now nearly seven years later I still get regular questions and frustrations about it. Part of this is due to how Arduinos have changed over time. You used to have to hand-reset an Arduino board, now the act of opening the serial port resets it. This has its plusses and minuses, but it really made my original use-case of arduino-serial fail. Then there were just all the minor deficiencies of the program.
To address some of these issues, but still keep things small & light, I’ve done a bit of fix-up of arduino-serial. It’s now hosted on Github at:
Changes & Improvements
Some changes that I recently made to arduino-serial:
- Separation of the application (
arduino-serial.c) from the library (
- Fixed probable
--portopen to allow re-opens
--sendlinecommand to send a string followed by a newline
--flushcommand to clear out receive buffer
--eolcharoption to let you specify your own end-of-line character if ‘\n’ isn’t appropriate
--timeoutoption to specify a read timeout (reads no longer block infinitely)
--quietflag to make output more terse/machine-readable
Here’s what the new usage help screen looks like:
laptop% ./arduino-serial Usage: arduino-serial -b <bps> -p <serialport> [OPTIONS] Options: -h, --help Print this help message -b, --baud=baudrate Baudrate (bps) of Arduino (default 9600) -p, --port=serialport Serial port Arduino is connected to -s, --send=string Send string to Arduino -S, --sendline=string Send string with newline to Arduino -r, --receive Receive string from Arduino & print it out -n --num=num Send a number as a single byte -F --flush Flush serial port buffers for fresh reading -d --delay=millis Delay for specified milliseconds -e --eolchar=char Specify EOL char for reads (default '\n') -t --timeout=millis Timeout for reads in millisecs (default 5000) -q --quiet Don't print out as much info Note: Order is important. Set '-b' baudrate before opening port'-p'. Used to make series of actions: '-d 2000 -s hello -d 100 -r' means 'wait 2secs, send 'hello', wait 100msec, get reply'
arduino-serial has always been designed so you can “pipeline” commands/options, but it wasn’t implemented very consistently. It’s a bit better now. You can do multiple send/read pairs, even use multiple serial ports, all from a single command-line invocation.
For example, if you have the “SerialCallResponseASCII” sketch from the Communications examples loaded onto your Arduino, you can run commands to take multiple data readings. In the example below, the order of operations are:
- serial port is opened (at 9600)
- the string “A” is sent (a single-byte)
- the first line is read
- sleep for 1000 milliseconds
- send “A” again
- read second data line
- flush read buffer (just to show we can)
- send “A” a third time
- and take a final reading
laptop% ./arduino-serial -b 9600 -p /dev/tty.usbmodemfd131 \ -s "A" -r -d 1000 -s "A" -r -F -s "A" -r send string:A read string:465,396,0 sleep 1000 millisecs send string:A read string:358,352,0 flushing receive buffer send string:A read string:307,305,0
The flush was put in there to demonstrate that you could flush the receive buffer mid-command if you wanted. It’s not required, but might help some situations.
To use multiple serial ports, you can do something like the below, which opens up one serial port at 9600 bps, does a send & receive, then opens another at 57600 bps and does a send & receive on it:
laptop% ./arduino-serial -b 9600 -p /dev/tty.usbmodemfd131 \ -s "A" -r \ -b 57600 -p /dev/tty.usbserial-A800f8ib \ -s "hello" -r