macOS阻止串行I/O [英] macOS blocking serial I/O

查看:116
本文介绍了macOS阻止串行I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个作为测试套件一部分的工具,该套件需要通过串行端口与某些硬件进行通讯,以便所测试的代码能够看到环境的变化.

I'm writing a tool as part of a test suite, which needs to talk over a serial port to some hardware so that the code being tested sees the environment change.

所以,我这样做:

open("/dev/tty.usbmodem14141", O_RDWR | O_NOCTTY);

只有它挂在那里.如果我将通话替换为

only it hangs there. If I replace that call with

open("/dev/tty.usbmodem14141", O_RDWR | O_NOCTTY | O_NONBLOCK);

然后它可以工作-但我不想不必摆弄select()和朋友,也不用写忙碌的轮询,这样我就可以从串行端口读取数据了;这就是阻塞I/O的目的.

then it works -- but I would prefer not to have to fiddle with select() and friends, or write a busy-loop poll, just so I can read from the serial port; that's what blocking I/O is for.

我需要做一些特殊的事情才能使它正常工作吗?

Do I need to do anything special for this to work?

推荐答案

在非阻塞模式下打开串行终端后,您可以清除文件状态标志以在阻塞模式下执行I/O.

When you have opened the serial terminal in nonblocking mode, you can then clear the file status flag to perform I/O in blocking mode.

要清除非阻塞状态标志,可以使用

To clear the nonblocking status flag you can use fcntl(), e.g.:

int flags;

flags = fcntl(fd, F_GETFL);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);

由于Linux版本的F_SETFL只能更改O_APPEND,O_ASYNC,O_DIRECT,O_NOATIME和O_NONBLOCK标志,因此通常的做法是将代码简化为

Since the Linux version of F_SETFL can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags, common practice is to simplify the code down to just

fcntl(fd, F_SETFL, 0);

(是的,单层的可移植性与

(Yes, the single-liner does not have the same degree of portability as advocated in Setting Terminal Modes Properly.)

这篇关于macOS阻止串行I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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