You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArduinoSerial.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Created by robin on 5/6/16.
  3. //
  4. #include <stdio.h> // Standard input/output definitions
  5. #include <unistd.h> // UNIX standard function definitions
  6. #include <fcntl.h> // File control definitions
  7. #include <errno.h> // Error number definitions
  8. #include <termios.h> // POSIX terminal control definitions
  9. #include <string.h> // String function definitions
  10. #include <sys/ioctl.h>
  11. #include "ArduinoSerial.h"
  12. ArduinoSerial::ArduinoSerial(std::string port, int baudrate)
  13. : port_(port)
  14. , bauderate_(baudrate)
  15. , fd_(0)
  16. {
  17. }
  18. ArduinoSerial::~ArduinoSerial()
  19. {
  20. close();
  21. }
  22. int ArduinoSerial::open()
  23. {
  24. struct termios toptions;
  25. //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
  26. fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK);
  27. if (fd_ == -1) {
  28. perror("serialport_init: Unable to open port ");
  29. return -1;
  30. }
  31. //int iflags = TIOCM_DTR;
  32. //ioctl(fd, TIOCMBIS, &iflags); // turn on DTR
  33. //ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
  34. if (tcgetattr(fd_, &toptions) < 0) {
  35. perror("serialport_init: Couldn't get term attributes");
  36. return -1;
  37. }
  38. speed_t brate;
  39. switch(bauderate_) {
  40. case 4800:
  41. brate=B4800;
  42. break;
  43. case 9600:
  44. brate=B9600;
  45. break;
  46. #ifdef B14400
  47. case 14400:
  48. brate=B14400;
  49. break;
  50. #endif
  51. case 19200:
  52. brate=B19200;
  53. break;
  54. #ifdef B28800
  55. case 28800:
  56. brate=B28800;
  57. break;
  58. #endif
  59. case 38400:
  60. brate=B38400;
  61. break;
  62. case 57600:
  63. brate=B57600;
  64. break;
  65. case 115200:
  66. brate=B115200;
  67. break;
  68. default:
  69. brate = (speed_t)bauderate_;
  70. }
  71. cfsetispeed(&toptions, brate);
  72. cfsetospeed(&toptions, brate);
  73. // 8N1
  74. toptions.c_cflag &= ~PARENB;
  75. toptions.c_cflag &= ~CSTOPB;
  76. toptions.c_cflag &= ~CSIZE;
  77. toptions.c_cflag |= CS8;
  78. // no flow control
  79. toptions.c_cflag &= ~CRTSCTS;
  80. //toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
  81. toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  82. toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  83. toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  84. toptions.c_oflag &= ~OPOST; // make raw
  85. // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
  86. toptions.c_cc[VMIN] = 0;
  87. toptions.c_cc[VTIME] = 0;
  88. //toptions.c_cc[VTIME] = 20;
  89. tcsetattr(fd_, TCSANOW, &toptions);
  90. if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) {
  91. perror("init_serialport: Couldn't set term attributes");
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. void ArduinoSerial::close()
  97. {
  98. if (fd_ != 0) {
  99. ::close(fd_);
  100. fd_ = 0;
  101. }
  102. }