167 struct termios options;
169 if (tcgetattr(fd_, &options) == -1) {
174 options.c_cflag |= (tcflag_t) (CLOCAL | CREAD);
175 options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL |
178 options.c_oflag &= (tcflag_t) ~(OPOST);
179 options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
181 options.c_iflag &= (tcflag_t) ~IUCLC;
184 options.c_iflag &= (tcflag_t) ~PARMRK;
188 bool custom_baud =
false;
192 case 0: baud = B0;
break;
195 case 50: baud = B50;
break;
198 case 75: baud = B75;
break;
201 case 110: baud = B110;
break;
204 case 134: baud = B134;
break;
207 case 150: baud = B150;
break;
210 case 200: baud = B200;
break;
213 case 300: baud = B300;
break;
216 case 600: baud = B600;
break;
219 case 1200: baud = B1200;
break;
222 case 1800: baud = B1800;
break;
225 case 2400: baud = B2400;
break;
228 case 4800: baud = B4800;
break;
231 case 7200: baud = B7200;
break;
234 case 9600: baud = B9600;
break;
237 case 14400: baud = B14400;
break;
240 case 19200: baud = B19200;
break;
243 case 28800: baud = B28800;
break;
246 case 57600: baud = B57600;
break;
249 case 76800: baud = B76800;
break;
252 case 38400: baud = B38400;
break;
255 case 115200: baud = B115200;
break;
258 case 128000: baud = B128000;
break;
261 case 153600: baud = B153600;
break;
264 case 230400: baud = B230400;
break;
267 case 256000: baud = B256000;
break;
270 case 460800: baud = B460800;
break;
273 case 576000: baud = B576000;
break;
276 case 921600: baud = B921600;
break;
279 case 1000000: baud = B1000000;
break;
282 case 1152000: baud = B1152000;
break;
285 case 1500000: baud = B1500000;
break;
288 case 2000000: baud = B2000000;
break;
291 case 2500000: baud = B2500000;
break;
294 case 3000000: baud = B3000000;
break;
297 case 3500000: baud = B3500000;
break;
300 case 4000000: baud = B4000000;
break;
305 #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) 310 speed_t new_baud =
static_cast<speed_t
> (baudrate_);
311 if (-1 == ioctl (fd_, IOSSIOSPEED, &new_baud, 1)) {
315 #elif defined(__linux__) && defined (TIOCSSERIAL) 316 struct serial_struct ser;
318 if (-1 == ioctl (fd_, TIOCGSERIAL, &ser)) {
323 ser.custom_divisor = ser.baud_base /
static_cast<int> (baudrate_);
325 ser.flags &= ~ASYNC_SPD_MASK;
326 ser.flags |= ASYNC_SPD_CUST;
328 if (-1 == ioctl (fd_, TIOCSSERIAL, &ser)) {
332 throw invalid_argument (
"OS does not currently support custom bauds");
335 if (custom_baud ==
false) {
337 ::cfsetspeed(&options, baud);
339 ::cfsetispeed(&options, baud);
340 ::cfsetospeed(&options, baud);
345 options.c_cflag &= (tcflag_t) ~CSIZE;
347 options.c_cflag |= CS8;
349 options.c_cflag |= CS7;
351 options.c_cflag |= CS6;
353 options.c_cflag |= CS5;
355 throw invalid_argument (
"invalid char len");
358 options.c_cflag &= (tcflag_t) ~(CSTOPB);
361 options.c_cflag |= (CSTOPB);
363 options.c_cflag |= (CSTOPB);
365 throw invalid_argument (
"invalid stop bit");
367 options.c_iflag &= (tcflag_t) ~(INPCK | ISTRIP);
369 options.c_cflag &= (tcflag_t) ~(PARENB | PARODD);
371 options.c_cflag &= (tcflag_t) ~(PARODD);
372 options.c_cflag |= (PARENB);
374 options.c_cflag |= (PARENB | PARODD);
378 options.c_cflag |= (PARENB | CMSPAR | PARODD);
381 options.c_cflag |= (PARENB | CMSPAR);
382 options.c_cflag &= (tcflag_t) ~(PARODD);
387 throw invalid_argument (
"OS does not support mark or space parity");
389 #endif // ifdef CMSPAR 391 throw invalid_argument (
"invalid parity");
409 options.c_iflag |= (IXON | IXOFF);
411 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
414 options.c_iflag |= (IXON | IXOFF);
416 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF);
421 options.c_cflag |= (CRTSCTS);
423 options.c_cflag &= (
unsigned long) ~(CRTSCTS);
424 #elif defined CNEW_RTSCTS 426 options.c_cflag |= (CNEW_RTSCTS);
428 options.c_cflag &= (
unsigned long) ~(CNEW_RTSCTS);
430 #error "OS Support seems wrong." 437 options.c_cc[VMIN] = 0;
438 options.c_cc[VTIME] = 0;
441 ::tcsetattr (fd_, TCSANOW, &options);
444 uint32_t bit_time_ns = 1e9 / baudrate_;
445 byte_time_ns_ = bit_time_ns * (1 + bytesize_ + parity_ + stopbits_);
#define THROW(exceptionClass, message)
Definition: serial.h:48