Linux的串行端口:阻止阅读与超时 [英] Linux Serial Port: Blocking Read with Timeout

查看:204
本文介绍了Linux的串行端口:阻止阅读与超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了很多有用的线程和一些教程,但我仍然有一些一些问题应该很简单。仅供参考这里,我已经详细阅读了一些主题:

I have studied many useful threads and some tutorials, but I'm still having some issues with something that should be very simple. For reference here are some threads that I've perused:

<一个href=\"http://stackoverflow.com/questions/2917881/how-to-implement-a-timeout-in-read-function-call\">How实现在读函数调用超时?

<一个href=\"http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c\">how来打开,读取,并从串口用C写

在任何情况下,我有一个有点问题。如果我收到的数据我的code正常工作。如果我不这样做,read()函数摊位和走出我的程序的唯一方法是使用kill -9(注意:我用信号处理信号的线程读取串行数据终止这不罪魁祸首,则read()调用仍然摊点,即使我已删除了我的信号处理)。我想要做的就是有一个读取块,一次读取一大块(从而节省CPU占用率),但如果读取没有接收到数据时,我把它wan't超时。

At any rate, I have a bit of a problem. My code works fine if I receive data. If I don't, the read() function stalls and the only way to get out of my program is to use kill -9 (NOTE: I use signal handling to signal to the thread reading the serial data to terminate. This is not the culprit, the read() call still stalls even if I have removed my signal handling). What I'm trying to do is to have a read that blocks and reads a chunk at a time (therefore saving CPU usage), however if the read receives no data, I wan't it to timeout.

下面是我申请到端口的设置:

Here are the settings that I'm applying to the port:

struct termios serial_struct;
serial_struct.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
serial_struct.c_iflag = IGNPAR;
serial_struct.c_oflag = 0;
serial_struct.c_lflag = 0;
serial_struct.c_cc[VTIME] = 1;  // timeout after .1s that isn't working
serial_struct.c_cc[VMIN] = 64;  // want to read a chunk of 64 bytes at a given time

然后我设定tcsetattr这些设置(),并确认该端口通过tcgetattr收到的设置()。我在想,我的设置可能是相互矛盾的,因为我的读取表现为阻塞和等待,直到接收到64个字节,但不这样做的问候超时什么。我知道我可以使用select()来处理超时,但我希望避免多系统调用。

I then set these settings with tcsetattr() and confirm that the port received the settings via tcgetattr(). I'm thinking that my settings may be conflicting, because my reads appear to be blocking and wait until 64 bytes are received, but do not do anything with regards to the timeout. I understand that I can use select() to deal with a timeout, but I'm hoping to avoid the multiple system calls.

和往常一样,在此先感谢您的帮助。

As always, thanks in advance for the help.

推荐答案

的termios 3 的:

MIN> 0; TIME> 0:TIME规定了十分之一秒的计时器的限制。一旦输入的初始字节可用,每收到一个新的字节后重新启动定时器。读(2)返回或当所请求的字节或MIN字节的数目中的较小者已被读出时,或当字节间超时。因为计时器才开始的初始字节变得可用之后,至少有一个字节将被读取。

MIN > 0; TIME > 0: TIME specifies the limit for a timer in tenths of a second. Once an initial byte of input becomes available, the timer is restarted after each further byte is received. read(2) returns either when the lesser of the number of bytes requested or MIN byte have been read, or when the inter-byte timeout expires. Because the timer is only started after the initial byte becomes available, at least one byte will be read.

请注意,该计时器不会的开始,直到接收的数据中的至少一个字节。接收第一数据字节后,如果有以往的第二时间十分之接收连续数据字节之间的间隙的读出将超时

Note that the timer does not start until at least one byte of data is received. After receiving that first data byte, the read will timeout if there is ever a gap of TIME tenths of a second between receiving consecutive data bytes.

这篇关于Linux的串行端口:阻止阅读与超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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