Resources »Controlling devices with the serial port and writing graphical interfaces
View document on multiple pages.
1. IntroductionThe serial port found on the back of most PCs is extremely useful
for robotics work. A variety of devices are configured to communicate
via a serial port, and you can use it to debug or control your own
projects. This project shows you how to use free tools to write
programs that use your serial port. We’ll show you how to make your
programs compatible with both Microsoft Windows and Linux, and
present a method for creating simple graphical interfaces. We will
assume you know a bit of C and are comfortable installing software on
your computer. Knowing Linux commands like 2. MaterialsHere’s what you’ll need to follow along:
3. Installing Cygwin (Windows only)Cygwin is a set of programs for Windows that gives you an environment very similar to linux. To install it, go to the Cygwin website and download the installer by clicking “Install cygwin now”. You will get a program called
Finish installing all of the software, and run the Cygwin shell. To make sure you have everything, try running “gcc”, “make”, and “wish”. Messages other than “no such command” should appear. All of the cygwin files can be found from Windows in the default directory 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
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
write(s->fd,buf,5); In this case, 5. Adding a GUI with Tcl/TkTcl/Tk is the most convenient way to create GUIs that will work on both Windows and Linux. Even if you only use one system, you will benefit from the fact that Tcl/Tk is quite easy to learn and used by a lot of people. The particular mix of Tcl/Tk and C that we recommend here was the best solution we found; if you know of other ways to do it, let us know! Now, take a look at
scale .servo$i.scale -width 20 -length 200 -orient vertical
-from 500 -to 5500 -command "stepTo 0 $i " -showvalue 0
This creates a slider with values from 500 to 5500, so that whenever you adjust the slider, the command stepTo 0 slider position will be run, with slider and position replaced by
the slider number and position. This command is defined in Tcl_CreateObjCommand(interp, "stepTo", &stepToProc, NULL, NULL); The arguments are interpreted by the C function
6. Compiling and running the program
Instructions for compilation are defined in the wish ssc-tester.tcl and a window with 16 sliders should appear. Connect the servo controller and a servo, then try moving the sliders to see if it works! 7. ConclusionA combination of C and Tcl/Tk is an easy and powerful way to make graphical controls for your devices. Tcl/Tk gives you a flexible set of commands for creating the graphical interface, while C lets you execute the low-level commands necessary to get at the serial port. This technique is good for making a wide variety of computer programs, for robotics and other things. Personally, we have been using four variations of it: the one you saw in this demo, programs for interfacing with a stepper motor controller and a nanometer-precision piezo controller, and even a jukebox for playing music from our mp3 collection! We hope you find it as useful as we have. |
|
Log In
|
Wish Lists
|
BIG Order Form
|
Shopping Cart
US toll free: 1-877-7-POLOLU ~
(702) 262-6648 |
|||||||||||||
| Catalog | Forum | Resources | Distributors | Ordering | About | Contact | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|