Win32/Python serial port event character problem

Derek Basch dbasch at yahoo.com
Sat Jun 29 23:14:23 EDT 2002


Thanks for the ideas Peter and Chris. I would use
pySerial but I have spent the last few months building
my own serial module that is very similiar to pySerial
but with some extra features. It was probably silly of
me to build my own from scratch but it was a good
challange and I certainly understand serial ports much
better now. So you think I should ditch the event
character and just read a preset number of characters
from the buffer on each loop? That sounds like a good
idea for OS compatability. I will use something like
this (from pySerial):
http://www.geocities.com/dbasch/tempers.txt

I dont understand what Chris is doing in the middle
though. Maybe you can answer that Chris?

I hadn't had a chance to write any code for detecting
the termination character and breaking up the data but
your pseudocode was just what I needed Peter.
Thanks!,
Derek Basch

P.S. Next time I will be more patient. Need to drink
less tea ;)  

Derek Basch wrote:
> 
> If anyone reads this and can show me a better way to
> work with serial event characters in win32 please
feel
> free to :)

I second Chris' comments about using something like
PySerial
and just handling this in a thread. Event characters
are
probably non-portable, while you could easily write
code
with PySerial that runs under Linux or Windows.

But give it time... you definitely didn't give it
enough time
before you reposted. :)

By the way, here's some typical (?) code for handling
a termination 
sequence on incoming data for something like this
(while inside of
some kind of serial port object):

def read(self, timeout=None):
buf = self.leftover
self.leftover = ''

while true:
data = FUNCTION_TO_READ_A_RAW_BLOB_OF_DATA()

# if new data received, add to the buffer and check
for
# a contained input termination sequence
if data:

buf = buf + data

termIndex = string.find(buf, self.inputTerminator)
if termIndex >= 0:
# move past the terminator
termIndex = termIndex + len(termIndex)

# preserve anything lying after input termination 
# sequence in the leftover buffer for next time
buf, self.leftover = buf[:termIndex], buf[termIndex:]

break

return buf

Each time you call it, it reads raw data and adds it
to a buffer
which it then rescans for the termination sequence. If
the buffer
contains the sequence, it splits it on that and
returns everything
up to and including the sequence, preserving
everything after it 
for the next call.

-Peter

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com





More information about the Python-list mailing list