Page 1 of 1

Extra character on UART

Posted: Sun Oct 09, 2016 10:44 am
by dhallgb
I'm using the WiPy 1.0 with the Waveshare e-ink display. That display needs a specific command string over UART. In attempting to send that string format I seem to get extraneous characters inserted in the string. To test, I set up a loopback connecting pins 12 and 13 with a small code fragment:

[code]
from machine import UART
uart = UART(0, baudrate=115200, pins=('GP12', 'GP13'))
print("UART:",uart)
s1 = "test"
print(uart.write(s1))
print(uart.readall())
s2 = "\xA50900CC33C33CAC"
print(uart.write(s2))
print(uart.readall())
[/code]

On running this I get something odd:

[code]
UART: UART(0, baudrate=115200, bits=8, parity=None, stop=1)
4
b'test'
16
b'\xc2\xa50900CC33C33CAC'
[/code]

Where does that leading hex '\xc2' come from?

Re: Extra character on UART

Posted: Sun Oct 09, 2016 5:43 pm
by pythoncoder
You may be encountering a Unicode issue: in Python3 strings are Unicode by default. Your first character lies outside the ASCII range and may be being represented as two bytes. UARTs, of course, work bytewise. Try declaring the data for transmission as a bytes object thus:

Code: Select all

S2 = b"\xA50900CC33C33CAC"

Re: Extra character on UART

Posted: Wed Oct 12, 2016 9:07 pm
by dhallgb
Thank you for that suggestion which was correct and led me to bytearrays, the ustruct module and fun with 'pack_into'. My library for the Waveshare e-ink display running on the WiPy is at https://github.com/dhallgb/eInk-micropython - it may also work on others such as the ESP boards but I have not tested those yet.

Re: Extra character on UART

Posted: Thu Oct 13, 2016 1:03 pm
by rcolistete
Thanks for this nice new driver.
Please post in the Driver section (viewforum.php?f=14) so more MicroPython users can see your contribution.