I'm using a Pi3B+ to test the commands and functionality of PySerial. I'm slowly working my way through it, having lots of fun, but when I get to "write_timeout" I find it doesn't seem to do anything. The PySerial 3.4 documentation defines "write_timeout" thus:
Getter: Get current write timeout setting
Setter: Set write timeout
Type: float (seconds)
Read or write current write timeout setting.
I interpreted this to imply that, following a write command, characters would be transmitted only for the time duration specified by write_timeout, and that any subsequent characters waiting in the output buffer would be omitted. In other words, I took it to mean that write_timeout allowed us to specify a 'time window' during which bytes would be transmitted and at the end of the window transmission would cease. (I can't actually think of any reason why you would wish to do this, but I'm sure there must be a use for it somewhere).
Eager to test the write_timeout function, I wrote the following simple program:
Now, at 9600 baud, the character time is roughly 1ms, so my idea was that by setting write_timeout to 10ms, only the first ten characters of the "Hello World! Hello World!" string would be transmitted. I tested the program in 'loopback' mode (TX connected to RX) with a logic/protocol analyser connected to TX. (Incidentally, the third line is there simply to confirm that the write_timeout has been set and recognised correctly).
Code: Select all
import serial ser = serial.Serial(port='/dev/ttyS0', baudrate=9600, timeout=1, write_timeout=0.01) print (ser.write_timeout) ser.write(b'Hello World! Hello World!') print (ser.read(25))
To my surprise, the protocol analyser displayed the entire string and the program returned:
b'Hello World! Hello World!'
What on earth is going on? I tried several different values for write_timeout but it made no difference - the entire string was always transmitted.
What's interesting is that when I changed the read timeout to 10ms (i.e., timeout=0.01), the program behaved exactly as you would expect:
That is, only the first ten characters were read, the remainder were ignored.
So either I've completely misunderstood the write_timeout functionality, or it just doesn't seem to be doing anything.