UART write human-readable ASCII text

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
gatorchu
Posts: 25
Joined: Sun Sep 22, 2019 3:50 pm

UART write human-readable ASCII text

Post by gatorchu » Wed Jul 01, 2020 1:11 am

First of all, I noticed that the UART class in both MicroPython and Pycom shared same syntax, so I post my question in this category. hopefully, I don't break the rule.

I'm working on Lopy4 interfacing with Nextion serial display (http://nextion.tech/instruction-set/#s6).

To send the data to the monitor for displaying, I must follow their rules (see below Arduino code for instance).

Code: Select all

//Serial print ASCII text "voltage1.val=" to the display serial port
Serial.print("voltage1.val=");     
//Then Print the value we want to be displayed to the display serial port     
Serial.print(Value);                        
//Always terminated with 3 full bytes after... 
Serial.write(0xff);                               
Serial.write(0xff);
Serial.write(0xff);
Sample code from https://www.electronoobs.com/eng_arduino_tut60_1.php

I had tried to migrate the Arduino code to Lopy4, but never make it works. After investigating, my understanding is that

Code: Select all

uart.write()
can not accomplish the work that

Code: Select all

Serial.print()
in Arduino does. Because the former write a series of bytes to the serial port, while the latter one prints human-readable ASCII text to the serial port.

So my question is can UART port write human-readable ASCII text? Please give some instructions. Thank you!

User avatar
dhylands
Posts: 3482
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART write human-readable ASCII text

Post by dhylands » Wed Jul 01, 2020 6:25 pm

Sure.

Code: Select all

uart.write('This is a test\r\n')
is perfectly valid code in MicroPython.

And if I jumper the Tx and Rx signals together, then I can read it back too:

Code: Select all

>>> u = pyb.UART(6,115200)
>>> u.write('This is a test\r\n')
16
>>> r = u.read()
>>> print(r)
b'This is a test\r\n'
>>> r.decode()
'This is a test\r\n'
>>> 
Note that when reading you'll get back bytes. You can convert that to a regular string by calling decode()

It's important to understand the difference between string objects like 'Test' and bytes b'Test'

string objects are UTF-8 encoded, whereas bytes are just a collection of bytes and may include sequences which are not valid UTF-8.

The Arduino doesn't understand UTF8 and everything there is really bytes.

User avatar
Roberthh
Posts: 2228
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART write human-readable ASCII text

Post by Roberthh » Wed Jul 01, 2020 7:47 pm

Python does not automagically convert non-string items to strings, like the Serial class of Arduino does. You can use the str() function of Python to get a strings representation of non-string objects. Of you use the formatting operators % or the format method of strings.

User avatar
dhylands
Posts: 3482
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART write human-readable ASCII text

Post by dhylands » Wed Jul 01, 2020 10:44 pm

Sorry I missed the point of that post. It turns out that you can use a UART as a file device with print as well.

Code: Select all

>>> u = pyb.UART(6, 115200)
>>> x = 42
>>> y = 'foo'
>>> z = {'a':32}
>>> print('x =', x, 'y =', y, 'z =', z, file=u)
>>> u.read()
b"x = 42 y = foo z = {'a': 32}\n"
>>> 
The above was done on my Pyboard 1.0 with Y1 jumpered to Y2.

gatorchu
Posts: 25
Joined: Sun Sep 22, 2019 3:50 pm

Re: UART write human-readable ASCII text

Post by gatorchu » Thu Jul 02, 2020 3:36 am

Thank you, dhylands and Roberthh!
I got it works right now! Your explanation helps a lot!

Post Reply