博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

pplinux

看庭前花开花落,荣辱不惊,望天上云卷云舒,去留无意。
   pplinux.cublog.cn
关于作者  
姓名:龙蚊
职业:网络民工
年龄:256
位置:zigbee定位
个性介绍:

我的分类  




关于linux串口,定时器与缓存的问题分析与解决
因为今天做串口通讯的时候发现一个怪现象,在虚拟机rh9中的用串口程序通讯,居然只能收到16个字符长的数据,而我的read(fd,buf,31);就是说我要一次跟plc通讯收31个字符的串,这样读的是31但每回只能收16个,考虑到buf[1024],而串口的缓存一定大于16btye的问题,想到问题可能出现在
newtio.c_cc[VTIME]    = 1;
newtio.c_cc[VMIN]  = 0;
这个地方,去官方看了下关于这个地方的说明:

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.

这样我们可以看出TIME是一个定时器而时间估计是100ms为单位,而MIN是一个字符间间植,

当我把

newtio.c_cc[VMIN]=0;
newtio.c_cc[VTIME]=31;

时发现31个的串可收,但如果其他的串小于31,串口会被中断堵塞

这样需要一个定时器来让他重新关闭,并把不足31的字符返回给buf里

所以可以写成

 newtio.c_cc[VMIN]=1;
 newtio.c_cc[VTIME]=31;

这样字符范围就在0-31之间的任意长度并在定时100ms内可以在次读去而不造成主塞现象

OK今天的一个小问题希望能给做arm 通讯的朋友一点帮助!

 发表于: 2008-06-21,修改于: 2008-06-21 11:54 已浏览114次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:13.59197

京ICP证041476号