how to speed up writing into the serial port in Linux?

I have written a simple program that writes into the serial port (blockingly) and measures the time it takes. writing 104 Bytes of data @ 19200 takes 58/59 msec.

Reading the same amount of data takes only 54 msec (making a simple math shows that 54 msec is the correct number).

How can i reduce the latency of writing into the serialport?? By the way my hardware is a Beagle Bone Black. My code follows:

QTime t;t.start();
int bytes_written = write(m_serialport_fd, data, data_size);
if(bytes_written != data_size)
   //handle error
tcdrain(m_serialport_fd);
qDebug()<<"Writing into serialport took" << t.elapsed();

Any hints is appreciated.

When you “write” to the serial port, you are really writing to a circular buffer. Then the OS will start moving the data from the buffer to the hardware serial peripheral, when “it gets around to it.”
The “latency” is in the OS.

So, you could re-write the kernel driver to respond faster, or write a PRU driver that did not depend on the OS to “get around to it.”

The easiest thing to reduce total latency is to just run the serial port faster. The OS overhead would remain the same, but the underlying transfer rate could be improved by a factor of 2 or 4 or … .

— Graham