]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/dtrrts.c
Add when the graps were generated
[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 #include <stdio.h>
45 #include <stdlib.h>
46
47 /* we need a termios structure to clear the HUPCL bit */
48 struct termios tio;
49
50 int main(int argc, char *argv[])
51 {
52   int fd;
53   int status;
54
55   if (argc != 4)
56   {
57     printf("Usage: setSerialSignal port                  DTR RTS\n");
58     printf("Usage: setSerialSignal /dev/ttyS0|/dev/ttyS1 0|1 0|1\n");
59     exit( 1 );
60   }
61
62   if ((fd = open(argv[1],O_RDWR)) < 0)
63   {
64     printf("Couldn't open %s\n",argv[1]);
65     exit(1);
66   }
67   tcgetattr(fd, &tio);          /* get the termio information */
68   tio.c_cflag &= ~HUPCL;        /* clear the HUPCL bit */
69   tcsetattr(fd, TCSANOW, &tio); /* set the termio information */
70
71   ioctl(fd, TIOCMGET, &status); /* get the serial port status */
72
73   if ( argv[2][0] == '1' )      /* set the DTR line */
74     status &= ~TIOCM_DTR;
75   else
76     status |= TIOCM_DTR;
77
78   if ( argv[3][0] == '1' )      /* set the RTS line */
79     status &= ~TIOCM_RTS;
80   else
81     status |= TIOCM_RTS;
82
83   ioctl(fd, TIOCMSET, &status); /* set the serial port status */
84
85   close(fd);                    /* close the device file */
86 }