SocketSerial.read() never returns data when timeout==0
Brought to you by:
cliechti
Situation
I create a SerialSocket with timeout set to 0 and the try to read bytes from this socket when they become available:
import serial socketserial = serial.serial_for_url('socket://localhost:5000', timeout=0) while True: print socketserial.read(1)
Observed behaviour
read(1) always returns an empty string, regardless of data available at the socket.
Expected behaviour
I would have expected it to return a character at a time when data becomes available at the socket.
Potential solution
I believe this may be avoided by modifying the read loop of SerialSocket.read() at protocol_socket.py
, line 151 onward:
while len(data) < size and (timeout is None or time.time() < timeout): try: ...
to read:
while len(data) < size: try: ... if not (timeout is None or time.time() < timeout): break
so that at least one read is always attempted.
Thanks!
Rob
similar fix applied to https://github.com/pyserial/pyserial