Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120751
  • 博文数量: 17
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-29 13:33
文章分类

全部博文(17)

文章存档

2009年(3)

2008年(14)

我的朋友

分类:

2008-06-21 11:54:47

因为今天做串口通讯的时候发现一个怪现象,在虚拟机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 通讯的朋友一点帮助!

阅读(3306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~