Serial port latency

Geir Thomassen
2001-03-22T18:24:37Z

Hi.

My program controls a device (a programmer for microcontrollers) via the
serial port. The program sits in a tight loop, writing a few (typical 6)
bytes to the port, and waits for a few (typ. two) bytes to be returned from
the programmer.

The program works, but it is very slow. I use an oscilloscope to monitor the
serial lines, and notices that there is a large delay between the returned
data, and the next new command. I really don't know if the delay is on the
sending or the receiving side (or both).

This is what the program does:

fd=open("/dev/ttyS0",O_NOCTTY | O_RDWR);

tcsetattr(fd,TCSANOW, &tio); /* setting baud, parity, raw mode, etc */

while() {
write( 6 bytes); /* send command */
read( 2 bytes); /* wait for reply */
}

The device on the serial port responds in typ. 10 ms, but the software uses
over 500ms to get the reply and send the next command. Why is this happening ?
I have a feeling that there is something obvious I am missing (like line
buffering, but that's a stdio (libc) thing, isn't it ?).

BTW, I am running a 2.2 kernel ....

Geir, geirt@powertech.no