Find the answer to your Linux question:
Results 1 to 3 of 3
Is there a trick to getting the tcflush() function to work on a serial port? I've got 2 systems, one XP and one Fedora 9 connected via their serial ports. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    2

    tcflush() not flushing serial port


    Is there a trick to getting the tcflush() function to work on a serial port?

    I've got 2 systems, one XP and one Fedora 9 connected via their serial ports. On each is a small test program to open the serial port, and if a button is pressed to send a test message: "hello World". Or if data is incoming, to read the data.

    I start the program on XP, open the port, then send the test message twice, which transmits the data to the Fedora box.

    Only then do I start the test program on the Fedora box. The beginning code
    for the openning logic is:
    port_ = ::open(portname, O_RDWR|O_NOCTTY|O_NONBLOCK|O_NDELAY);
    rc = tcflush(port_, TCIOFLUSH);
    ...

    My expectation was that the 2 messages which were sent BEFORE the port on the Linux box was opened, would be thrown away. But that doesn't happen. The test program on the Linux box reads all the data from the 2 messages right after it's open logic completes.

    If anyone can suggest how to actually flush the port of this "spurious" data I would enjoy hearing about it.

    Cheers!

    luvlearnn

  2. #2
    Just Joined!
    Join Date
    Sep 2008
    Location
    Hungary
    Posts
    1
    Hi,

    I've just faced the same problem.
    Here is the solution that works for me:

    Code:
    fd = open(portname, O_RDWR|O_NOCTTY);
    usleep(200000);
    tcflush(fd, TCIOFLUSH);
    Anyway, I still do not have any reasons why this solution works. Maybe the port has to settle after opening?

    Best regards,
    derzsil

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    2
    derzsil,

    Thanks! Your solution to insert the delay also worked with my code on the fedora box I'm using. Also, after getting your hint, I searched the web for the terms:
    linux +"serial port" +tcflush +delay

    adding "delay" made the difference and I hit on some additional posts that flesh out the problem a bit more. Try looking at:

    Linux-Kernel Archive: Fwd: PROBLEM: serial port receive buffers not being flushed properly

    for another discussion of the problem, with a list of linux kernel versions in which they found the problem (started in 2.6.20...) BTW my system is using :
    kernel release : 2.6.25.9-76.fc9.x86_64

    a later version than those mentioned in the link. Thanks for your time posting the reply!

    Cheers!
    luvlearnn

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •