LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1800|回复: 4

请教一个串口read函数的问题

[复制链接]
发表于 2005-5-19 23:56:37 | 显示全部楼层 |阅读模式
用read读取串口的数据,部分代码如下:
       int fd;
          int bytes;
          unsigned char buff[128];
          fd=open("/dev/ttyS0", O_RDWR|O_NDELAY|O_NOCTTY);
          if(fd!=1)
          {
                 .......
                 bytes=read(fd, buff, 128);
                 ......
           }
每读一次,bytes的值都是8,是不是每次最多只能读取8个字节?或者,能不能让它一次读取超过8个字节?哪位知道的请指教,谢谢!
发表于 2005-5-20 09:43:49 | 显示全部楼层
低手的见解:
可能是c_lflag设置成non-canonical的了吧,看看你是怎么设置的?下面是从Serial-Programming-HOWTO copy的一段
In non-canonical input processing mode, input is not assembled into lines and input processing (erase, kill, delete, etc.) does not occur. Two parameters control the behavior of this mode: c_cc[VTIME] sets the character timer, and c_cc[VMIN] sets the minimum number of characters to receive before satisfying the read.

If MIN > 0 and TIME = 0, MIN sets the number of characters to receive before the read is satisfied. As TIME is zero, the timer is not used.

If MIN = 0 and TIME > 0, TIME serves as a timeout value. The read will be satisfied if a single character is read, or TIME is exceeded (t = TIME *0.1 s). If TIME is exceeded, no character will be returned.

If MIN > 0 and TIME > 0, TIME serves as an inter-character timer. The read will be satisfied if MIN characters are received, or the time between two characters exceeds TIME. The timer is restarted every time a character is received and only becomes active after the first character has been received.

If MIN = 0 and TIME = 0, read will be satisfied immediately. The number of characters currently available, or the number of characters requested will be returned. According to Antonino (see contributions), you could issue a fcntl(fd, F_SETFL, FNDELAY); before reading to get the same result.

By modifying newtio.c_cc[VTIME] and newtio.c_cc[VMIN] all modes described above can be tested.
回复 支持 反对

使用道具 举报

发表于 2005-5-20 09:58:46 | 显示全部楼层
这是读真实串口,又不是虚拟终端或者vt100终端。绝对应该采用non-canonical(非行规)。
至于只返回8个字节,我觉得是你的串口外设的问题。他的数据锁存器也许是只有64位的。
要不就是串口接口控制器驱动的问题,查查内核的串口源文件,看看它是否把串口的input buffer只设置为了最大64位。
回复 支持 反对

使用道具 举报

发表于 2005-5-20 10:07:50 | 显示全部楼层
好消息啊,我找到了你想要的。
首先打开设备不能采用O_NDELAY选项,
另外要通过tcsetchar()设置Control Characters里面的VMIN为128。另外再设置一个足够等到这128位数据的时间VTIME。
如此,就ok了。
回复 支持 反对

使用道具 举报

发表于 2006-8-28 17:12:34 | 显示全部楼层
O_NDELAY方式打开的话,会不会阻塞死了?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表