我如何读取超时的 tty 文件?
我在 /dev 中有 tty 设备,我在其中发送 AT 命令。我想逐行读取并在超时后停止读取文件。
I have tty device in /dev , where I send AT commands. I want to read line by line and stop reading file after timeout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用程序
stty
来配置tty设备。要查看终端 /dev/ttyS0 的设置,请尝试stty -a -F /dev/ttyS0 。
有关超时的默认设置为 min = 1; time = 0,表示读取程序会一直读取,直到至少读取完一个字符,并且不会超时。使用例如
stty -F /dev/ttyS0 min 0 time 10
,读取程序(例如
cat
)将在一秒后完成读取,无论是否读取了任何内容。参数time
的单位是十分之一秒;您可以查看man stty
了解更多信息。You can use the program
stty
to configure the tty device. To see the settings for terminal /dev/ttyS0, trystty -a -F /dev/ttyS0
The default settings regarding timeout are
min = 1; time = 0
, which means that the reading program will read until at least one character has been read and there is no timeout. Using e.g.stty -F /dev/ttyS0 min 0 time 10
the reading program (e.g.
cat
) will finish reading after one second whether anything has been read or not. The unit for the parametertime
is tenths of a second; you can check outman stty
for more information.从 此处,您可以有这样的脚本:
当 FLAG==GO 时,脚本将从串行端口一次读取一行。 STATUS 变量获取 READ 命令的返回值。根据手册,如果达到指定的超时时间,READ 将返回任何不同于 0 的值;当发生这种情况时,FLAG 会被更新,退出读取循环。
Compiling some info from here, you can have a script in the sorts of:
While FLAG==GO, the script will read one line at a time from the serial port. The STATUS variable gets the return of READ command. According to the manual READ will return anything different than 0 if the specified timeout is reached; when that happens, FLAG is updated, exiting the read loop.