Looking for terminator in data drom UART

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
michalt38
Posts: 2
Joined: Wed Mar 15, 2017 4:48 pm

Looking for terminator in data drom UART

Post by michalt38 » Thu Mar 23, 2017 9:08 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Looking for terminator in data drom UART

Post by dhylands » Fri Mar 24, 2017 12:05 am

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

Post Reply