diff --git a/cpp/a.out b/cpp/a.out index 52c5bcd..c42e6c4 100755 Binary files a/cpp/a.out and b/cpp/a.out differ diff --git a/cpp/main.cpp b/cpp/main.cpp index c332876..1bf0f9d 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -20,12 +20,18 @@ int main(int argc, char *argv[]) { int rec_len; char buffer[128]; - g_uart.open(argv[1]); + if (argc != 3) { + cout << "input error,please input ttyS and baudrate" << endl; + return 0; + } + + g_uart.open(argv[1], argv[2]); while (1) { rec_len = g_uart.receive(buffer, sizeof(buffer)); if (rec_len > 0) { g_uart.send(buffer, rec_len); + memset(buffer, 0, sizeof(buffer)); } usleep(1000); } diff --git a/cpp/uart.cpp b/cpp/uart.cpp index d015ad3..9cb3c92 100644 --- a/cpp/uart.cpp +++ b/cpp/uart.cpp @@ -10,12 +10,22 @@ using namespace iflytop; using namespace std; +static map s_baudratemap = { + {"0", B0}, {"50", B50}, {"75", B75}, {"110", B110}, // + {"134", B134}, {"150", B150}, {"200", B200}, {"300", B300}, // + {"600", B600}, {"1200", B1200}, {"1800", B1800}, {"2400", B2400}, // + {"4800", B4800}, {"9600", B9600}, {"19200", B19200}, {"38400", B38400}, // + {"57600", B57600}, {"115200", B115200}, {"230400", B230400}, {"460800", B460800}, // + {"500000", B500000}, {"576000", B576000}, {"921600", B921600}, {"1000000", B1000000}, // + {"1152000", B1152000}, {"1500000", B1500000}, {"2000000", B2000000}, {"2500000", B2500000}, // + {"3000000", B3000000}, {"3500000", B3500000}, {"4000000", B4000000}, +}; + Uart::Uart() {} Uart::~Uart() {} -int Uart::open(string path) { +int Uart::open(string path, string ratestr) { int rc; - m_name = path; m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY); @@ -24,22 +34,23 @@ int Uart::open(string path) { return -1; } + if (s_baudratemap.find(ratestr) == s_baudratemap.end()) { + return -1; + } + m_rate = ratestr; + memset(&m_tty, 0, sizeof(m_tty)); m_tty.c_cflag |= CLOCAL | CREAD; // 激活本地连接与接受使能 - - m_tty.c_cflag &= ~CSIZE; // 失能数据位屏蔽 - m_tty.c_cflag |= CS8; // 8位数据位 - - m_tty.c_cflag &= ~CSTOPB; // 1位停止位 - - m_tty.c_cflag &= ~PARENB; // 无校验位 - + m_tty.c_cflag &= ~CSIZE; // 失能数据位屏蔽 + m_tty.c_cflag |= CS8; // 8位数据位 + m_tty.c_cflag &= ~CSTOPB; // 1位停止位 + m_tty.c_cflag &= ~PARENB; // 无校验位 m_tty.c_cc[VTIME] = 0; m_tty.c_cc[VMIN] = 0; - cfsetispeed(&m_tty, B115200); // 设置输入波特率 - cfsetospeed(&m_tty, B115200); // 设置输出波特率 + cfsetispeed(&m_tty, s_baudratemap[ratestr]); // 设置输入波特率 + cfsetospeed(&m_tty, s_baudratemap[ratestr]); // 设置输出波特率 tcflush(m_fd, TCIFLUSH); // 刷清未处理的输入和/或输出 diff --git a/cpp/uart.hpp b/cpp/uart.hpp index 5915501..4cf4168 100644 --- a/cpp/uart.hpp +++ b/cpp/uart.hpp @@ -19,14 +19,29 @@ class Uart { private: int m_fd = 0; string m_name; - int m_rate = B115200; + string m_rate; struct termios m_tty; public: Uart(); ~Uart(); - - int open(string path); + /** + * @brief 打开串口 + * + * @param path + * @param rate + * "0" "50" "75" "110" + * "134" "150" "200" "300" + * "600" "1200" "1800" "2400" + * "4800" "9600" "19200" "38400" + * "57600" "115200" "230400" "460800" + * "500000" "576000" "921600" "1000000" + * "1152000" "1500000" "2000000" "2500000" + * "3000000" "3500000" "4000000" + * + * @return int + */ + int open(string path, string ratestr); int send(char *data, int size); int receive(char *data, int size_max); int close();