Arduino-serial
From Wikiants
Arduino-serial: C code to talk to Arduino
The Arduino’s USB port is actually a serial port in disguise. To your computer it appears as a ‘virtual’ serial port. This is good news if you want to write custom code on your computer to talk with the Arduino, as talking to serial ports is a well-solved problem. (Unfortunately, so well-solved that there’s many ways of solving it.)
On the Arduino forum there’s been a few requests for some example C code of how to talk to Arduino. The nice thing about standard POSIX C code is that it works on every computer (Mac/Linux/PC) and doesn’t require any extra libraries (like what Java and Python need). The bad thing about C is that it can be pretty incomprehensible.
Here is arduino-serial.c, a command-line C program that shows how to send data to and receive data from an Arduino board. It attempts to be as simple as possible while being complete enough in the port configuration to let you send and receive arbitrary binary data, not just ASCII. It’s not a great example of C coding, but from it you should be able to glean enough tricks to write your own stuff.
Contents |
[edit] Downloads
[edit] Linux
Open a Terminal and go by example to /usr/local/src make a directory called arduino-serial and start the download:
mkdir /usr/local/src/arduino-serial cd /usr/local/src/arduino-serial wget http://todbot.com/arduino/host/arduino-serial/arduino-serial.c
[edit] Compilation
The line 199 has a special baud rate which almost nobody use. You have to uncomment it ( a double slash: // ) or download the patch, which should fix it. After that the program can be compiled with the following code ( gcc must be installed ):
gcc -o arduino-serial arduino-serial.c
[edit] Helpful notes
./arduino-serial -h -h, --help Print this help message -p, --port=serialport Serial port Arduino is on -b, --baud=baudrate Baudrate (bps) of Arduino -s, --send=data Send data to Arduino -r, --receive Receive data from Arduino & print it out -n --num=num Send a number as a single byte -d --delay=millis Delay for specified milliseconds
Note: Order is important. Set '-b' before doing '-p'. Used to make series of actions: '-d 2000 -s hello -d 100 -r' means 'wait 2secs, send 'hello', wait 100msec, get reply'
For starting the program arduino-serial from every directory, you can move the file to /usr/local/bin:
mv ./arduino-serial /usr/local/bin
or better, put a symlink:
ln -s ./arduino-serial /usr/local/bin/arduino-serial
[edit] Examples
[edit] Send the single ASCII character “6″ to Arduino
./arduino-serial -b 9600 -p /dev/tty.usbserial -s 6
This would cause the Arduino to blink 6 times if you’re using the serial_read_blink.pde sketch from Spooky Arduino.
[edit] Send the string “furby” to Arduino
./arduino-serial -b 9600 -p /dev/cu.usbserial -s furby
[edit] Receive data from Arduino
./arduino-serial -b 9600 -p /dev/cu.usbserial -r read: 15 Hello world!
The output is what you would expect if you were running the serial_hello_world.pde sketch from Spooky Arduino.
[edit] Send ASCII string “get” to Arduino and receive result
./arduino-serial -b 9600 -p /dev/cu.usbserial -s get -r read: d=0
[edit] Bash Script | Communication with Linux
#!/bin/bash
# patrick schoenenberger
# p ( at ) wikiants.org
# license under by-nc-sa from creative commons
i=0
ardread=0
stopvar=no
touch /tmp/stop
echo 0 > /tmp/stop
/opt/bin/arduino-serial -b 9600 -p /dev/usb/tts/0 -d 3000 -r
function arduread
{
ardread=`/opt/bin/arduino-serial -b 9600 -p /dev/usb/tts/0 -d 200 -r`
ardudiff
}
function ardudiff
{
case $ardread in
"read: 1") mplay;;
"read: 0") killmplay;;
esac
}
function mplay
{
echo mplayer
sleep 5
dbclient -f -y -i /opt/.ssh/id_rsa elchig@192.168.xxx.xxx '/usr/local/bin/mplay'
sleep 3
}
function stopplay
{
stopvar=$(cat /tmp/stop);
if [ $stopvar -eq 1 ];
then
killmplay
fi;
}
function killmplay
{
echo killmplayer
nice -19 dbclient -f -y -i /opt/.ssh/id_rsa elchig@192.168.xxx.xxx 'killall -9 mplay'
nice -19 dbclient -f -y -i /opt/.ssh/id_rsa elchig@192.168.xxx.xxx 'killall -9 mplayer'
sleep 5
echo 0 > /tmp/stop
}
while [ $i -eq 0 ];
do
arduread
stopplay
done
[edit] See also
Videozentrifuge HowTo A videosculpture which uses arduino serial ( german )
[edit] Preferences
Categories: Arduino | Software | Interface | Linux | Bash | Open Source

