34 #include <visp3/core/vpConfig.h>
43 #include <sys/ioctl.h>
45 #include <sys/types.h>
49 #include <visp3/core/vpException.h>
50 #include <visp3/core/vpSerial.h>
54 #define TIOCINQ FIONREAD
56 #define TIOCINQ 0x541B
106 : m_port(port), m_fd(-1), m_is_open(false), m_xonxoff(false), m_rtscts(false), m_baudrate(baudrate), m_parity(parity),
107 m_bytesize(bytesize), m_stopbits(stopbits), m_flowcontrol(flowcontrol)
109 if (m_port.empty() ==
false)
180 if (-1 == ioctl(m_fd, TIOCINQ, &count)) {
194 if (m_is_open ==
true) {
242 if (m_port.empty()) {
245 if (m_is_open ==
true) {
249 m_fd =
::open(m_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
277 if (m_is_open ==
false) {
282 struct timeval timeout = { timeout_s, 0 };
285 FD_SET(
static_cast<unsigned int>(m_fd), &readfds);
287 int ret = select(FD_SETSIZE, &readfds, (fd_set *)
nullptr, (fd_set *)
nullptr, &timeout);
297 ssize_t n =
::read(m_fd, c, 1);
312 size_t eol_len = eol.length();
316 size_t bytes_read = this->
read(&c, 1);
317 if (bytes_read == 0) {
321 if (std::string(line, line.size() - eol_len, eol_len) == eol) {
334 if (m_is_open ==
false) {
338 ssize_t r =
::write(m_fd, s.c_str(), s.size());
339 if (r != (ssize_t)(s.size())) {
344 void vpSerial::configure()
350 struct termios options;
352 if (tcgetattr(m_fd, &options) == -1) {
358 options.c_cflag |= (tcflag_t)(CLOCAL | CREAD);
359 options.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN);
361 options.c_oflag &= (tcflag_t)~(OPOST);
362 options.c_iflag &= (tcflag_t)~(INLCR | IGNCR | ICRNL | IGNBRK);
365 options.c_iflag &= (tcflag_t)~IUCLC;
368 options.c_iflag &= (tcflag_t)~PARMRK;
372 switch (m_baudrate) {
538 ::cfsetspeed(&options, baudrate);
540 ::cfsetispeed(&options, baudrate);
541 ::cfsetospeed(&options, baudrate);
545 options.c_cflag &= (tcflag_t)~CSIZE;
546 switch (m_bytesize) {
548 options.c_cflag |= CS8;
551 options.c_cflag |= CS7;
554 options.c_cflag |= CS6;
557 options.c_cflag |= CS5;
561 switch (m_stopbits) {
563 options.c_cflag &= (tcflag_t)~(CSTOPB);
566 options.c_cflag |= (CSTOPB);
571 options.c_iflag &= (tcflag_t)~(INPCK | ISTRIP);
574 options.c_cflag &= (tcflag_t)~(PARENB | PARODD);
577 options.c_cflag &= (tcflag_t)~(PARODD);
578 options.c_cflag |= (PARENB);
581 options.c_cflag |= (PARENB | PARODD);
586 switch (m_flowcontrol) {
603 options.c_iflag |= (IXON | IXOFF);
606 options.c_iflag &= (tcflag_t)~(IXON | IXOFF | IXANY);
608 options.c_iflag &= (tcflag_t)~(IXON | IXOFF);
614 options.c_cflag |= (CRTSCTS);
616 options.c_cflag &= (
unsigned long)~(CRTSCTS);
617 #elif defined CNEW_RTSCTS
619 options.c_cflag |= (CNEW_RTSCTS);
621 options.c_cflag &= (
unsigned long)~(CNEW_RTSCTS);
623 #error "OS doesn't support serial rtscts"
626 options.c_cc[VMIN] = 0;
627 options.c_cc[VTIME] = 0;
630 ::tcsetattr(m_fd, TCSANOW, &options);
633 #elif !defined(VISP_BUILD_SHARED_LIBS)
635 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)
@ 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)
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)
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)