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.






Oh! It sounds like you don’t yet have the “arduino-serial” program compiled yet.
Did you do the steps at the top of this post? (e.g. “gcc -o arduino-serial arduino-serial.c”)
It is likely you do not have the Mac OS X developer tools installed (it’s an optional install).
If that’s the case, here’s a compiled version of arduino-serial:
http://todbot.com/arduino/host/arduino-serial/arduino-serial-macosx.zip
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.
I’m trying to send a command to the arduino and get a response but I can’t seem to get it to work:
command:
./arduino-serial -b 9600 -p /dev/ttyUSB0 -d 2000 -s foo -rresponse:
read: foothe response is whatever I tried to send the arduino, my sketch sends different output depending on the command sent.
How can i send a int to the board?
Cheers, Henry
Hello,
I’m starting an Arduino Project in wich i need to communicate several Arduino boards with one PC via serial port. I know almost nothing about working with serial ports so I’m first learning about it.
Your code is being really useful for me. Now I’m starting to write a bit of code on my own but using yours as a refference for the “setting the serial connection” part since I’m still getting familiar with this.
I’ve seen that yor work is under a CC license but I’m not sure about the specifications. Do you allow derived works? Is it OK for you if I publish something of my code based on yours giving recognition to you?
Thanks,
Sakul.
[...] Arduino I found this page on the Arduino website and also another interesting website. (I did find this site about C and Arduino which might be useful in the future; and this one is I think about [...]
Hi
I am trying to use:-
laptop% ./arduino-serial -b 9600 -p /dev/ttyUSB0 -s get -r
I don’t get back what I expect. Is there an Arduino sketch I can download that is known to work with arduino-serial used in this way?
Regards
Hi, this is great and seems to work well for me so far.
I am wondering what the best way to recompile the data I receive into a string.
Basically I am using arduino-serial to send text that I want to display on a 16×2 LCD screen hooked up to my arduino.
If I use “/Applications/arduino-serial -b 9600 -p /dev/tty.usbmodem1d21 -s hello”
My arduino returns:
104
101
108
108
111
Which is the ascii characters for each of the letters.
I’m thinking I need some form of array that waits for the first character that is not -1 then stores each character until the next -1
I am pretty new to Arduino and this seems a little tricky.
Any advice?
Thanks.
Phil
After some experimentation I have it somewhat working.
However, I was expecting my final string to be “hello” but right now it looks like this.
hÿÿÿ
eÿÿÿ
lÿÿÿ
lÿÿÿ
oÿÿÿ
I’m guessing that there is some kind of carriage return or something being sent.
Here is my code.
Any ideas?
Thanks.
Phil
char val; // variable to store the data from the serial port
char buffer[5]; // string array
void setup() {
Serial.begin(9600); // connect to the serial port
}
void loop () {
val = Serial.read(); // read the serial port
if (val > -1) { // check for the start of valid data
buffer[0] = val; // put the first charcter into the buffer
for (int i=1; i < 4; i++) { // read 4 more characters
buffer[i] = Serial.read();
} // end loop
Serial.println(buffer);
} // end if
}
Hi Phil,
The problem is the Arduino is operating “too fast” for the serial input data stream so that “
Serial.read()” is returning -1 when you do yourbuffer[i] = Serial.read();. You need to check for -1 every time you do “Serial.read()“, unless you are absolutely sure Serial has all the data you need.One way to do this is to wait for the data at every character, like:
for( int i=1; i< 4; i++) { while( !Serial.available() ) ; // wait for data buffer[i] = Serial.read(); }But that technique will hang waiting for data if none comes.
If you know exactly how many bytes are being sent, though, the receiving gets easier. For your "hello" example, it can be:
void loop() { if( Serial.available >= 5 ) { // wait for at least 5 bytes for( int i=0; i<5; i++) { buffer[i] = Serial.read(); } Serial.println(buffer); } }I talk about this in Arduino serial protocol design patterns. Receiving and parsing data on the Arduino is currently a bit messy if the data isn’t a fixed length in size. This will get better soon, as an Arduino update is coming that will have ways of receiving until a newline or other arbitrary character.