36 #include <visp3/core/vpConfig.h>
45 #include <sys/ioctl.h>
47 #include <sys/types.h>
51 #include <visp3/core/vpException.h>
52 #include <visp3/core/vpSerial.h>
56 #define TIOCINQ FIONREAD
58 #define TIOCINQ 0x541B
99 : m_port(port), m_fd(-1), m_is_open(false), m_xonxoff(false), m_rtscts(false), m_baudrate(baudrate), m_parity(parity),
100 m_bytesize(bytesize), m_stopbits(stopbits), m_flowcontrol(flowcontrol)
102 if (m_port.empty() ==
false)
169 if (-1 == ioctl(m_fd, TIOCINQ, &count)) {
182 if (m_is_open ==
true) {
225 if (m_port.empty()) {
228 if (m_is_open ==
true) {
232 m_fd =
::open(m_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
260 if (m_is_open ==
false) {
265 struct timeval timeout = {timeout_s, 0};
268 FD_SET(
static_cast<unsigned int>(m_fd), &readfds);
270 int ret = select(FD_SETSIZE, &readfds, (fd_set *)
nullptr, (fd_set *)
nullptr, &timeout);
274 }
else if (ret == 0) {
278 ssize_t n =
::read(m_fd, c, 1);
293 size_t eol_len = eol.length();
297 size_t bytes_read = this->
read(&c, 1);
298 if (bytes_read == 0) {
302 if (std::string(line, line.size() - eol_len, eol_len) == eol) {
315 if (m_is_open ==
false) {
319 ssize_t r =
::write(m_fd, s.c_str(), s.size());
320 if (r != (ssize_t)(s.size())) {
325 void vpSerial::configure()
331 struct termios options;
333 if (tcgetattr(m_fd, &options) == -1) {
339 options.c_cflag |= (tcflag_t)(CLOCAL | CREAD);
340 options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN);
342 options.c_oflag &= (tcflag_t) ~(OPOST);
343 options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
346 options.c_iflag &= (tcflag_t)~IUCLC;
349 options.c_iflag &= (tcflag_t)~PARMRK;
353 switch (m_baudrate) {
519 ::cfsetspeed(&options, baudrate);
521 ::cfsetispeed(&options, baudrate);
522 ::cfsetospeed(&options, baudrate);
526 options.c_cflag &= (tcflag_t)~CSIZE;
527 switch (m_bytesize) {
529 options.c_cflag |= CS8;
532 options.c_cflag |= CS7;
535 options.c_cflag |= CS6;
538 options.c_cflag |= CS5;
542 switch (m_stopbits) {
544 options.c_cflag &= (tcflag_t) ~(CSTOPB);
547 options.c_cflag |= (CSTOPB);
552 options.c_iflag &= (tcflag_t) ~(INPCK | ISTRIP);
555 options.c_cflag &= (tcflag_t) ~(PARENB | PARODD);
558 options.c_cflag &= (tcflag_t) ~(PARODD);
559 options.c_cflag |= (PARENB);
562 options.c_cflag |= (PARENB | PARODD);
567 switch (m_flowcontrol) {
584 options.c_iflag |= (IXON | IXOFF);
587 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
589 options.c_iflag &= (tcflag_t) ~(IXON | IXOFF);
595 options.c_cflag |= (CRTSCTS);
597 options.c_cflag &= (
unsigned long)~(CRTSCTS);
598 #elif defined CNEW_RTSCTS
600 options.c_cflag |= (CNEW_RTSCTS);
602 options.c_cflag &= (
unsigned long)~(CNEW_RTSCTS);
604 #error "OS doesn't support serial rtscts"
607 options.c_cc[VMIN] = 0;
608 options.c_cc[VTIME] = 0;
611 ::tcsetattr(m_fd, TCSANOW, &options);
614 #elif !defined(VISP_BUILD_SHARED_LIBS)
616 void dummy_vpSerial(){};
error that can be emitted 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)