Documents » Controlling devices with the serial port and writing graphical interfaces »4. Writing a program in CFirst we’ll check that your C compiler is working fine. Open your
favorite editor and make a file called
main() {
printf("hello, world!\n");
}
Now, from the same directory in the Cygwin/Linux shell, run the command make hello A program called 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: * We’ll discuss in detail what is going on here before we run the
program, starting with
s->fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);
The port /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, |
|
Home
|
Contact
|
About
|
Forum
|
US toll free: 1-877-7-POLOLU |