Sample code/C/Serial

From GumstixDocsWiki

Jump to: navigation, search

This page presents sertest, a really simple serial communications program.

The source

You can find the source and Makefile here:

   * sertest.c
   * Makefile

The Makefile assumes that you create a directory parallel to the gumstix-buildroot (i.e. on my system, gumstix-build root can be found at /home/dhylands/gumstix/gumstix-buildroot, so for the sertest example, I'd create /home/dhylands/gumstix/sertest).

Once you've copied sertest.c and Makefile into the sertest directory, type

make

and the sertest program will be built. Copy the sertest program onto the gumstix. The usage for sertest is as follows:

Usage: sertest [option(s)]
  Download a program via serial/i2c

  -b, --baud=baud   Set the baudrate used
  -d, --debug       Turn on debug output
  -h, --help        Display this message
  -p, --port=port   Set the I/O port
  -v, --verbose     Turn on verbose messages

To connect to a device on the STUART port at 38400 baud, you'd use the following command:

sertest -p ttyS2 -b 38400

Anything you type will be sent out the serial port and anything received will be printed. If you short the Tx and Rx leads on your serial cable, you should be able to see what you type.

Note: This requires that you've already installed buildroot, which creates the toolchain required to compile the code.


Troubleshooting

Some hardware requires a carriage return (0x0d) to function properly. This program sends line feed (0x0a) by default. Minicom and Hyperterm send carriage retun instead of line feed, so if your device works as expected when connected via Minicom or Hyperterm, but not using sertest then this may be your problem.

To make this mod add the following code before line 285 (where the program writes to the serial port):

        if ( ch == 0x0a )
            ch = 0x0d;

Reference

For detailed information on programming serial ports from C, check out the Serial Programming HOWTO

Personal tools