medivh medivh - 5 months ago 52
Python Question

PySerial write() instant timeout


I found out what the problem was and have answered my own question.

Original question below this line

I have a serial bridge between

implemented in software (Specifically, HDD's Free Virtual Serial Configuration Utility)

I have two different python scripts starting up in two different instances of Powershell, receive first:

import serial
receive = serial.Serial(port = 'COM5', baudrate = 9600)
text =
print text

And then the sender:

import serial
send = serial.Serial(port = 'COM4', baudrate = 9600, timeout = 0)

When starting the sender script, the receiver script gets the sent message (So communication is clearly established) but the sender script immediately ends with an error:

Traceback (most recent call last):
File ".\", line 3, in <module>
File "C:\Python27\lib\site-packages\serial\", line 270, in write
raise writeTimeoutError
serial.serialutil.SerialTimeoutException: Write timeout

I get the same error when I change the sender script to

send = serial.Serial(port = 'COM4', baudrate = 9600)

So my question is: What exactly is timing out? How do I prevent that from happening? I mean, the data IS being sent so I could probably just put the whole thing in a
try/except(and do nothing)
block but that seems like a bad solution in the long run.


The clue is in the error message[1]

File "C:\Python27\lib\site-packages\serial\", line 270, in write
raise writeTimeoutError

so we open that file and find:

if self._writeTimeout != 0: # if blocking (None) or w/ write timeout (>0)
            # Wait for the write to complete.
            #~ win32.WaitForSingleObject(self._overlappedWrite.hEvent, win32.INFINITE)
            err = win32.GetOverlappedResult(self.hComPort, self._overlappedWrite, ctypes.byref(n), True)
            if n.value != len(data):
                raise writeTimeoutError

Read that first conditional again:

if self._writeTimeout != 0:

so let us rewrite our code from before

send = serial.Serial(port = 'COM4', baudrate = 9600, timeout = 0)


send = serial.Serial(port = 'COM4', baudrate = 9600, writeTimeout = 0)

and Et Voila: No exception.

[1] Well Designed Error Messages? That's new!