comparison miscutil/openport.c @ 434:3822f3b198d4

fc-serterm: written
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 22 Jun 2014 01:21:10 +0000
parents
children
comparison
equal deleted inserted replaced
433:2d8ab1b0df8d 434:3822f3b198d4
1 /*
2 * Serial port opening code for fc-serterm
3 */
4
5 #include <sys/types.h>
6 #include <sys/file.h>
7 #include <sys/ioctl.h>
8 #include <termios.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <unistd.h>
14
15 extern int target_fd;
16
17 static struct baudrate {
18 char *name;
19 speed_t termios_code;
20 } baud_rate_table[] = {
21 {"19200", B19200},
22 {"38400", B38400},
23 {"57600", B57600},
24 {"115200", B115200},
25 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
26 {"203125", B230400},
27 {"406250", B460800},
28 {"812500", B921600},
29 /* table search terminator */
30 {NULL, B0}
31 };
32
33 open_target_serial(ttydev, baudname)
34 char *ttydev, *baudname;
35 {
36 struct termios target_termios;
37 struct baudrate *br;
38
39 for (br = baud_rate_table; br->name; br++)
40 if (!strcmp(br->name, baudname))
41 break;
42 if (!br->name) {
43 fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n",
44 baudname);
45 exit(1);
46 }
47 target_fd = open(ttydev, O_RDWR|O_NONBLOCK);
48 if (target_fd < 0) {
49 perror(ttydev);
50 exit(1);
51 }
52 target_termios.c_iflag = IGNBRK;
53 target_termios.c_oflag = 0;
54 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
55 target_termios.c_lflag = 0;
56 target_termios.c_cc[VMIN] = 1;
57 target_termios.c_cc[VTIME] = 0;
58 cfsetispeed(&target_termios, br->termios_code);
59 cfsetospeed(&target_termios, br->termios_code);
60 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
61 perror("initial tcsetattr on target");
62 exit(1);
63 }
64 return 0;
65 }