read function in python serial

Peter Hansen peter at engcorp.com
Thu Aug 11 08:37:16 EDT 2005


sinan . wrote:
> hi i`m developing a program that reads the serial device. i like the
> readline() function, but readline() depends on \n character, i want a
> similar function that waits for a specific character or string that i
> gave like [ETX] (hex03) how can i do this ?
> thank you.

def serial_readline(port, eos='\n'):
     '''read from port until \n or specified end-of-string is found'''
     data = ''
     while 1:
         data += port.read(1)
         if data.endswith(eos):
             return data


This is just a starting point.  If it's not adequate, please clarify 
your needs.

-Peter



More information about the Python-list mailing list