// // Created by robin on 5/6/16. // #include // Standard input/output definitions #include // UNIX standard function definitions #include // File control definitions #include // Error number definitions #include // POSIX terminal control definitions #include // String function definitions #include #include "ArduinoSerial.h" ArduinoSerial::ArduinoSerial(std::string port, int baudrate) : port_(port) , bauderate_(baudrate) , fd_(0) { } ArduinoSerial::~ArduinoSerial() { close(); } int ArduinoSerial::open() { struct termios toptions; //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK); if (fd_ == -1) { perror("serialport_init: Unable to open port "); return -1; } //int iflags = TIOCM_DTR; //ioctl(fd, TIOCMBIS, &iflags); // turn on DTR //ioctl(fd, TIOCMBIC, &iflags); // turn off DTR if (tcgetattr(fd_, &toptions) < 0) { perror("serialport_init: Couldn't get term attributes"); return -1; } speed_t brate; switch(bauderate_) { case 4800: brate=B4800; break; case 9600: brate=B9600; break; #ifdef B14400 case 14400: brate=B14400; break; #endif case 19200: brate=B19200; break; #ifdef B28800 case 28800: brate=B28800; break; #endif case 38400: brate=B38400; break; case 57600: brate=B57600; break; case 115200: brate=B115200; break; default: brate = (speed_t)bauderate_; } cfsetispeed(&toptions, brate); cfsetospeed(&toptions, brate); // 8N1 toptions.c_cflag &= ~PARENB; toptions.c_cflag &= ~CSTOPB; toptions.c_cflag &= ~CSIZE; toptions.c_cflag |= CS8; // no flow control toptions.c_cflag &= ~CRTSCTS; //toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw toptions.c_oflag &= ~OPOST; // make raw // see: http://unixwiz.net/techtips/termios-vmin-vtime.html toptions.c_cc[VMIN] = 0; toptions.c_cc[VTIME] = 0; //toptions.c_cc[VTIME] = 20; tcsetattr(fd_, TCSANOW, &toptions); if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) { perror("init_serialport: Couldn't set term attributes"); return -1; } return 0; } void ArduinoSerial::close() { if (fd_ != 0) { ::close(fd_); fd_ = 0; } }