33 #include <sys/ioctl.h> 38 #include <linux/serial.h> 41 #include <boost/lexical_cast.hpp> 51 low_latency_mode(false),
68 flow_control(flow_control),
69 low_latency_mode(low_latency_mode),
101 error_msg_ =
"Invalid baud rate: " + boost::lexical_cast<std::string>(config.
baud);
125 fd_ = open(device.c_str(), config.
writable ? O_RDWR : O_RDONLY);
128 error_msg_ =
"Error opening serial port <" + device +
">: " + strerror(errno);
133 if (tcgetattr(
fd_, &term) < 0)
135 error_msg_ =
"Unable to set serial attributes <" + device +
">: " + strerror(errno);
144 term.c_cflag |= CSTOPB;
148 term.c_cflag &= ~CSTOPB;
153 term.c_cflag |= PARENB;
154 term.c_cflag &= ~PARODD;
158 term.c_cflag |= PARENB;
159 term.c_cflag |= PARODD;
163 term.c_cflag &= ~PARENB;
164 term.c_cflag &= ~PARODD;
169 term.c_cflag &= ~CSIZE;
174 term.c_cflag &= ~CSIZE;
178 if (cfsetspeed(&term, config.
baud) < 0)
180 error_msg_ =
"Invalid baud rate: " + boost::lexical_cast<std::string>(config.
baud);
185 if (tcsetattr(
fd_, TCSAFLUSH, &term) < 0)
187 error_msg_ =
"Unable to set serial port attributes <" + device +
">: " + strerror(errno);
209 struct serial_struct serial_info;
211 if (ioctl(
fd_, TIOCGSERIAL, &serial_info) < 0)
213 error_msg_ =
"Failed to set low latency mode. Cannot get serial configuration: " + std::string(strerror(errno));
217 serial_info.flags |= ASYNC_LOW_LATENCY;
219 if (ioctl(
fd_, TIOCSSERIAL, &serial_info) < 0)
221 error_msg_ =
"Failed to set low latency mode. Cannot set serial configuration: " + std::string(strerror(errno));
232 if (baud == B50 || baud == 50)
236 else if (baud == B75 || baud == 75)
240 else if (baud == B110 || baud == 110)
244 else if (baud == B134 || baud == 134)
248 else if (baud == B150 || baud == 150)
252 else if (baud == B200 || baud == 200)
256 else if (baud == B300 || baud == 300)
260 else if (baud == B600 || baud == 600)
264 else if (baud == B1200 || baud == 1200)
268 else if (baud == B1800 || baud == 1800)
272 else if (baud == B2400 || baud == 2400)
276 else if (baud == B4800 || baud == 4800)
280 else if (baud == B9600 || baud == 9600)
284 else if (baud == B19200 || baud == 19200)
288 else if (baud == B38400 || baud == 38400)
292 else if (baud == B57600 || baud == 57600)
296 else if (baud == B115200 || baud == 115200)
300 else if (baud == B230400 || baud == 230400)
304 else if (baud == B460800 || baud == 460800)
308 else if (baud == B576000 || baud == 576000)
312 else if (baud == B921600 || baud == 921600)
316 else if (baud == B1000000 || baud == 1000000)
320 else if (baud == B1152000 || baud == 1152000)
324 else if (baud == B1500000 || baud == 1500000)
328 else if (baud == B2000000 || baud == 2000000)
332 else if (baud == B2500000 || baud == 2500000)
336 else if (baud == B3000000 || baud == 3000000)
340 else if (baud == B3500000 || baud == 3500000)
344 else if (baud == B4000000 || baud == 4000000)
360 struct pollfd fds[1];
362 fds[0].events = POLLIN;
364 int poll_return = poll(fds, 1, timeout);
365 if (poll_return == 0)
367 error_msg_ =
"Timed out while waiting for data.";
370 else if (poll_return < 0)
372 int error_num = errno;
378 error_msg_ =
"Error polling serial port: " + std::string(strerror(errno));
383 size_t to_read = max_bytes;
387 ioctl(
fd_, FIONREAD, &bytes);
390 error_msg_ =
"Error getting number of available bytes from serial port: " + std::string(strerror(errno));
393 to_read =
static_cast<size_t>(bytes);
396 size_t output_size = output.size();
397 output.resize(output_size + to_read);
399 int result = read(
fd_, output.data() + output_size, to_read);
403 output.resize(output_size + result);
407 output.resize(output_size);
414 else if (result == 0)
420 int error_num = errno;
427 error_msg_ =
"Error reading serial port: " + std::string(strerror(errno));
435 return write(
fd_, input.data(), input.size());
virtual bool Open(const std::string &device, SerialConfig config=SerialConfig())
virtual int32_t Write(const std::vector< uint8_t > &input)
virtual Result ReadBytes(std::vector< uint8_t > &output, size_t max_bytes, int32_t timeout)
int32_t ParseBaudRate(int32_t baud)