Difference between print() and uart.write()

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Difference between print() and uart.write()

Post by microBoa » Thu Apr 15, 2021 7:19 am

If I print this byte string I get the output I want, but if I try to send it with uart.write() I get gibberish.

Code: Select all

>>> print(b'\xff\xfd\x18')
b'\xff\xfd\x18'
>>> uart.write(b'\xff\xfd\x18')
ÿý3
I've tried various things from googling, but some of the functions aren't available in Micropython, and other things just don't work.
For example: can't convert 'int' object to str implicitly

Based on the number of questions I've seen, I'm not alone.

How can I get the output I'm expecting from uart.write()? What does print() do differently?

User avatar
pythoncoder
Posts: 5208
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Difference between print() and uart.write()

Post by pythoncoder » Thu Apr 15, 2021 7:30 am

I don't really understand what you're doing here, perhaps you could clarify. The purpose of print() and uart.write() are entirely different.

This is a session on a Pyboard:

Code: Select all

>>> from pyb import UART
>>> uart = UART(1)
>>> uart.write(b'\xff\xfd\x18')
3
>>> 
uart.write returns 3 because 3 bytes were transmitted on the UART. If a receiving UART were connected to the transmitting one, it would receive the three bytes b'\xff\xfd\x18'.
Peter Hinch

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: Difference between print() and uart.write()

Post by microBoa » Thu Apr 15, 2021 7:50 am

Currently I'm using print(), but I want to do uos.dupterm(None, 1) and still see the output I was getting from print() for debugging.
When I use print() the output I see is b'\xff\xfd\x18' (actually this is just sample output from a variable), this is what I'd like to see when I do uart.write().

print() still spits the output to the uart, so I'm trying to figure out what it does differently that gives me the output I'm expecting.

stanely
Posts: 54
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Difference between print() and uart.write()

Post by stanely » Thu Apr 15, 2021 1:48 pm

That's not gibberish. What you're seeing is that binary value mapped to the corresponding ASCII character. See here for \xff, https://www.codetable.net/hex/ff

Print is showing you a printable representation of your bytes. uart.write() is just spewing the bytes. There might be a way to use repr() to get closer to what you want with,

Code: Select all

uart.write(repr(b'\xff\xfd\x18'))
What happens when you do the following?

Code: Select all

uart.write(eval(repr(b'\xff\xfd\x18')))

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: Difference between print() and uart.write()

Post by microBoa » Fri Apr 16, 2021 5:18 am

I realize its not gibberish, but since it wasn't what I was I wanted...

Anyway, I figured it out. I think near the beginning of my attempts I tried this but still got the same error, now I know why.

Code: Select all

uart.write('SEQ/Len():      '+str(sequence).encode('ascii')+'\r\n')
The error I got was something like
can't implicitly convert string to int
I thought it was because of variable part of the string, but if you look closely I was missing the 'b' before the "SEQ/LEN()" and that's what was causing the error.

Now everything is working as expected. The whole byte-string thing with unicode and utf-8 is a little rough to get right sometimes.

Thanks for taking a look!

Post Reply