linux下串口打开卡住,打开设备时,Linux串口缓冲区不为空

这篇博客探讨了Linux终端驱动程序如何缓冲输入,即使设备未打开。作者建议在打开串行端口时立即读取所有挂起的输入以模仿小型操作系统的行为,并提供了一段示例代码来实现这一功能,包括设置阻塞和非阻塞模式,以及调整读取超时。
摘要由CSDN通过智能技术生成

即使没有打开,Linux终端驱动程序也会缓冲输入.这可能是一个有用的功能,特别是如果速度/奇偶校验/等.设置得当.

要复制较小操作系统的行为,请在打开时立即从端口读取所有挂起的输入:

...

int fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_SYNC);

if (fd < 0)

exit (1);

set_blocking (fd, 0); // disable reads blocked when no input ready

char buf [10000];

int n;

do {

n = read (fd, buf, sizeof buf);

} while (n > 0);

set_blocking (fd, 1); // enable read blocking (if desired)

... // now there is no pending input

void set_blocking (int fd, int should_block)

{

struct termios tty;

memset (&tty, 0, sizeof tty);

if (tcgetattr (fd, &tty) != 0)

{

error ("error %d getting term settings set_blocking", errno);

return;

}

tty.c_cc[VMIN] = should_block ? 1 : 0;

tty.c_cc[VTIME] = should_block ? 5 : 0; // 0.5 seconds read timeout

if (tcsetattr (fd, TCSANOW, &tty) != 0)

error ("error setting term %sblocking", should_block ? "" : "no");

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值