user_uart.cpp
Go to the documentation of this file.
1 //
2 // The MIT License (MIT)
3 //
4 // Copyright (c) 2019 Livox. All rights reserved.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 
25 #include "user_uart.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <termios.h>
32 #include <unistd.h>
33 
34 namespace livox_ros {
35 
36 UserUart::UserUart(uint8_t baudrate_index, uint8_t parity)
37  : baudrate_(baudrate_index), parity_(parity) {
38  fd_ = 0;
39  is_open_ = false;
40 }
41 
43  is_open_ = false;
44  if (fd_ > 0) {
46  tcflush(fd_, TCOFLUSH);
47  tcflush(fd_, TCIFLUSH);
48 
49  close(fd_);
50  }
51 }
52 
53 int UserUart::Open(const char *filename) {
54  fd_ = open(filename, O_RDWR | O_NOCTTY); //| O_NDELAY
55  if (fd_ < 0) {
56  printf("Open %s fail!\n", filename);
57  return -1;
58  } else {
59  chmod(filename, S_IRWXU | S_IRWXG | S_IRWXO); /* need add here */
60  printf("Open %s success!\n", filename);
61  }
62 
63  if (fd_ > 0) {
65  if (Setup(baudrate_, parity_)) {
66  return -1;
67  }
68  }
69 
70  is_open_ = true;
71  return 0;
72 }
73 
75  is_open_ = false;
76  if (fd_ > 0) {
78  tcflush(fd_, TCOFLUSH);
79  tcflush(fd_, TCIFLUSH);
80  return close(fd_);
81  }
82 
83  return -1;
84 }
85 
87 int UserUart::Setup(uint8_t baudrate_index, uint8_t parity) {
88  static uint32_t baud_map[19] = {
89  B2400, B4800, B9600, B19200, B38400, B57600, B115200,
90  B230400, B460800, B500000, B576000, B921600, B1152000, B1500000,
91  B2000000, B2500000, B3000000, B3500000, B4000000};
92  tcflag_t baudrate;
93  struct termios options;
94 
95  if ((baudrate_index > BR4000000) || (parity > P_7S1)) {
96  return -1;
97  }
98 
100  tcgetattr(fd_, &options);
101  memset(&options, 0, sizeof(options));
102  tcflush(fd_, TCIOFLUSH);
103  tcsetattr(fd_, TCSANOW, &options);
104  usleep(10000);
105 
107  options.c_cflag |= (CLOCAL | CREAD);
108 
110  // options.c_cflag &= ~CRTSCTS;
111 
113  // options.c_iflag &= ~(IXON | IXOFF | IXANY);
114 
115  // options.c_oflag &= ~OPOST;
116 
118  options.c_cflag &= ~CBAUD;
119  baudrate = baud_map[baudrate_index];
120  options.c_cflag |= baudrate;
121 
122  switch (parity) {
123  case P_8N1:
125  options.c_cflag &= ~PARENB;
126  options.c_cflag &= ~CSTOPB;
127  options.c_cflag &= ~CSIZE;
128  options.c_cflag |= CS8;
129  break;
130  case P_7E1:
132  options.c_cflag |= PARENB;
133  options.c_cflag &= ~PARODD;
134  options.c_cflag &= ~CSTOPB;
135  options.c_cflag &= ~CSIZE;
136  options.c_cflag |= CS7;
137  break;
138  case P_7O1:
140  options.c_cflag |= PARENB;
141  options.c_cflag |= PARODD;
142  options.c_cflag &= ~CSTOPB;
143  options.c_cflag &= ~CSIZE;
144  options.c_cflag |= CS7;
145  break;
146  case P_7S1:
148  options.c_cflag &= ~PARENB;
149  options.c_cflag &= ~CSTOPB;
150  options.c_cflag &= ~CSIZE;
151  options.c_cflag |= CS8;
152  break;
153  default:
154  return -1;
155  }
156 
158  options.c_iflag &= ~INPCK;
159 
161  // options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
162 
164  options.c_cc[VTIME] = 1;
165 
167  options.c_cc[VMIN] = 1;
168 
170  tcflush(fd_, TCIOFLUSH);
171 
173  tcsetattr(fd_, TCSANOW, &options);
174 
175  return 0;
176 }
177 
178 ssize_t UserUart::Write(const char *buffer, size_t size) {
179  if (fd_ > 0) {
180  return write(fd_, buffer, size);
181  } else {
182  return 0;
183  }
184 }
185 
186 ssize_t UserUart::Read(char *buffer, size_t size) {
187  if (fd_ > 0) {
188  return read(fd_, buffer, size);
189  } else {
190  return 0;
191  }
192 }
193 
194 } // namespace livox_ros
unsigned char uint8_t
Definition: stdint.h:125
int Open(const char *filename)
Definition: user_uart.cpp:53
unsigned int uint32_t
Definition: stdint.h:127
ssize_t Read(char *buffer, size_t size)
Definition: user_uart.cpp:186
int Setup(uint8_t baudrate_index, uint8_t parity)
Definition: user_uart.cpp:87
volatile bool is_open_
Definition: user_uart.h:82
UserUart(uint8_t baudrate_index, uint8_t parity)
Definition: user_uart.cpp:36
ssize_t Write(const char *buffer, size_t size)
Definition: user_uart.cpp:178


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46