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:


  write(s->fd,buf,5);

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.