如何在Linux中有效地等待RS232的CTS或DSR? [英] How to efficiently wait for CTS or DSR of RS232 in Linux?

查看:351
本文介绍了如何在Linux中有效地等待RS232的CTS或DSR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在以下列方式读取串行端口的CTS和DSR信号:

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

现在我想等待 get_cts )返回true。一个简单的循环不是我认为最好的解决方案(因为它是非常资源密集型)。

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

C或C ++在Linux? (我不能使用任何硬件流控制,因为我根本不需要串行数据线。)

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.)

推荐答案

ioctl TIOCMIWAIT 会阻塞,直到给定的一组信号发生变化。

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

很遗憾这个ioctl没有记录在 tty_ioctl(4)页面或 ioctl_list(4)

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

我在这个问题中了解了这个ioctl:

I have learned about this ioctl in this question:

Python监视器串行端口(RS-232)握手信号

这篇关于如何在Linux中有效地等待RS232的CTS或DSR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