nonblocking reads in windows?

Noah noah at
Fri Aug 23 23:12:09 CEST 2002

Joe Connellan <joec at> wrote in message news:<3D660F45.F2C0BE8E at>...
> I have opened a serial port and if there is nothing there to be read
> hangs whereas I want it to return "".
> if I try to open the fd with
> port ="COM1", os.O_RDWR | os.O_NONBLOCK)
> I get an AttributeError saying that os has no attribute O_NONBLOCK. And
> the windows select module doesn't support filehandles so it seems I
> cannot do it that way either.
> Is there any way I can do this on windows?
> Jon

O_NONBLOCK is a POSIX thing not supported.

Try the select module.

I'm not sure that you can open the COM port this way.
That may be your main problem.

This example may give you a hint. I could not test it because I could
not figure out how to open the COM1 port on my Windows machine using
the syntax you described. I know that the win32all library
( has demo code for
talking to the com ports. It looks very different from this.
The demo is called This is probably the route you want
to go since then you would not have to deal with the blocking problem in
the same way.

Assuming you can naively open the COM1 port that way then you
should be able to use select to do non-blocking poll.
The select part of your problem would look something like this...

import select, os

port_fd ='COM1', os.O_RDWR) # This didn't work for me...
    # This didn't work either:
    #     port = open ('COM1:', 'r')
    #     port_fd = port.fileno
    # Nor this:
    #     port = open ('com1', 'r+b')

# Note that timeout is zero.
# See
r, w, e =[port_fd], [], [], 0)
if port_fd in r:
    print (port)


More information about the Python-list mailing list