238c0023dd802bd2558f83902315001ff775c368
[digitaldcpower] / ddcp-script-ttyinit.c
1 /* vim: set sw=8 ts=8 si et: */
2 /* Linux software to set the speed on the serial line 
3 * Written by Guido Socher 
4 * run this program like this:
5 * ttydevinit /dev/ttyUSB0 (for usb com1) and then use
6 * cat > /dev/ttyUSB0 to write or cat /dev/ttyUSB0 to read the answers
7 */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <termios.h>
14
15 int main(int argc, char *argv[])
16 {
17         struct termios portset;
18         char *device;
19         int fd;
20
21         if (argc != 2){
22                 printf("USAGE: ddcp-script-ttyinit /dev/ttyUSB0\n");
23                 printf("or     ddcp-script-ttyinit /dev/ttyUSB1\n");
24                 exit(0);
25         }
26         device=argv[1];
27
28         /* Set up io port correctly, and open it... */
29         fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
30         if (fd == -1) {
31                 fprintf(stderr, "ERROR: open for %s failed.\n",device);
32                 exit(1);
33         }
34         tcgetattr(fd, &portset);
35         cfmakeraw(&portset);
36         cfsetospeed(&portset, B9600); /* speed */
37         //cfsetospeed(&portset, B115200); /* speed */
38         //cfsetospeed(&portset, B19200); /* speed */
39         tcsetattr(fd, TCSANOW, &portset);
40         close(fd);
41         return(0);
42 }