read function in python serial

S

sinan .

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.
 
P

Peter Hansen

sinan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top