Checking for data on a serial port

Grant Edwards grant at nowhere.
Tue Mar 28 16:50:52 EST 2000


In article <38E10C5D.895467C1 at earthlink.net>, Jay Freeman wrote:

>I'm probably missing something obvious in the docs, but I'd like to have
>a way to see if there is any data waiting to be read on a serial port.
>I'm doing a touch screen driven app on RedHat 6.1. I've tried two
>methods of reading the serial port and they both behave the same way.
>I've just opened /dev/ttyS0 for read, and used readline(), and I've
>redirected stdin and used raw_input(). Both methods just sit on that
>statement and wait until some data comes in. What I'd like to do is
>check and see if there is any data, if there is then read it, if not
>then check the time against a default timeout value so I can kill some
>intermediate screen that the user has walked away from and start the
>session over (it's a kiosk). Neither termios or tty modules seem to be
>able to do what I want (although termios is handy for flushing extra
>touches out of the buffer). Any ideas?

You need to use the file-descriptor based read() after opening
the device in non-blocking mode.  Look up the "os" module,
specifically os.open() and os.read().  Or, you can use fcntl.ioctl()
to change to non-blocking mode after the device is open.

-- 
Grant Edwards                   grante             Yow!  Why am I in this ROOM
                                  at               in DOWNTOWN PHILADELPHIA?
                               visi.com            



More information about the Python-list mailing list