我有Linux程序,使用 select()read() 读取串行端口 . 它工作正常,但存在性能问题:

程序每个 read() 调用仅接收1-4个字节的数据 . 这会导致不必要的开销 .

为了避免这个问题,我将VTIME设置为20(2秒),将VMIN设置为9 .

tcgetattr( fd, &termiosv );

termiosv.c_cc[VMIN]  = 9;
termiosv.c_cc[VTIME] = 20;

tcsetattr( fd, TCSANOW, &termiosv );

但是当只有1-4个字节可用时, select() 会立即返回 . 跟踪程序:

2012.03.31 14:31:34  Data from serial:
0000: 7E A0                           

2012.03.31 14:31:34  Data from serial:
0000: 07 21 03 71                     

2012.03.31 14:31:34  Data from serial:
0000: 13 63 7E

我的问题是:

VTIME和VMIN串行端口选项是否适用于 select

如果它应该工作,为什么 select 太早返回?

使用非规范模式 . 串口配置是:

~ # stty -F /dev/ttySC0 -a
speed 9600 baud; rows 24; columns 80;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O;
min = 9; time = 20;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke
~ #