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.
Usage
laptop% gcc -o arduino-serial arduino-serial.c
laptop% ./arduino-serial
Usage: arduino-serial -p <serialport> [OPTIONS]
Options:
-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'
Example Use
Send the single ASCII character “6″ to Arduino
laptop% ./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.
Send the string “furby” to Arduino
laptop% ./arduino-serial -b 9600 -p /dev/cu.usbserial -s furby
Receive data from Arduino
laptop% ./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.
Send ASCII string “get” to Arduino and receive result
laptop% ./arduino-serial -b 9600 -p /dev/cu.usbserial -s get -r
read: d=0
Internals
There are three interesting functions that show how to implement talking to serial ports in C:
int serialport_init(const char* serialport, int baud)
— given a serial port name and a speed, return a file descriptor to the open serial port.int serialport_write(int fd, const char* str)
– write out a string on the given a serial port file descriptorint serialport_read_until(int fd, char* buf, char until)
– read from serial port into a buffer until a given character is received
You can and should write improved versions of the read and write functions that better match your application.
Update 8 Dec 2006:
Justin McBride sent in a patch because it turns out Linux’s termios.h doesn’t define B14400 & B28800. I’ve updated arduino-serial.c to include the patch, but commented out for now. No one uses those baudrates much anyway. :) If you need them, uncomment the additions out, or better yet, download Justin’s tarball that includes the changes and a Makefile to auto-detect your platform.
Update 26 Dec 2007:
Added ability to sent binary bytes with the ‘-n’ flag.
Added a delay option so you can open a port, wait a bit, then send data. This is useful when using an Arduino Diecimila which resets on serial port open.





I got the Arduino to respond to this Terminal input: arduino-serial -b 9600 -p /dev/tty.usbmodemb21 -s a
but the LED just does 3 quick flashes, not what was programmed for the LED to do in the sketch.
In fact it just flashes the LED regardless of what I send it or what is in the sketch to do.
Any ideas?
Carl
I got the Arduino to respond to this input from Terminal: arduino-serial -b 9600 -p /dev/tty.usbmodemb21 -d 2000 -s b
but the Arduino LED just flashes 3 times very quickly, regardless of what I have programmed into it’s sketch.
Any idea what may be going on?
Thanks,
Carl
Hi Carl,
Can you post your Arduino sketch so I can try it myself?
Finally got it resolved, with a lot of help from a friend. We’re using an AppleScript to telnet to ser2sock
to the Arduino, attached to a remote machine.
Many thanks,
Carl
[SOLVED] I’ve spent over a month trying to have a good communication between linux (Debian Squeeze) and ARDUINO using C programming. I tried every possible configuration with termios.h. My problem was that linux seems to communicate twice at the beginning of the connection with ARDUINO, confusing all the following dialogue. I don’t now why it happens, but finally I’ve found a solution to solve it.
So, this is the C code to initialize serial connection with ARDUINO using termios.h. If you don’t know how the following functions work, I suggest this link: http://pubs.opengroup.org/onlinepubs/7908799/xsh/termios.h.htmlSo.
—- Beginning of C code —–
#include /* File control definitions */ #include /* Error number definitions */ #include /*linux serial library*/ #define DEVICE_ADDR "/dev/ttyACM0" int main() { struct termios options; fd = open(DEVICE_ADDR, O_RDWR | O_NOCTTY | O_NDELAY); if (fd<0) perror("open_port: Unable to open /dev/ttyACM0 - "); options.c_cflag=6322; //6322 is for BOUDRATE 115200, if you want 9600 use 2237 instead options.c_lflag=0; options.c_iflag=0; options.c_oflag=0; tcsetattr(fd, TCSANOW, &options); //the following two lines is the strange thing that makes the whole thing work sleep(1); //Sleep time works only from 1 to 2 (2 not included) tcflush(fd, TCIFLUSH);— end of C code —
This is all, from now on you can add your own code to read and write with ARDUINO, using C read and write functions.
Have a good programming day.