'Any idea why I get a UnicodeError?
I tried to make a connection via UART between a Raspberry π pico and a wifi board (esp8266). The board sends data, the pico also receives. But when decoding I get a UnicodeError without description. The data are bytes in utf-8. Does anyone have an idea?
Here is my code:
1 from machine import UART, Pin
2 from time import sleep
3
4 uart0 = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
5
6 while True:
7 rxData = bytes()
8
9 while uart0.any() > 0:
10 rxData += uart0.read(1)
11
12 if len(rxData):
13 print(rxData.decode('utf-8'), end='')
Generates the following error:
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
UnicodeError:
The data as contains something like this:
...
b'eceiving'
b' from remote s'
b'erver\r\n"Here\'s th'
b'e rule for bargains'
b': "Do other men, f'
b'or they would do yo'
b'u."\r\n That\'s the tru'
b'e business precept.'
b'" Charles Dickens ('
b'1812-70)\r\x00\r\nclosin'
b'g connection\r\n'
...
Solution 1:[1]
I have had a similar problem today. The problem I am having is that either None is returned from the readline() or it's garbage. Both of which the decode does not like. To debug it, I just used a print to check the received data before the decode is done. I did not have this error in Python in Debian (as the readline() blocks, only now in Micropython (as the readline returns staright away). Maybe this helps you a bit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Graeme McPhillips |
