PySerial - is there a way to select multiple ports at once?

I am developing an application that needs to be connected to many serial ports. I havnt found a way to do this without using a thread on a port. Is there a way to do this with a single thread? Something like picking or polling on multiple ports at the same time? I am using pyserial 2.6

+4
source share
1 answer

I assume you are using PySerial on a unix platform ...

Since PySerial objects implement fileno () to get the base file descriptor, you can pass them directly to select , which allows you to work with multiple PySerial objects at the same time.

Another alternative would be to set nonblocking () and deal with the fact that your reads and writes may return errno.EWOULDBLOCK errors. This is probably the easiest way.

A third alternative would be to use twisted serial ports if you don't miss your head around how things are twisted.

Update

For Windows, pretty much your only alternative, besides using streams, is to use the inWaiting () method. Interrogate all your serial ports by regularly inWaiting() . If there are waiting things, you can read this and only that many bytes are non-blocking.

Unfortunately, pyserial does not have “how much free space there is in the output buffer,” which means that when writing to serial ports you run the risk of blocking. If you use a typical serial port protocol, a default buffer size of a few kilobytes will ensure that this is usually not a problem.

+5
source

All Articles