]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/dtrrts.c
Added new kernel (3.4-rt) and configuration
[can-benchmark.git] / utils / dtrrts.c
1 /*
2  * setSerialSignal v0.1 9/13/01
3  * www.embeddedlinuxinterfacing.com
4  *
5  *
6  * The original location of this source is
7  * http://www.embeddedlinuxinterfacing.com/chapters/06/setSerialSignal.c
8  *
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.
13  *
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.
18  *
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
23  */
24 /* setSerialSignal
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.
28  *
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.
34  */
35
36 /*
37 gcc -o setSerialSignal setSerialSignal.c
38 */
39
40
41 #include <sys/ioctl.h>
42 #include <fcntl.h>
43 #include <termios.h>
44
45 /* we need a termios structure to clear the HUPCL bit */
46 struct termios tio;
47
48 int main(int argc, char *argv[])
49 {
50   int fd;
51   int status;
52
53   if (argc != 4)
54   {
55     printf("Usage: setSerialSignal port                  DTR RTS\n");
56     printf("Usage: setSerialSignal /dev/ttyS0|/dev/ttyS1 0|1 0|1\n");
57     exit( 1 );
58   }
59
60   if ((fd = open(argv[1],O_RDWR)) < 0)
61   {
62     printf("Couldn't open %s\n",argv[1]);
63     exit(1);
64   }
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 */
68
69   ioctl(fd, TIOCMGET, &status); /* get the serial port status */
70
71   if ( argv[2][0] == '1' )      /* set the DTR line */
72     status &= ~TIOCM_DTR;
73   else
74     status |= TIOCM_DTR;
75
76   if ( argv[3][0] == '1' )      /* set the RTS line */
77     status &= ~TIOCM_RTS;
78   else
79     status |= TIOCM_RTS;
80
81   ioctl(fd, TIOCMSET, &status); /* set the serial port status */
82
83   close(fd);                    /* close the device file */
84 }
85