gpt4 book ai didi

Linux串口问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:04 25 4
gpt4 key购买 nike

我正在编写代码以在 Linux 中通过串口与另一台设备通信

即使到达 0 个字符,我也希望超时非阻塞。 termios 结构允许您设置 VTIME 和 VMIN,但如果 VMIN 大于 0,并且如果返回 0 个字符,读取调用将永远阻塞...WTF,为什么。这似乎不包括其他设备在短时间内出现故障并且现在我的应用程序在读取调用时被阻止的情况。这似乎是一个需要忽视的关键行为。我真的不想实现自己的超时。

Write command
Read block timeout of around .3s(if 0 characters, still wait max of .3s)

最佳答案

如果您希望 read 在超时后返回无数据,您可以设置 MIN == 0TIME > 0。来自 tcsetattr(3):

MIN == 0; TIME > 0: TIME specifies the limit for a timer in tenths of a second. The timer is started when read(2) is called. read(2) returns either when at least one byte of data is available, or when the timer expires. If the timer expires without any input becoming available, read(2) returns 0.

此模式可用于构建更高级别的通信功能,您无论如何都必须实现这些功能以处理部分读取(即如果另一端在“数据包”中间崩溃)、错误数据包、拆分数据包等在。这就是简单的 read 无法为您完成的所有事情。

关于Linux串口问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7694165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com