1

I have Linux program that reads a serial port by using select() and read(). It works fine, but there is a performance problem:

Program receives data only 1-4 bytes per single read() call. And that causes unnecessary overhead.

I set VTIME to 20 (2 seconds) and VMIN to 9, for avoiding the problem.

tcgetattr( fd, &termiosv );

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

tcsetattr( fd, TCSANOW, &termiosv );

But still select() returns immediately when there is only 1-4 bytes available. Trace from the program:

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  

My questions are:

Does VTIME and VMIN serial port options work with select?

If it should work, why select returns too early?

Non-canonical mode is used. Serial port configuration is:

~ # 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
~ #
SKi
  • 8,007
  • 2
  • 26
  • 57
  • 1
    When you say "a performance problem", what exactly do you have in mind? Multiple calls to `read()`? – NPE Jan 31 '12 at 13:02
  • Yes, lots of select() and read() calls causes quite much CPU-load. CPU of my device is not so fast. – SKi Jan 31 '12 at 13:30
  • `select` returns either on timeout or when one of the descriptors in one of the sets is "ready". That means if you have a descriptor in the read set, and there is data to read from that descriptor, no matter how little or how much; even one byte is enough, then `select`will return. – Some programmer dude Jan 31 '12 at 13:39
  • Ok, so I can't use VMIN & VTIME for solving my problem. With blocking fd, it seems to work the following way: Select() returns after when 1 byte is received, but read() calls blocks for 2 seconds, if there isn't 9 bytes to read :( – SKi Jan 31 '12 at 14:30

0 Answers0