39 #include <visp3/core/vpConfig.h> 48 #include <sys/types.h> 49 #include <sys/ioctl.h> 54 #include <visp3/core/vpSerial.h> 55 #include <visp3/core/vpException.h> 59 # define TIOCINQ FIONREAD 61 # define TIOCINQ 0x541B 103 : m_port(port), m_fd(-1), m_is_open(false), m_xonxoff(false), m_rtscts(false),
104 m_baudrate(baudrate), m_parity(parity),
105 m_bytesize(bytesize), m_stopbits(stopbits), m_flowcontrol(flowcontrol)
107 if (m_port.empty () ==
false)
125 m_baudrate = baudrate;
134 m_bytesize = bytesize;
143 m_flowcontrol = flowcontrol;
161 m_stopbits = stopbits;
195 if (-1 == ioctl (m_fd, TIOCINQ, &count)) {
208 if (m_is_open ==
true) {
251 if (m_port.empty ()) {
254 if (m_is_open ==
true) {
258 m_fd =
::open (m_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
286 if (m_is_open ==
false) {
291 struct timeval timeout = {timeout_s, 0};
294 FD_SET(static_cast<unsigned int>(m_fd), &readfds);
296 int ret = select(FD_SETSIZE, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
306 ssize_t n =
::read(m_fd, c, 1);
321 size_t read_so_far = 0;
322 size_t eol_len = eol.length ();
326 size_t bytes_read = this->
read(&c, 1);
327 read_so_far += bytes_read;
328 if (bytes_read == 0) {
332 if (std::string(line, line.size() - eol_len, eol_len) == eol) {
345 if (m_is_open ==
false) {
349 ssize_t r =
::write(m_fd, s.c_str(), s.size());
350 if (r != (ssize_t)(s.size())) {
355 void vpSerial::configure()
361 struct termios options;
363 if (tcgetattr(m_fd, &options) == -1) {
369 options.c_cflag |= (tcflag_t) (CLOCAL | CREAD);
370 options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL |
373 options.c_oflag &= (tcflag_t) ~(OPOST);
374 options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
377 options.c_iflag &= (tcflag_t) ~IUCLC;
380 options.c_iflag &= (tcflag_t) ~PARMRK;
386 case 0: baudrate = B0;
break;
389 case 50: baudrate = B50;
break;
392 case 75: baudrate = B75;
break;
395 case 110: baudrate = B110;
break;
398 case 134: baudrate = B134;
break;
401 case 150: baudrate = B150;
break;
404 case 200: baudrate = B200;
break;
407 case 300: baudrate = B300;
break;
410 case 600: baudrate = B600;
break;
413 case 1200: baudrate = B1200;
break;
416 case 1800: baudrate = B1800;
break;
419 case 2400: baudrate = B2400;
break;
422 case 4800: baudrate = B4800;
break;
425 case 9600: baudrate = B9600;
break;
428 case 14400: baudrate = B14400;
break;
431 case 19200: baudrate = B19200;
break;
434 case 38400: baudrate = B38400;
break;
437 case 57600: baudrate = B57600;
break;
440 case 115200: baudrate = B115200;
break;
443 case 230400: baudrate = B230400;
break;
446 case 460800: baudrate = B460800;
break;
449 case 500000: baudrate = B500000;
break;
452 case 576000: baudrate = B576000;
break;
455 case 921600: baudrate = B921600;
break;
458 case 1000000: baudrate = B1000000;
break;
461 case 1152000: baudrate = B1152000;
break;
464 case 1500000: baudrate = B1500000;
break;
467 case 2000000: baudrate = B2000000;
break;
470 case 2500000: baudrate = B2500000;
break;
473 case 3000000: baudrate = B3000000;
break;
476 case 3500000: baudrate = B3500000;
break;
479 case 4000000: baudrate = B4000000;
break;
486 ::cfsetspeed(&options, baudrate);
488 ::cfsetispeed(&options, baudrate);
489 ::cfsetospeed(&options, baudrate);
493 options.c_cflag &= (tcflag_t) ~CSIZE;
495 case eightbits: options.c_cflag |= CS8;
break;
496 case sevenbits: options.c_cflag |= CS7;
break;
497 case sixbits: options.c_cflag |= CS6;
break;
498 case fivebits: options.c_cflag |= CS5;
break;
502 case stopbits_one: options.c_cflag &= (tcflag_t) ~(CSTOPB);
break;
507 options.c_iflag &= (tcflag_t) ~(INPCK | ISTRIP);
510 options.c_cflag &= (tcflag_t) ~(PARENB | PARODD);
break;
512 options.c_cflag &= (tcflag_t) ~(PARODD);
513 options.c_cflag |= (PARENB);
break;
515 options.c_cflag |= (PARENB | PARODD);
break;
519 switch(m_flowcontrol) {
536 options.c_iflag |= (IXON | IXOFF);
539 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
541 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF);
547 options.c_cflag |= (CRTSCTS);
549 options.c_cflag &= (
unsigned long) ~(CRTSCTS);
550 #elif defined CNEW_RTSCTS 552 options.c_cflag |= (CNEW_RTSCTS);
554 options.c_cflag &= (
unsigned long) ~(CNEW_RTSCTS);
556 #error "OS doesn't support serial rtscts" 559 options.c_cc[VMIN] = 0;
560 options.c_cc[VTIME] = 0;
563 ::tcsetattr (m_fd, TCSANOW, &options);
566 #elif !defined(VISP_BUILD_SHARED_LIBS) 568 void dummy_vpSerial(){};
Data is encoded with 6 bits.
vpSerial(const std::string &port="", unsigned long baudrate=9600, bytesize_t bytesize=eightbits, parity_t parity=parity_none, stopbits_t stopbits=stopbits_one, flowcontrol_t flowcontrol=flowcontrol_none)
Data is encoded with 8 bits.
error that can be emited by ViSP classes.
void write(const std::string &s)
std::string readline(const std::string &eol)
void setBaudrate(const unsigned long baudrate)
void setFlowcontrol(const flowcontrol_t &flowcontrol)
Data is encoded with 5 bits.
bool read(char *c, long timeout_s)
void setPort(const std::string &port)
void setStopbits(const stopbits_t &stopbits)
Data is encoded with 7 bits.
void setParity(const parity_t &parity)
void setBytesize(const bytesize_t &bytesize)