39 #include <visp3/core/vpConfig.h>
48 #include <sys/ioctl.h>
50 #include <sys/types.h>
54 #include <visp3/core/vpException.h>
55 #include <visp3/core/vpSerial.h>
59 #define TIOCINQ FIONREAD
61 #define TIOCINQ 0x541B
102 : m_port(port), m_fd(-1), m_is_open(false), m_xonxoff(false), m_rtscts(false), m_baudrate(baudrate), m_parity(parity),
103 m_bytesize(bytesize), m_stopbits(stopbits), m_flowcontrol(flowcontrol)
105 if (m_port.empty() ==
false)
172 if (-1 == ioctl(m_fd, TIOCINQ, &count)) {
185 if (m_is_open ==
true) {
228 if (m_port.empty()) {
231 if (m_is_open ==
true) {
235 m_fd =
::open(m_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
263 if (m_is_open ==
false) {
268 struct timeval timeout = {timeout_s, 0};
271 FD_SET(
static_cast<unsigned int>(m_fd), &readfds);
273 int ret = select(FD_SETSIZE, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
277 }
else if (ret == 0) {
281 ssize_t n =
::read(m_fd, c, 1);
296 size_t eol_len = eol.length();
300 size_t bytes_read = this->
read(&c, 1);
301 if (bytes_read == 0) {
305 if (std::string(line, line.size() - eol_len, eol_len) == eol) {
318 if (m_is_open ==
false) {
322 ssize_t r =
::write(m_fd, s.c_str(), s.size());
323 if (r != (ssize_t)(s.size())) {
328 void vpSerial::configure()
334 struct termios options;
336 if (tcgetattr(m_fd, &options) == -1) {
342 options.c_cflag |= (tcflag_t)(CLOCAL | CREAD);
343 options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN);
345 options.c_oflag &= (tcflag_t) ~(OPOST);
346 options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
349 options.c_iflag &= (tcflag_t)~IUCLC;
352 options.c_iflag &= (tcflag_t)~PARMRK;
356 switch (m_baudrate) {
522 ::cfsetspeed(&options, baudrate);
524 ::cfsetispeed(&options, baudrate);
525 ::cfsetospeed(&options, baudrate);
529 options.c_cflag &= (tcflag_t)~CSIZE;
530 switch (m_bytesize) {
532 options.c_cflag |= CS8;
535 options.c_cflag |= CS7;
538 options.c_cflag |= CS6;
541 options.c_cflag |= CS5;
545 switch (m_stopbits) {
547 options.c_cflag &= (tcflag_t) ~(CSTOPB);
550 options.c_cflag |= (CSTOPB);
555 options.c_iflag &= (tcflag_t) ~(INPCK | ISTRIP);
558 options.c_cflag &= (tcflag_t) ~(PARENB | PARODD);
561 options.c_cflag &= (tcflag_t) ~(PARODD);
562 options.c_cflag |= (PARENB);
565 options.c_cflag |= (PARENB | PARODD);
570 switch (m_flowcontrol) {
587 options.c_iflag |= (IXON | IXOFF);
590 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
592 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF);
598 options.c_cflag |= (CRTSCTS);
600 options.c_cflag &= (
unsigned long)~(CRTSCTS);
601 #elif defined CNEW_RTSCTS
603 options.c_cflag |= (CNEW_RTSCTS);
605 options.c_cflag &= (
unsigned long)~(CNEW_RTSCTS);
607 #error "OS doesn't support serial rtscts"
610 options.c_cc[VMIN] = 0;
611 options.c_cc[VTIME] = 0;
614 ::tcsetattr(m_fd, TCSANOW, &options);
617 #elif !defined(VISP_BUILD_SHARED_LIBS)
619 void dummy_vpSerial(){};
error that can be emited by ViSP classes.
std::string readline(const std::string &eol)
@ flowcontrol_software
Software flow control.
@ flowcontrol_none
No flow control.
@ flowcontrol_hardware
Hardware flow control.
void setParity(const parity_t &parity)
void setStopbits(const stopbits_t &stopbits)
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)
@ parity_odd
Check for odd parity.
@ parity_none
No parity check.
@ parity_even
Check for even parity.
@ stopbits_two
2 stop bits are used
@ stopbits_one
1 stop bit is used
void setFlowcontrol(const flowcontrol_t &flowcontrol)
bool read(char *c, long timeout_s)
void setBytesize(const bytesize_t &bytesize)
void write(const std::string &s)
void setPort(const std::string &port)
@ eightbits
Data is encoded with 8 bits.
@ fivebits
Data is encoded with 5 bits.
@ sevenbits
Data is encoded with 7 bits.
@ sixbits
Data is encoded with 6 bits.
void setBaudrate(const unsigned long baudrate)