How to efficiently wait for CTS or DSR of RS232 in Linux?

梦想与她 提交于 2019-12-06 03:56:32

问题


Currently, I'm reading the CTS and DSR signals of a serial port in the following way:

bool get_cts(int fd) {
    int s;
    ioctl(fd, TIOCMGET, &s);
    return (s & TIOCM_CTS) != 0;
}

Now I'd like to wait until get_cts() returns true. A simple loop isn't the best solution I think (as it's extremely resource-intensive).

void wait_cts(int fd) {
    while(1) {
        if(get_cts(fd)) {
             return;
        }
    }
}

Is there any better solution using C or C++ on Linux? (I cannot use any hardware flow control as I don't need the serial data lines at all.)


回答1:


There is the ioctl TIOCMIWAIT which blocks until a given set of signals change.

Sadly this ioctl is not documented in the tty_ioctl(4) page nor in ioctl_list(4).

I have learned about this ioctl in this question:

Python monitor serial port (RS-232) handshake signals




回答2:


The select system call is meant for applications like that. You can do other work, or sleep, then periodically check the status of the FD_SET. It might even be overkill for what you are doing, if your program does nothing else but grab data.



来源:https://stackoverflow.com/questions/8952098/how-to-efficiently-wait-for-cts-or-dsr-of-rs232-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!