4. Writing a program in C

First we’ll check that your C compiler is working fine. Open your favorite editor and make a file called hello.c with the following contents:

main() {
  printf("hello, world!\n");

Now, from the same directory in the Cygwin/Linux shell, run the command

make hello

A program called hello (or hello.exe) should automatically be created. Run it by typing hello, and your message should be displayed.

If that worked, download ssc-tester.zip (3k zip), our sample project, and take a look at it. The program is divided into three main files:

*ssc.c contains functions for communicating over the serial port *ssctester.c contains “wrappers” for those functions that allow them to be accessed from the GUI *ssc-tester.tcl is the GUI. It is a Tcl/Tk script that displays a window with some sliders and buttons.

We’ll discuss in detail what is going on here before we run the program, starting with ssc.c. When the program starts, the first thing to run is the function connectSsc, which opens a connection to the serial port with the line

  s->fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);

The port /dev/ttyS1 in linux is called COM2 in Windows - if you would like to use COM1 instead, change it to

/dev/ttyS0. One problem with the serial port is that it can be very hard to tell the difference between a malfunctioning device and an incorrect port number, so make sure you choose the right one now. The cryptic commands below,

  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);

  options.c_cflag |= (CLOCAL | CREAD); /* enable */

  options.c_cflag &= ~PARENB; /* 8N1 */
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

simply set up the serial parameters. You’ll need to pick the right ones for your device, but for most devices, 8N1 is what you need. Make sure to set the right baud rate in the first two lines. A common option that must be set is flow control — for Xon/Xoff (software flow control) add this line:

  options.c_iflag |= (IXON | IXOFF | IXANY); /* Xon/Xoff */

and for RTS/CTS (hardware flow control) you will need this one:

  options.c_iflag |= CRTSCTS; /* RTS/CTS */

Devices might use any combination of the two types of flow control - refer to the device manual for details, and be prepared to experiment. For devices that can be controlled with textual commands, a terminal emulator such as Hyperterm (Windows) or Minicom (Linux) might be useful, because it will let you select communication parameters on the fly.

The actual commands are sent to the serial port in the

sendCommand function. Refer to the user’s guide for the 16-servo controller if you want to understand exactly what we’re sending here. The important thing is that you need to use the write command to write any data out to the port, like this:


In this case, buf is a single command containing five bytes. The third argument to write specifies the number of bytes, so make sure to adapt this correctly to your own device. Opening the port and writing data to it is the tricky part — once you understand that, go on to the next section and read about creating a GUI.

New Products

Magnetic Encoder Disc for Mini Plastic Gearmotors, OD 9.7 mm, ID 1.5 mm, 12 CPR (Bulk)
100:1 Metal Gearmotor 20Dx44L mm 6V with Extended Motor Shaft
156:1 Metal Gearmotor 20Dx44L mm 6V with Extended Motor Shaft
Romi 32U4 Control Board
RoboClaw 2x45A Motor Controller (V5D, pin header I/O)
Magnetic Encoder Disc for 20D mm Metal Gearmotors, OD 9.7 mm, ID 2.0 mm, 20 CPR (Bulk)
RoboClaw 2x15A Motor Controller (V5D)
63:1 Metal Gearmotor 20Dx43L mm 6V with Extended Motor Shaft
25:1 Metal Gearmotor 20Dx41L mm 6V with Extended Motor Shaft
Magnetic Encoder Pair Kit for 20D mm Metal Gearmotors, 20 CPR, 2.7-18V
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors