Skip to contentSkip to conversation

Serial port latency

DISCUSSION Geir Thomassen 5 participants 9 messages Mar 22, 2001 – Mar 23, 2001

lore
Share & tools
lore
#0 lore raw

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, [email protected]

#1 lore raw
Quoted 4 lines · Geir Thomassen

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.

Check out the man page for the "low_latency" configuration parameter in the setserial man page. This will cause the serial driver to burn a small amount of additional CPU overhead when processing characters, but it will lower the time between when characters arrive at the RS-232 port and when they are made available to the user program. The preferable solution is to use a intelligent windowing protocol that isn't heavily latency dependent (all modern protocols, such as kermit, zmodem, tcp/ip, etc. do this). But if you can't, using setserial to set the "low_latency" flag will allow you to work around a dumb communications protocol.

- Ted

#2 lore raw
In reply to Geir Thomassen
Quoted 7 lines · Theodore Tso
Quoted 4 lines · Geir Thomassen

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.

Quoted 11 lines

Check out the man page for the "low_latency" configuration parameter in the setserial man page. This will cause the serial driver to burn a small amount of additional CPU overhead when processing characters, but it will lower the time between when characters arrive at the RS-232 port and when they are made available to the user program. The preferable solution is to use a intelligent windowing protocol that isn't heavily latency dependent (all modern protocols, such as kermit, zmodem, tcp/ip, etc. do this). But if you can't, using setserial to set the "low_latency" flag will allow you to work around a dumb communications protocol.

I have tried this on both a stock Redhat 7.0 kernel and on 2.2.19pre5 with your serial-5.05 driver, and I could not measure any difference with and without the "low_latency" parameter to setserial ....

#setserial -a /dev/ttyS1
/dev/ttyS1, Line 1, UART: 16550A, Port: 0x02f8, IRQ: 3

	Baud_base: 115200, close_delay: 50, divisor: 0
	closing_wait: 3000
	Flags: spd_normal skip_test low_latency

The serial port chip is 16550A, which has a built in fifo. Can this be the source of my problems ?

Geir

#3 lore raw
In reply to Geir Thomassen
Quoted 4 lines

The serial port chip is 16550A, which has a built in fifo. Can this be the source of my problems ?

Geir

I thought about that. If the number of receive bytes in the RX FIFO is less that the trigger level then a timeout has to occur before getting the next receive data interrupt.

The 16550AF data book says that this timeout is 4 characters from when the last byte is received. This is a maximum of 160ms at 300bps (when using 12bit characters: 1 start + 8 data + parity + 2 stop).

So this would be smaller at 9600 and could not account for the 500ms delay.

Paul Fulghum [email protected]
Microgate Corporation www.microgate.com

#4 lore raw
In reply to Geir Thomassen
Quoted 15 lines · Jim Penny
Quoted 10 lines · Geir Thomassen

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).

Quoted 4 lines

500ms seems a bit too long, but the quick answer is scheduler latency. You will certainly be scheduled out by the read, after all, it is not your program that is doing the read, but the kernel. I think you are scheduled out after the write, as well.

I do believe the write will make the kernel write the first byte into the UART transmit buffer directly. If this is the case, then the data should appear on the serial lines almost immediately. There might be a delay before the bottom half handler process the byte (or is the bottom half only used on receive ?), but I do believe that this happens before the next process is scheduled (BH handlers are all finished before any syscalls returns). If this is the case, then scheduling out on write should not do any harm.

Quoted 8 lines

Your task is then sitting idle until the scheduler's next pass, if nothing else is happening, an interrupt is scheduled on the RTC, the CPU is set to idle power, and your system sleeps until the RTC wakes it back up, and the schedule queue is re-examined.

This normally implies a wait of at least a jiffy after every read.

Isn't a jiffy 10ms on i386 ? Scheduling delays should not be any more than say 50 ms, but I am measuring 500ms. I could live with 50ms, which would make my program finish in 3 min. Now it is using 30 min !!

Quoted 2 lines

(But your time seems really excessive.)

Yep :-(

Quoted 3 lines

The usual cure is to write a custom driver which does a single read/write command. If, as I recall, you are scheduled out on reads as well as writes, this lets you get rid of half the jiffies.

That could be the solution to my problem, but I won't start on that before I understand what the problem is ...

Geir, [email protected]

#5 lore raw
In reply to Geir Thomassen
Quoted 10 lines

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 */
}

What are your settings for VTIME and VMIN?

Signature · 1 line
/Jonathan Lundell.
#6 lore raw
In reply to Geir Thomassen
Quoted 3 lines · Geir Thomassen

The serial port chip is 16550A, which has a built in fifo. Can this be the source of my problems ?

Well, if you set the uart to be 16450 using setserial, this will cause Linux to avoid enabling the FIFO. That will cause the loop to save the 4 character times (which at 9600 bps is 4ms). If your original protocol is writing six characters, and then reads 2 characters in a tight loop, that means a total cycle takes 8ms, and disabling the FIFO will have significant savings assuming that all other causes of latencies have been removed. (The FIFO delay can cause a slowdown by 50%).

- Ted

#7 lore raw
In reply to Geir Thomassen

Hi!

Quoted 22 lines

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 */
}

Quoted 4 lines

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 ?).

Set HZ=1000 in include/asm/params.h and see if it helps.

Pavel

Signature · 2 lines
I'm [email protected]. "In my country we have almost anarchy and I don't care."
Panos Katsaloulis describing me w.r.t. patents at [email protected]
#8 lore raw
In reply to Geir Thomassen

Problem solved !

The main problem was that I didn't know that the equipment used DTR as character acknowledge (not RTS/CTS which is common for flow control). I didn't even bother to look at those lines with the oscilloscope. Now the software runs fine with negligible latency.

Now the total latency is so low that the low_latency option to setserial really matters, it reduces the total run time of my program with 38 %

I don't get any measurable speed improvement by forcing the UART to be detected as a 8250 or a 16450 though.

I have not tested with HZ=1000.

All in all, my problem is solved, thank you all.

Geir