C++ 为什么串行/调制解调器代码会弄乱我的终端显示?

C++ 为什么串行/调制解调器代码会弄乱我的终端显示?,c++,linux,terminal,serial-port,modem,C++,Linux,Terminal,Serial Port,Modem,我编写了一些代码,基本上使用regex/dev/tty*查找unix系统上的任何调制解调器,然后查看是否可以打开端口,如果可以,则发送AT命令,并检查响应消息是否包含字符“OK” 代码确实找到了调制解调器,但不幸的是,它弄乱了终端显示。见下文。我注意到它还打印AT命令-请参见下面的输出。为什么我的终端显示被改变了?我如何修复 运行程序后,如果输入命令并输入,例如ls,则不会显示该命令,但按enter键时,确实会看到输出 代码如下: #include <iostream> #inclu

我编写了一些代码,基本上使用regex/dev/tty*查找unix系统上的任何调制解调器,然后查看是否可以打开端口,如果可以,则发送AT命令,并检查响应消息是否包含字符“OK”

代码确实找到了调制解调器,但不幸的是,它弄乱了终端显示。见下文。我注意到它还打印AT命令-请参见下面的输出。为什么我的终端显示被改变了?我如何修复

运行程序后,如果输入命令并输入,例如ls,则不会显示该命令,但按enter键时,确实会看到输出

代码如下:

#include <iostream>
#include <string>
#include <unordered_map>
#include <iomanip>

#include <memory>

#include <sstream>
#include <thread>

#include <iostream>
#include <filesystem>
#include <regex>

#include <unistd.h>  // close
#include <fcntl.h>   // open, O_RDWR, etc
#include <termios.h>

#include <string.h>

#include <sys/select.h>  // timeouts for read

#include <sys/timeb.h>   // measure time taken

int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        // Error from tcgetattr - can use strerror(errno)
        return -1;
    }

    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
      // Error from tcsetattr- use strerror(errno)
        return -1;
    }
    return 0;
}

long enumerate_ports(std::unordered_map <std::string, std::string>& ports) {

    // ls /dev | grep ^tty.*
    const std::regex my_filter( "^tty.*" );
    std::string path = "/dev/";
    for (const auto & entry : std::filesystem::directory_iterator(path)) {
     std::smatch sm;

     std::string tmp = entry.path().filename().string();
     // if we have a regex match attempt to open port and send AT command
     if (std::regex_match(tmp, sm, my_filter)) {
     std::string portname = entry.path().string();
         int fd = ::open(portname.c_str(), O_RDWR | O_NOCTTY);
         if (fd < 0) {
       // Error opening port
             continue;
         } else {
       // port was opened successfully
           // try to write AT command and do we get an OK response
           // baudrate 9600, 8 bits, no parity, 1 stop bit
           if(set_interface_attribs(fd, B9600) != 0) {
             ::close(fd);
         continue;
           }

           int wlen = ::write(fd, "AT\r\n", 4);
           if (wlen != 4) {
         // Error from write
               ::close(fd);
               continue;
           }

          // tcdrain() waits until all output written to the object referred 
          // to by fd has been transmitted.
           tcdrain(fd);

           fd_set set;
           struct timeval timeout;

           FD_ZERO(&set); /* clear the set */
           FD_SET(fd, &set); /* add our file descriptor to the set */

           timeout.tv_sec = 0;
           timeout.tv_usec = 100000; // 100 milliseconds

           // wait for data to be read or timeout
           int rv = select(fd + 1, &set, NULL, NULL, &timeout);
           if(rv > 0) {  // no timeout or error
               unsigned char buf[80];
               const int bytes_read = ::read(fd, buf, sizeof(buf) - 1);
               if (bytes_read > 0) {
                   buf[bytes_read] = 0;
                   unsigned char* p = buf;
                   // scan for "OK"
                   for (int i = 0; i < bytes_read; ++i) {
             if (*p == 'O' && i < bytes_read - 1 && *(p+1) == 'K') {
                       // we have a positive response from device so add to ports
                       ports[portname] = "";
                       break;
                     }
                     p++;
                   }
               }
        }
       ::close(fd);
         }
     }
   }

   return ports.size();
}


int main() {

    struct timeb start, end;
    int diff;
    ftime(&start);

    // get list of ports available on system
    std::unordered_map <std::string, std::string> ports;
    long result = enumerate_ports(ports);
    std::cout << "No. found modems: " << result << std::endl;
    for (const auto& item : ports) {
        std::cout << item.first << "->" << item.second << std::endl;
    }

    ftime(&end);
    diff = (int) (1000.0 * (end.time - start.time)
        + (end.millitm - start.millitm));

    printf("Operation took %u milliseconds\n", diff);
}
为什么串行/调制解调器代码会弄乱我的终端显示

准确的回答要求您在执行代码之前发布终端的设置,即stty-A

代码确实找到了调制解调器,但不幸的是,它弄乱了终端显示

最简单的,即简单的解决方法/解决方案是遵循旧的但很少遵循的建议,即保存然后恢复终端的termios设置,如中所示

<> P>代码中所需的简单更改将是类似于忽略C和C++的混合;我只知道下面的补丁

 struct termios savetty;

 int set_interface_attribs(int fd, int speed)
 {
+    struct termios tty;

     if (tcgetattr(fd, &tty) < 0) {
         // Error from tcgetattr - can use strerror(errno)
         return -1;
     }
+    savetty = tty;    /* preserve original settings for restoration */

     cfsetospeed(&tty, (speed_t)speed);
     cfsetispeed(&tty, (speed_t)speed);
运行程序后,如果输入命令并输入,例如ls,则不会显示该命令

这显然是清除ECHO属性的结果。 丢失的回车可能是由于清除了OPOST。 其他被程序清除但可能被shell设置的显著属性有ICANON、ICRNL和IEXTEN。 但是,与其试图确定到底需要撤销什么,正确且有保证的修复方法是简单地将termios设置恢复到其原始状态


另一种惰性方法是在执行程序后使用sttysane命令

看起来您没有从终端输出中删除“\n”,或者您的终端输出需要\r\n。很难说你想要一行还是多行的结果。请将ICANON设置为tty.clflag。我相信这会剥夺\r。另外,如果您将自己添加到拨号组,则不需要sudo。@jww命令为tty.c|u lflag&=~ECHO | ECHONL | ICANON | ISIG | IEXTEN;从此处移除ICANON不会改变行为。
 struct termios savetty;

 int set_interface_attribs(int fd, int speed)
 {
+    struct termios tty;

     if (tcgetattr(fd, &tty) < 0) {
         // Error from tcgetattr - can use strerror(errno)
         return -1;
     }
+    savetty = tty;    /* preserve original settings for restoration */

     cfsetospeed(&tty, (speed_t)speed);
     cfsetispeed(&tty, (speed_t)speed);
+    if (tcsetattr(fd, &savetty) < 0) {
+        // report cannot restore attributes
+    }
     ::close(fd);