Preface: In Part 1 I covered the Arduino sketch that will output serial data to read on the PC it’s connected to. Next I’ll go over the resources I used to learn about terminal programming, and the only example I could find online of communicating with an Arduino in C.
Part 2: Information Resources
Serial communication in a Unix environment is handled through the terminal interface. This really threw me for a loop when I started researching this because I had figured reading and writing to a serial port would have its own separate set of functions unique to serial port interfacing. The “terminal” is the command line, right?
Well, it’s complicated. My keyboard is a device that sends characters into the computer to be interpreted and acted on. So is a printer. So is a modem. So is a device hooked up to the serial port (which at one time could be a printer/modem/keyboard/mouse). When you think about the long history you can forgive that the terminal interface became a bit of a catch-all over the years. The terminal I/O chapter of Advanced Programming in the Unix Environment starts off with “The handling of terminal I/O is a messy area, regardless of the operating system”.
My point is this – doing a search for how to read/write serial port data is going to give you a ton of results, and most aren’t going to be even remotely relevant to reading serial data from an Arduino.
There is one exception – Tod Kurt’s blog has an excellent write-up and fantastic example program for reading and writing serial data to/from an Arduino. The only “problem” with it is that it’s very flexible, uses command line toggles to get information, and has lots of code dedicated to parsing the info passed from the command line. What I’m saying is that it’s a great, flexible program and a wonderful example, but it’s super hard to tease out the important bits for someone new to terminal programming (like me). That being said, it’s exactly what you need to make sure you can compile to verify that it’s possible to use the terminal I/O API yourself.
Download the example program and compile it…
>>gcc arduino-serial.c -o arduino-serial
then run it, passing parameters that make sense for the sketch the Arduino is running…
>>./arduino-serial -b 9800 -p /dev/ttyACM0 -d 1500 -r
The passed parameters “-b 9800″ means 9800 baud (which we defined in the sketch), “-p /dev/ttyACM0″ sets the serial port to /dev/ttyACM0 (might be different for you), “-d 1500″ means wait 1.5 seconds, and “-r” means read the serial data.
I’ll go into this more in the next part, but the delay is important. It provides a brief moment for the Arduino to reboot and start sending data.
If all goes well you should see the output “read: Hello World”. If the compile fails, it means you’ll have to figure out what libraries are missing on your machine. I don’t recall having to do anything special on mine (Ubuntu 12.04).
I recommend reading through the program Tod wrote. If you’re familiar enough with C and Unix programming it might be enough of an explanation for how to read/write serial data to an Arduino. The only reason it wasn’t enough for me is that I had a hard time seeing past all the code to parse the command line parameters, and didn’t know what all of the structures in the initialization function were doing.
Here are two other resources I’ve been using while learning about serial communication:
In part 3 I’ll go through writing (and explaining) a small program that uses the bare minimum to read “hello world” from the Arduino.