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.

New Products

9V, 2.5A Step-Up/Step-Down Voltage Regulator S13V25F9
5V, 3.4A Step-Down Voltage Regulator D30V30F5
VL53L7CX Time-of-Flight 8×8-Zone Wide FOV Distance Sensor Carrier with Voltage Regulator, 350cm Max
Free Circuit Cellar magazine March 2023
ACS724 Current Sensor Carrier -2.5A to +2.5A
Motoron M3S550 Triple Motor Controller Shield Kit for Arduino
Motoron M2T550 Dual I²C Motor Controller (Header Pins Soldered)
Motoron M1U550 Single Serial Motor Controller
Motoron M1T550 Single I²C Motor Controller
12V, 2.5A Step-Up/Step-Down Voltage Regulator S13V25F12
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors