2 * setSerialSignal v0.1 9/13/01
3 * www.embeddedlinuxinterfacing.com
6 * The original location of this source is
7 * http://www.embeddedlinuxinterfacing.com/chapters/06/setSerialSignal.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Library General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * setSerialSignal sets the DTR and RTS serial port control signals.
26 * This program queries the serial port status then sets or clears
27 * the DTR or RTS bits based on user supplied command line setting.
29 * setSerialSignal clears the HUPCL bit. With the HUPCL bit set,
30 * when you close the serial port, the Linux serial port driver
31 * will drop DTR (assertion level 1, negative RS-232 voltage). By
32 * clearing the HUPCL bit, the serial port driver leaves the
33 * assertion level of DTR alone when the port is closed.
37 gcc -o setSerialSignal setSerialSignal.c
41 #include <sys/ioctl.h>
45 /* we need a termios structure to clear the HUPCL bit */
48 int main(int argc, char *argv[])
55 printf("Usage: setSerialSignal port DTR RTS\n");
56 printf("Usage: setSerialSignal /dev/ttyS0|/dev/ttyS1 0|1 0|1\n");
60 if ((fd = open(argv[1],O_RDWR)) < 0)
62 printf("Couldn't open %s\n",argv[1]);
65 tcgetattr(fd, &tio); /* get the termio information */
66 tio.c_cflag &= ~HUPCL; /* clear the HUPCL bit */
67 tcsetattr(fd, TCSANOW, &tio); /* set the termio information */
69 ioctl(fd, TIOCMGET, &status); /* get the serial port status */
71 if ( argv[2][0] == '1' ) /* set the DTR line */
76 if ( argv[3][0] == '1' ) /* set the RTS line */
81 ioctl(fd, TIOCMSET, &status); /* set the serial port status */
83 close(fd); /* close the device file */