Page 1 of 1
Difference between print() and uart.write()
Posted: Thu Apr 15, 2021 7:19 am
by microBoa
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?
Re: Difference between print() and uart.write()
Posted: Thu Apr 15, 2021 7:30 am
by pythoncoder
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'.
Re: Difference between print() and uart.write()
Posted: Thu Apr 15, 2021 7:50 am
by microBoa
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.
Re: Difference between print() and uart.write()
Posted: Thu Apr 15, 2021 1:48 pm
by stanely
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,
What happens when you do the following?
Code: Select all
uart.write(eval(repr(b'\xff\xfd\x18')))
Re: Difference between print() and uart.write()
Posted: Fri Apr 16, 2021 5:18 am
by microBoa
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!