Extra character on 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
User avatar
dhallgb
Posts: 5
Joined: Tue Oct 04, 2016 3:24 pm

Extra character on UART

Post by dhallgb » Sun Oct 09, 2016 10:44 am

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?

User avatar
pythoncoder
Posts: 1962
Joined: Fri Jul 18, 2014 8:01 am

Re: Extra character on UART

Post by pythoncoder » Sun Oct 09, 2016 5:43 pm

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"
Peter Hinch

User avatar
dhallgb
Posts: 5
Joined: Tue Oct 04, 2016 3:24 pm

Re: Extra character on UART

Post by dhallgb » Wed Oct 12, 2016 9:07 pm

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.

rcolistete
Posts: 90
Joined: Thu Dec 31, 2015 3:12 pm

Re: Extra character on UART

Post by rcolistete » Thu Oct 13, 2016 1:03 pm

Thanks for this nice new driver.
Please post in the Driver section (viewforum.php?f=14) so more MicroPython users can see your contribution.

Post Reply