为什么此串行/调制解调器代码会使我的终端显示混乱?

时间:2019-05-26 17:24:07

标签: c++ linux terminal serial-port modem

我写了一些代码,基本上使用regex / dev / tty *在Unix系统上找到任何调制解调器,然后进行任何匹配,看是否可以打开端口,如果可以,请发送AT命令并检查响应消息是否包含字符“确定”。

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

运行程序后,如果输入命令并输入(例如ls),则不会显示该命令,但是当您按Enter键时,您会看到输出。

代码如下:

api-platform/core v2.2

输出:

#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);
}

1 个答案:

答案 0 :(得分:1)

  

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

一个精确的答案要求您在执行代码之前发布终端的设置,即stty -a

  

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

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

代码中所需的简单更改将类似于以下补丁(请忽略C和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);

然后在 enumerate_ports()中,::close(fd);的最后两个实例需要替换为执行恢复的顺序:

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

  

运行程序后,如果输入命令并输入,例如ls,则不显示该命令...

这显然是清除ECHO属性的结果。
回车“丢失”可能是由于OPOST已清除。
您的程序已清除但可能希望外壳程序设置的其他显着属性是ICANON,ICRNL和IEXTEN。
但是,正确的,有保证的修补程序不是尝试确定要完全撤消的操作,而是仅将termios设置恢复为原始状态。

另一种(懒惰)方法是在执行程序后使用stty sane命令。