Page 1 of 1

Looking for terminator in data drom UART

Posted: Thu Mar 23, 2017 9:08 pm
by michalt38
Hello,
I need to catch a terminator \r in data I receive from UART and I've got this code:
[code]
data = ""
while True:

bytes = uart.read()
if bytes is not None:
idx = str(bytes).find("'", 2)
data += str(bytes)[2:idx]
print(data[len(data)-2:])
if data.find("\r") != -1:
print("Here\r\n")
[/code]
But data.find("\r") always returns -1 even when data contains \r. How to solve this problem?

Re: Looking for terminator in data drom UART

Posted: Fri Mar 24, 2017 12:05 am
by dhylands
I suspect that you're being tripped up by the difference between strings and bytestrings.

The UART returns bytestrings, so you should be searching for b'\r'

Code: Select all

MicroPython v1.8.7-201-ga34ff35 on 2017-02-07; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> data = b'abc\rdef'
>>> data.find('\r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert 'str' object to str implicitly
>>> data.find(b'\r')
3