FAQ: Losing serial data on Serial Port

Discussion on device driver codes

FAQ: Losing serial data on Serial Port

Postby admin » Mon Nov 06, 2006 10:22 pm

Losing serial data on Serial Port

Serial Receive Architecture:
The flow of incoming serial data is as follows:

* Data is sent over the wire
* UART receives the data and stores it in an internal 128 element FIFO
* The serial poller thread runs, it reads the data out of the UART FIFO and stores it in the SerialInputStream's internal buffer. By Default this buffer is 1024 bytes.
* User program reads the data from the SerialInputStream and buffers or processes the serial data.

Reasons that serial data might be lost:

* The UART throws away data
It takes 133ms to fill an empty UART FIFO at 9600 baud, less time if the FIFO is not empty. If the polling thread does not get a chance to run the UART 's internal FIFO will overflow. The garbage collector, runs at a higher priority than the polling thread. Long garbage collection times will starve the serial polling thread causing UART FIFO to overflow and lose data.
Ways to avoid this:
o minimize the amount of time spent garbage collecting. Realize that collecting still takes time even if there is no garbage. Since all objects have to be swept.

* The SerialInputStream throws away data
It is the developers responsibility to ensure that the SerialInputStream is serviced at a rate to ensuring that the internal input buffer does not overflow. If the internal buffer overflows it has no place to put the data, and starts throwing away data.

Note, Wrapping the SerialInputStream, with a BufferedInputStream in no way ensures that the SerialInputStream is serviced in a manner to prevent data from being lost.

Ways to avoid the SerialInputStream from throwing away data:
o create a thread that empties the serial input stream into a user program buffer at a fixed rate which is faster than the rate data is being received.
o minimize the amount of processing done while read data from the serial input stream.
o avoid using the javax.comm serial event listener for DataAvailable events, or if you do make sure you don't block the thread.
o don't call System.out in the read loop.
Site Admin
Posts: 80
Joined: Sat Oct 28, 2006 6:14 am

Return to Device Drivers

Who is online

Users browsing this forum: No registered users and 1 guest