1

select()我有使用and读取串行端口的 Linux 程序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 );

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

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
~ #
4

0 回答 0