Reduce Serial Latency

Hey guys

I really had some troubles reducing the serial latency between my computer and my arduino, it was always > 20ms, which was too much for my case. after some research I found a solution: I had to patch the rxtx lib, now the latency is < 2ms..

this blog entry Serial Latency: Teensy vs. Arduino has all the details you need. if you could do the benchmark yourself and post the results here, we could get a better picture...

cheers

On my windows machine, in the advance comport settings for the FTDI driver, there is a latency timer selection that can be set as low as 1ms (looks like the default is 16ms). I think this has to be tweeked sometimes when downloading programs into chips.

the two settings are related yes, but if you decrease the ftdi latency timer down to 1ms, rxtx still waits up to 20ms to forward the received packet to your application.

|---FTDI TimeFrame---|
|---RXTX Time Frame---|

With windows and simple user programs, 15ms is often as short a time as one can get windows to check a program for commands due to the "multitasking" of the OS.

you're talking about windows 3.11, right?

Seriously - I don't get it, where those 15ms are comming from? whats a simple user program? what does multitasking have to do with arduinos serial latency?

what does multitasking have to do with arduinos serial latency?

Your program isn't the only show in town, take a look at the task manager. Maybe the Windows kernel (or whatever they call it) only checks for task IO every 15mS.


Rob

It would be much better to run the benchmark and post actual latency measurements, rather than toss around wild speculation based on intuition or guesswork. Even if you don't have all 3 types of boards, running it on at least 1 board would be FAR better than zero.

You could even run the benchmark on an otherwise idle system, and with various other programs running. The code measures average and worst case latency. If you really wanted to make a point about multitasking, it'd be possible to modify the code to print each test run and put the results into a histogram or something.

Of course, the "native" code is written with Mac OS-X. Maybe someone could port it to WIN32? Maybe I'll do it... I did write the OS-X version afterall.

Does the java benchmark work on Windows? I didn't see the java code in the zip file. Did I just miss it?

you're talking about windows 3.11, right?

Ah, must be a mac user. :wink:

Maybe the Windows kernel (or whatever they call it)

"microkernel". But most folks (including those at Microsoft) call it a "kernel".

only checks for task IO every 15mS.

Nope. The time quanta is 10 milliseconds on the vast majority of hardware. In order to consume the entire quanta, an application has to make no operating system calls for the entire 10 milliseconds and run at an elevated priority (which is extremely rare). In addition, when an I/O operation completes (like reading from a serial port), the process waiting for the I/O operation gets a priority boost which typically results in that application being bumped to the top of the heap. Couple all that with the fact that most modern Windows computers have multiple processor cores, on a mostly idle computer, the vast majority of the time a process waiting for a serial port read will run immediately when data arrives.

The latency may come from the Java support library, the USB subsystem, or the driver but it does not come from the kernel.

I ported the code to Windows (so it works on Mac, Linux and Windows). Here it is:

http://www.pjrc.com/teensy/beta/latency_test.zip

To run the test, just program the latency_test.pde sketch onto your Arduino, Teensy or other Arduino compatible board.

The in a terminal or command prompt window, run the latency_test program. I included a pre-built copy for Windows. To compile, edit the Makefile and make sure your platform is selected, then compile with "make".

I'll post some results in a few minutes....

Arduino Uno on Windows XP, system idle:

port com5 opened
waiting for board to be ready:
..ok
latency @ 1 bytes: 4.06 ms average, 15.63 maximum
latency @ 2 bytes: 4.22 ms average, 15.63 maximum
latency @ 12 bytes: 4.22 ms average, 15.63 maximum
latency @ 30 bytes: 4.53 ms average, 15.63 maximum
latency @ 62 bytes: 8.13 ms average, 15.63 maximum
latency @ 71 bytes: 8.28 ms average, 15.63 maximum
latency @ 128 bytes: 13.13 ms average, 15.63 maximum
latency @ 500 bytes: 45.00 ms average, 46.88 maximum
latency @ 1000 bytes: 87.34 ms average, 93.75 maximum
latency @ 2000 bytes: 172.19 ms average, 187.50 maximum
latency @ 4000 bytes: 344.06 ms average, 359.38 maximum
latency @ 8000 bytes: 684.06 ms average, 687.50 maximum

Teensy 2.0 on Windows XP, system idle:

