Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
serial.c
Go to the documentation of this file.
1 /*
2  * OpenUAS OBC
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include <termios.h>
10 #include <unistd.h> /* UNIX standard function definitions */
11 #include <fcntl.h> /* File control definitions */
12 #include <errno.h> /* Error number definitions */
13 
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <termios.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/termios.h>
20 #include <sys/ioctl.h>
21 
22 /****************************************************************************/
23 /* Open serial device for requested protocoll */
24 /****************************************************************************/
25 
26 int fd; /* File descriptor for the port */
27 
28 int serial_init(char *port_name)
29 {
30  struct termios orig_termios, cur_termios;
31 
32  int br = B115200;
33 
34  fd = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
35 
36  if (fd == -1) {
37  printf("opening modem serial device : fd < 0\n");
38  return -1;
39  }
40 
41  if (tcgetattr(fd, &orig_termios)) {
42  printf("getting modem serial device attr\n");
43  return -2;
44  }
45  cur_termios = orig_termios;
46 
47  /* input modes - turn off input processing */
48  cur_termios.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
49  | ICRNL | IXON | IXANY | IXOFF | IMAXBEL);
50  /* pas IGNCR sinon il vire les 0x0D */
51  cur_termios.c_iflag |= BRKINT;
52 
53  /* output_flags - turn off output processing */
54  cur_termios.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
55 
56  /* control modes */
57  cur_termios.c_cflag &= ~(CSIZE | CSTOPB | CREAD | PARENB | PARODD | HUPCL | CLOCAL);
58  cur_termios.c_cflag |= CREAD | CS8 | CLOCAL;
59  cur_termios.c_cflag &= ~(CRTSCTS);
60 
61  /* local modes */
62  cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | FLUSHO | PENDIN);
63  cur_termios.c_lflag |= NOFLSH;
64 
65  if (cfsetspeed(&cur_termios, br)) {
66  printf("setting modem serial device speed\n");
67  return -3;
68  }
69 
70  if (tcsetattr(fd, TCSADRAIN, &cur_termios)) {
71  printf("setting modem serial device attr\n");
72  return -4;
73  }
74 
75  return 0;
76 }
77 
78 
#define B115200
Definition: uart_arch.h:48
int serial_init(char *port_name)
Definition: serial.c:28
int fd
Definition: serial.c:26