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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. BResult 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. return BResult().error(strerror(errno));
  29. }
  30. //int iflags = TIOCM_DTR;
  31. //ioctl(fd, TIOCMBIS, &iflags); // turn on DTR
  32. //ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
  33. if (tcgetattr(fd_, &toptions) < 0) {
  34. return BResult().error(strerror(errno));
  35. }
  36. speed_t brate;
  37. switch(bauderate_) {
  38. case 4800:
  39. brate=B4800;
  40. break;
  41. case 9600:
  42. brate=B9600;
  43. break;
  44. #ifdef B14400
  45. case 14400:
  46. brate=B14400;
  47. break;
  48. #endif
  49. case 19200:
  50. brate=B19200;
  51. break;
  52. #ifdef B28800
  53. case 28800:
  54. brate=B28800;
  55. break;
  56. #endif
  57. case 38400:
  58. brate=B38400;
  59. break;
  60. case 57600:
  61. brate=B57600;
  62. break;
  63. case 115200:
  64. brate=B115200;
  65. break;
  66. default:
  67. brate = (speed_t)bauderate_;
  68. }
  69. cfsetispeed(&toptions, brate);
  70. cfsetospeed(&toptions, brate);
  71. // 8N1
  72. toptions.c_cflag &= ~PARENB;
  73. toptions.c_cflag &= ~CSTOPB;
  74. toptions.c_cflag &= ~CSIZE;
  75. toptions.c_cflag |= CS8;
  76. // no flow control
  77. toptions.c_cflag &= ~CRTSCTS;
  78. //toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
  79. toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
  80. toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  81. toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  82. toptions.c_oflag &= ~OPOST; // make raw
  83. // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
  84. toptions.c_cc[VMIN] = 0;
  85. toptions.c_cc[VTIME] = 0;
  86. //toptions.c_cc[VTIME] = 20;
  87. tcsetattr(fd_, TCSANOW, &toptions);
  88. if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) {
  89. return BResult().error(strerror(errno));
  90. }
  91. return BResult().ok(true);
  92. }
  93. void ArduinoSerial::close()
  94. {
  95. if (fd_ != 0) {
  96. ::close(fd_);
  97. fd_ = 0;
  98. }
  99. }
  100. Result<std::pair<SERIAL_PACKET_TYPE, std::string>> ArduinoSerial::read()
  101. {
  102. SERIAL_PACKET_TYPE packetType = 0;
  103. auto res = readBytes((char*)&packetType, sizeof(packetType));
  104. if (!res) {
  105. return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
  106. }
  107. SERIAL_PACKET_LENGTH_TYPE length = 0;
  108. res = readBytes((char*)&length, sizeof(length));
  109. if (!res) {
  110. return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
  111. }
  112. if (length > 0) {
  113. char buff[length];
  114. res = readBytes(buff, length);
  115. if (!res) {
  116. return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
  117. }
  118. std::pair<SERIAL_PACKET_TYPE, std::string> pair(packetType, std::string(buff, length));
  119. return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().ok(pair);
  120. }
  121. std::pair<SERIAL_PACKET_TYPE, std::string> pair(packetType, "");
  122. return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().ok(pair);
  123. }
  124. BResult ArduinoSerial::readBytes(char* buffer, size_t length)
  125. {
  126. ssize_t res = 0;
  127. bool done = false;
  128. size_t pos = 0;
  129. while (!done)
  130. {
  131. res = ::read(fd_, buffer + pos, (size_t) length);
  132. if (res < 0)
  133. {
  134. return BResult().error(strerror(errno));
  135. }
  136. if (res > 0) {
  137. pos += res;
  138. if (pos >= length) {
  139. done = true;
  140. }
  141. }
  142. else {
  143. usleep(1);
  144. }
  145. }
  146. return BResult().ok(true);
  147. }
  148. BResult ArduinoSerial::write(const char *data, SERIAL_PACKET_LENGTH_TYPE length)
  149. {
  150. auto res = writeBytes((const char*)&length, sizeof(length));
  151. if (!res) {
  152. return res;
  153. }
  154. return writeBytes(data, length);
  155. }
  156. BResult ArduinoSerial::writeBytes(const char *data, size_t length)
  157. {
  158. auto res = ::write(fd_, data, length);
  159. if (res < 0) {
  160. return BResult().error(strerror(errno));
  161. }
  162. return BResult().ok(true);
  163. }