pySerial - Is there a way to select on multiple ports at once?

前端 未结 1 1026
北海茫月
北海茫月 2021-01-19 01:26

I am developing app that need to communicate with many serial ports. I havnt found a way to do this without using thread per port. Is there a way to do this with single thr

相关标签:
1条回答
  • 2021-01-19 01:40

    I'm assuming you are using PySerial on a unix like platform...

    Since PySerial objects implement fileno() to get the underlying file descriptor you can pass them straight into select which will allow you to do deal with multiple PySerial objects at once.

    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 simplest method.

    A third alternative would be to use twisted serial ports if you don't ming getting your head round the way twisted does things.

    Update

    For Windows, pretty much your only alternative other than using threads is to use the inWaiting() method. Poll all your serial ports regularly reading inWaiting() from them. If there is stuff waiting then you can read that and only that many bytes without blocking.

    Unfortunately pyserial doesn't have a "how much free space is there in the output buffer" method which means that when you write to serial ports you are at risk of blocking. If you are implementing a typical serial port protocol the default buffer sizes of a few kilobytes will ensure that this isn't normally a problem.

    0 讨论(0)
提交回复
热议问题