z Windows serial communication

Spencer Doidge spencer at spencerdoidge.com
Sat Feb 2 21:40:26 EST 2002


Many thanks and kudos to Issac Barona for his serial communications Python
enhancements, which I downloaded from

>From this collection, which I have been using and studying, I have been
examining SerialPort_win.py, and I cannot find where there is an input
buffer that I can access after calling self.read(n). I do not know Python
well enough yet to modify SerialPort_win.py, but I will try to learn now.

Of course, I will welcome any help from anyone willing to save me a little
time. I have emailed to Mr. Barona, but he has not replied.

I notice that self.read(n) generates an exception when Win32 API's
ReadFile(...) times out. For my own purposes, I prefer not to generate an
exception unless ReadFile() returns FALSE, denoting a communication error
such as a framing error or parity error. In particular, my understanding of
ReadFile(..) is that it treats a timeout as a normal event and returns TRUE
when there is a timeout, even if lnNumberOfBytesToRead points to a different
value than lpNumberOfBytesRead. I can detect a failed receive operation this

if (lnNumberOfBytesToRead != *lpNumberOfBytesRead){
// protocol error: did not receive expected number of bytes

When I am receiving a packet, I decide that the sender is finished sending
when I have waited long enough after the last received character, that is,
when ReadIntervalTimeout as set in SetCommTimeouts(..) is allowed by the
sender to expire. It is after that event that I parse the received message.

I have modified SerialPort_win.py accordingly.
I will be glad to share my work with anyone interested, and I will gladly
accept anyone's help.

I have written TP3, a small multithreaded Win32 API app (VC++ project) that
sends and receives, via the serial port, binary data translated from ASCII
entered on the keyboard. If you have a 2nd PC and a null-modem cable to
connect your two PCs, this makes a nice way to test Mr. Barona's software.
Since the app is changing from day to day, I will wait for someone to
express an interest before posting the source somewhere.

Spencer Doidge

More information about the Python-list mailing list