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

查看:19
本文介绍了如何在 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;
        }
    }
}

在 Linux 上使用 C 或 C++ 有更好的解决方案吗?(我不能使用任何硬件流控制,因为我根本不需要串行数据线.)

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天全站免登陆