Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 03-23-2011, 04:13 AM   #1
LQ Newbie
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Serial port output buffer lag

Hi guys!
This is my first post here, hope this is the best place to ask this question. It is definitely a linux problem (same code working fine on windows).
I'm developing an application in C++ (compiled with gcc), running on Ubuntu 10.4 guest OS on XP host. I need to implement MBus protocol and I'm stuck with serial port behavior.
I configure the port properly, then I try to write some data to the port. No matter the length of the buffer I want to write, on the output of the serial port I can see 2 first characters from the buffer, then a space (time delay of about a single char length), and then the rest of the buffer. The delay varies with baudrate (higher baud, smaller delay). Seen on oscilloscope, so I'm quite confident what's going on. All the buffer content is properly sent over the serial port, the only problem is the time delay after first 2 bytes...

Have you got any idea what can cause the problem and how can I get rid of it?

I don't think it is a VM problem, I tried to run this app on PC with native Ubuntu Hardy and the behavior was exact.


int initport()
int fd = 0;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
printf("\n SP failed!\n");
return fd;
fcntl(fd, F_SETFL, 0);
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 2400...
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
fcntl(fd, F_SETFL, FNDELAY);
printf("\nSP OK\n");//erial port opened succesfully, FD = %d\n", fd);
return fd;

int main(void)
int sfd = initport();

unsigned char bfr[100] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
while (1)
write(sfd, bfr, 12);
return 0;

Thanx in advance!
Old 03-24-2011, 12:11 PM   #2
Senior Member
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 3,228
Blog Entries: 10

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I have to be honest with you, (1) extreme kudos to you for doing the due diligence and writing the correct program first, testing it out, ... putting in a proper effort before sending out a question; however (2) I have also seen the programming examples much like you have, read, and re-read the Serial HOWTO and for the life of me, I can't get things to always work correctly by using the tc functions and fcntl.

So as a result, I fork() and exec() the following:

stty -F/dev/ttyS# 19200 raw -echo

And wait on the child to complete.

Believe me, I have 4 serial USB tty devices on an embedded system, all different data rates, from 9600 up to 115.2k and getting the ports into raw mode was extremely important for me to properly get my data without delays. For some reason if you don't do that, you get what I saw and what I think you were describing, sometimes you do not get the data until a later point; it's there, but delayed. And when you have async data of indeterminate length, it is a huge deal to be stuck waiting for part of it.

If you really do wish to give the program instructions a continued effort, then read the Linux Serial HOWTO, there's an async port example right in there, and one thing you're missing is a fcntl call that includes FASYNC, and I think you are supposed to set ICANON, not clear it as you have.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial port UART FIFO buffer override Mar_kas Linux - Hardware 2 05-13-2010 12:47 AM
expecting Serial port output to a buffer nathan Programming 5 09-22-2009 03:29 PM
serial port not filling buffer entirely windell Programming 1 08-05-2008 10:12 AM
serial port lag mogumbo Linux - Hardware 6 03-26-2008 11:42 AM
Serial Port Buffer Overrun damiendusha Linux - Hardware 1 06-22-2006 02:56 AM

All times are GMT -5. The time now is 03:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration