Difference between revisions of "CSC231 C-Program for Arduino connection"

From dftwiki3
Jump to: navigation, search
(Update 26 Dec 2007:)
 
(One intermediate revision by the same user not shown)
Line 36: Line 36:
 
  laptop% ./arduino-serial -b 9600 -p /dev/tty.usbserial -s 6
 
  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 [[CSC231_Serial_Red_Blink.pde |serial_read_blink.pde]] sketch from Spooky Arduino.
+
This would cause the Arduino to blink 6 times if you’re using the [[CSC231_Serial_Red_Blink.pde |serial_read_blink.pde]] sketch from [http://todbot.com/blog/spookyarduino/ Spooky Arduino].
  
 
Send the string “furby” to Arduino
 
Send the string “furby” to Arduino
Line 47: Line 47:
 
  read: 15 Hello world!
 
  read: 15 Hello world!
  
The output is what you would expect if you were running the serial_hello_world.pde sketch from Spooky Arduino.
+
The output is what you would expect if you were running the [[CSC231 Serial_Hello_World.pde | serial_hello_world.pde]] sketch from [http://todbot.com/blog/spookyarduino/ Spooky Arduino].
 +
 
  
 
Send ASCII string “get” to Arduino and receive result
 
Send ASCII string “get” to Arduino and receive result
Line 53: Line 54:
 
   laptop% ./arduino-serial -b 9600 -p /dev/cu.usbserial -s get -r
 
   laptop% ./arduino-serial -b 9600 -p /dev/cu.usbserial -s get -r
 
  read: d=0
 
  read: d=0
 +
 
=Internals=
 
=Internals=
  
Line 75: Line 77:
  
 
Posted by todbot on Wednesday, December 6th, 2006 at 8:13 pm.
 
Posted by todbot on Wednesday, December 6th, 2006 at 8:13 pm.
 +
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC231]][[Category:Arduino]]

Latest revision as of 18:28, 8 October 2010

This is taken from http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/


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.

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 descriptor
  • int 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.

Posted by todbot on Wednesday, December 6th, 2006 at 8:13 pm.