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

I need to catch a terminator \r in data I receive from UART and I've got this code:
data = ""
while True:

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

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

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')

Post Reply