port com4 opened
waiting for board to be ready:
.ok
latency @ 1 bytes: 0.94 ms average, 15.63 maximum
latency @ 2 bytes: 1.09 ms average, 15.63 maximum
latency @ 12 bytes: 1.09 ms average, 15.63 maximum
latency @ 30 bytes: 0.94 ms average, 15.63 maximum
latency @ 62 bytes: 1.09 ms average, 15.63 maximum
latency @ 71 bytes: 0.94 ms average, 15.63 maximum
latency @ 128 bytes: 1.25 ms average, 15.63 maximum
latency @ 500 bytes: 3.91 ms average, 15.63 maximum
latency @ 1000 bytes: 6.56 ms average, 15.63 maximum
latency @ 2000 bytes: 12.19 ms average, 15.63 maximum
latency @ 4000 bytes: 23.91 ms average, 31.25 maximum
latency @ 8000 bytes: 47.19 ms average, 62.50 maximum

Teensy 2.0 on Windows 7 Home Premium, system idle:

port COM3: opened
waiting for board to be ready:
.ok
latency @ 1 bytes: 0.94 ms average, 15.60 maximum
latency @ 2 bytes: 0.94 ms average, 15.60 maximum
latency @ 12 bytes: 1.09 ms average, 15.60 maximum
latency @ 30 bytes: 0.94 ms average, 15.60 maximum
latency @ 62 bytes: 1.09 ms average, 15.60 maximum
latency @ 71 bytes: 0.94 ms average, 15.60 maximum
latency @ 128 bytes: 1.56 ms average, 15.60 maximum
latency @ 500 bytes: 3.90 ms average, 15.60 maximum
latency @ 1000 bytes: 6.55 ms average, 15.60 maximum
latency @ 2000 bytes: 12.01 ms average, 15.60 maximum
latency @ 4000 bytes: 24.18 ms average, 46.80 maximum
latency @ 8000 bytes: 46.96 ms average, 62.40 maximum

Arduino Uno on Windows 7 Home Premium, system idle:

port COM4: opened
waiting for board to be ready:
..ok
latency @ 1 bytes: 4.06 ms average, 15.60 maximum
latency @ 2 bytes: 4.06 ms average, 15.60 maximum
latency @ 12 bytes: 4.21 ms average, 15.60 maximum
latency @ 30 bytes: 4.37 ms average, 15.60 maximum
latency @ 62 bytes: 8.27 ms average, 15.60 maximum
latency @ 71 bytes: 8.11 ms average, 15.60 maximum
latency @ 128 bytes: 14.04 ms average, 46.80 maximum
latency @ 500 bytes: 45.08 ms average, 46.80 maximum
latency @ 1000 bytes: 87.52 ms average, 93.60 maximum
latency @ 2000 bytes: 172.07 ms average, 187.20 maximum
latency @ 4000 bytes: 343.98 ms average, 358.80 maximum
latency @ 8000 bytes: 683.90 ms average, 686.40 maximum

Arduino Uno on Linux, Ubuntu 10.10, 32 bit:

port /dev/ttyACM0 opened
waiting for board to be ready:
..ok
latency @ 1 bytes: 5.23 ms average, 10.04 maximum
latency @ 2 bytes: 5.16 ms average, 10.99 maximum
latency @ 12 bytes: 6.69 ms average, 13.23 maximum
latency @ 30 bytes: 8.68 ms average, 16.00 maximum
latency @ 62 bytes: 10.47 ms average, 14.56 maximum
latency @ 71 bytes: 11.69 ms average, 16.00 maximum
latency @ 128 bytes: 16.30 ms average, 20.50 maximum
latency @ 500 bytes: 48.28 ms average, 53.00 maximum
latency @ 1000 bytes: 90.53 ms average, 96.00 maximum
latency @ 2000 bytes: 175.15 ms average, 180.00 maximum
latency @ 4000 bytes: 344.12 ms average, 348.49 maximum
latency @ 8000 bytes: 686.79 ms average, 692.50 maximum

Teensy 2.0 on Linux, Ubuntu 10.10, 32 bit:

port /dev/ttyACM0 opened
waiting for board to be ready:
.ok
latency @ 1 bytes: 4.28 ms average, 8.00 maximum
latency @ 2 bytes: 4.02 ms average, 7.99 maximum
latency @ 12 bytes: 4.39 ms average, 8.01 maximum
latency @ 30 bytes: 4.55 ms average, 8.01 maximum
latency @ 62 bytes: 4.16 ms average, 8.00 maximum
latency @ 71 bytes: 4.71 ms average, 8.50 maximum
latency @ 128 bytes: 4.72 ms average, 8.01 maximum
latency @ 500 bytes: 7.41 ms average, 12.01 maximum
latency @ 1000 bytes: 9.07 ms average, 14.12 maximum
latency @ 2000 bytes: 16.20 ms average, 20.00 maximum
latency @ 4000 bytes: 27.68 ms average, 33.00 maximum
latency @ 8000 bytes: 49.24 ms average, 52.50 maximum

PJRC, thanks for the data!

Think there would be any Latency with a Ether/TCP to Modbus conversion? I will be using a converter. if this latency doesn't extend beyond (0.15-0.28 seconds) we're green.