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.

100 lines
2.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "uart.hpp"
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <iostream>
  7. using namespace iflytop;
  8. using namespace std;
  9. static map<string, uint32_t> s_baudratemap = {
  10. {"0", B0}, {"50", B50}, {"75", B75}, {"110", B110}, //
  11. {"134", B134}, {"150", B150}, {"200", B200}, {"300", B300}, //
  12. {"600", B600}, {"1200", B1200}, {"1800", B1800}, {"2400", B2400}, //
  13. {"4800", B4800}, {"9600", B9600}, {"19200", B19200}, {"38400", B38400}, //
  14. {"57600", B57600}, {"115200", B115200}, {"230400", B230400}, {"460800", B460800}, //
  15. {"500000", B500000}, {"576000", B576000}, {"921600", B921600}, {"1000000", B1000000}, //
  16. {"1152000", B1152000}, {"1500000", B1500000}, {"2000000", B2000000}, {"2500000", B2500000}, //
  17. {"3000000", B3000000}, {"3500000", B3500000}, {"4000000", B4000000},
  18. };
  19. Uart::Uart() {}
  20. Uart::~Uart() {}
  21. int Uart::open(string path, string ratestr) {
  22. int rc;
  23. m_name = path;
  24. m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY);
  25. if (m_fd < 0) {
  26. cout << "open " << path << " failed" << endl;
  27. return -1;
  28. }
  29. if (s_baudratemap.find(ratestr) == s_baudratemap.end()) {
  30. return -1;
  31. }
  32. m_rate = ratestr;
  33. memset(&m_tty, 0, sizeof(m_tty));
  34. m_tty.c_cflag |= CLOCAL | CREAD; // 激活本地连接与接受使能
  35. m_tty.c_cflag &= ~CSIZE; // 失能数据位屏蔽
  36. m_tty.c_cflag |= CS8; // 8位数据位
  37. m_tty.c_cflag &= ~CSTOPB; // 1位停止位
  38. m_tty.c_cflag &= ~PARENB; // 无校验位
  39. m_tty.c_cc[VTIME] = 0;
  40. m_tty.c_cc[VMIN] = 0;
  41. cfsetispeed(&m_tty, s_baudratemap[ratestr]); // 设置输入波特率
  42. cfsetospeed(&m_tty, s_baudratemap[ratestr]); // 设置输出波特率
  43. tcflush(m_fd, TCIFLUSH); // 刷清未处理的输入和/或输出
  44. /* Apply attributes */
  45. rc = tcsetattr(m_fd, TCSANOW, &m_tty);
  46. if (rc) {
  47. cout << "tcsetattr failed" << endl;
  48. return -3;
  49. }
  50. return 0;
  51. }
  52. int Uart::send(char *data, int size) {
  53. int sent = 0;
  54. sent = write(m_fd, data, size);
  55. if (sent > 0) {
  56. printf("send success, data is %s\r\n", data);
  57. }
  58. return sent;
  59. }
  60. int Uart::receive(char *data, int size_max) {
  61. int received = 0;
  62. received = read(m_fd, data, size_max);
  63. if (received > 0) {
  64. printf("recv success,recv size is %d,data is %s\r\n", received, data);
  65. }
  66. return received;
  67. }
  68. int Uart::close() {
  69. ::close(m_fd);
  70. m_fd = -1;
  71. return 0;
  72. }
  73. bool Uart::flush_rx() {
  74. if (m_fd < 0) return false;
  75. int rc = tcflush(m_fd, TCIFLUSH);
  76. return rc == 0;
  77. }
  78. bool Uart::flush_tx() {
  79. if (m_fd < 0) return false;
  80. int rc = tcflush(m_fd, TCOFLUSH);
  81. return rc == 0;
  82. }