nonblocking reads in windows?

Joe Connellan joec at mill.co.uk
Tue Aug 27 07:03:55 EDT 2002


unfortunately select does not work with files in win32. I've tried the win32all (win32file)
extensions and cannot get them to talk to my com port properly.

Thanks

Joe

Noah wrote:

> Joe Connellan <joec at mill.co.uk> wrote in message news:<3D660F45.F2C0BE8E at mill.co.uk>...
> > I have opened a serial port and if there is nothing there to be read
> > os.read() hangs whereas I want it to return "".
> >
> > if I try to open the fd with
> > port = os.open("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
> (http://starship.python.net/crew/mhammond/) has demo code for
> talking to the com ports. It looks very different from this.
> The demo is called win32comport_demo.py. 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 = os.open('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 http://www.python.org/dev/doc/devel/lib/module-select.html#l2h-1997
> r, w, e = select.select([port_fd], [], [], 0)
> if port_fd in r:
>     print os.read (port)
>
> Yours,
> Noah




More information about the Python-list mailing list