Sec*_*one 5 c c++ linux serial-port wait
目前,我正在通过以下方式读取串口的CTS和DSR信号:
bool get_cts(int fd) {
int s;
ioctl(fd, TIOCMGET, &s);
return (s & TIOCM_CTS) != 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我想等到get_cts()返回true.一个简单的循环不是我认为的最佳解决方案(因为它非常耗费资源).
void wait_cts(int fd) {
while(1) {
if(get_cts(fd)) {
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Linux上使用C或C++有没有更好的解决方案?(我不能使用任何硬件流控制,因为我根本不需要串行数据线.)
有一个ioctl TIOCMIWAIT阻塞,直到给定的一组信号发生变化.
遗憾的是,这个ioctl没有在tty_ioctl(4)页面中记录,也没有在ioctl_list(4).
我在这个问题中已经了解了这个ioctl: