Is it possible to use python to get True Full Duplex on a Serial port?

Hendrik van Rooyen hendrik at microcorp.co.za
Fri Aug 14 11:04:07 CEST 2009


In the past, on this  group, I have made statements that said that on Linux, 
the serial port handling somehow does not allow transmitting and receiving at 
the same time, and nobody contradicted me.

I am running into the self same issue again.

What I normally do is to open the port like this:

port = open("/dev/ttyS0","r+b",0)

and then I unblock it with:

def unblock(f):
    """Given file 'f', sets its unblock flag to true."""

    fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

Then I can write a loop that uses a try-except to see if there are characters 
available, and that examines a queue to see if there is something to 
transmit, to give the appearance of full duplex functionality.

What I would really like is to have two threads - one that does blocking input 
waiting for a character, and one that examines an output queue and transmits 
the stuff it finds.

When I try to do this, it does not seem to work - as far as I can see, it is 
as if the underlying implementation is somehow single threaded - if it is 
waiting for a received character, it waits until something comes in before it 
will transmit anything.  So if you are talking to a device that does not 
respond, the whole thing freezes up waiting for a character that never comes, 
and nothing is transmitted either, despite the call to 
port.write(somestring).  The write blocks, and everything stops, waiting for 
the receipt to finish.

Is there a way to get full duplex, so that the transmit and receive are 
independent of each other?

Or are we stuck with a disk-like model that forces a sequence on reads and 
writes?

- Hendrik




More information about the Python-list mailing list