Need to convert UART received data.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Need to convert UART received data.

Post by MMliam » Sat Jul 23, 2022 12:58 pm

Here's the code I'm testing.
SENDER ESP32:

Code: Select all

from machine import UART
uart=UART(2, 115200)
print(uart)

writeData = 'Hello\r\n'
uart.write(writeData)
print('\nData Sent: '+writeData)
OUTPUT:
UART(2, baudrate=115201, bits=8, parity=None, stop=1, tx=17, rx=16, rts=-1, cts=-1, txbuf=256, rxbuf=256, timeout=0, timeout_char=0)

Data Sent: Hello
RECEIVER ESP32:

Code: Select all

from machine import UART

uart=UART(2, 115200)
print('\n'+str(uart))
print('\nReceiver waiting......')

byteCnt = 0
while byteCnt==0:
    byteCnt = uart.any()
print(byteCnt)
recvData = uart.readline()
print('\nReceived Data: '+str(recvData))
OUTPUT:
UART(2, baudrate=115201, bits=8, parity=None, stop=1, tx=17, rx=16, rts=-1, cts=-1, txbuf=256, rxbuf=256, timeout=0, timeout_char=0)

Receiver waiting......
41

Received Data: b'\xbf\xff*dA@\x00\x00\x00\x80\x80\x88\x80\x80\x80\x00\x00\x00\x08 \x08\x00\x00\x00\x08\x88\x88\x80\x80\x00\x10 \x00\x81\xa3\xe2\xfa\xfb\xff\xff\xff'
Would like to print "Hello" at the receiver. I thought "readline" was supposed to return data up to CR-LF? Also the received data doen't even look right a Hex "H" is 48; don't even see it in the data.

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 3:28 pm

Did you connect the GND lines between the two boards?

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Need to convert UART received data.

Post by MMliam » Sat Jul 23, 2022 3:38 pm

I haven't physically connected the ground lines, but the two units are powered from the same USB host PC source, so the grounds should be common.

But I have some additional information, I reduced the RECEIVER instructions to the following:

Code: Select all

from machine import UART
import ubinascii

uart=UART(2, 115200)
print('\n'+str(uart))
And then had the SENDER send it's message ('Hello\r\n'), then read the received results in REPL; here's what I got:
>>> uart.read()
b'F\x00\x00\x00\x00\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x80\x80\x80\x80\xea\xff@\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x89\xff\xff\xff\x0f\x16@\x80\x88\x00\x00\x88\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\xfe&\x88\x80\x00\x00\x00\x00\x80\x00\x00\x00\x00\x80\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x02\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xf0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x80\x80\xfe\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xfc \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\xf7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00H\xf7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf4\xbf\xffD\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\x00\x88\xf6\x0f\x00\x00\x00\x00\x00\x00\x00\x00Hello\r\n'
You'll note the "Hello\r\n" is at the end, but for some reason the senders uart.write appears to be sending the entire buffer.

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 4:08 pm

The code should be:

from machine import UART
import ubinascii

uart=UART(2, 115200)
print('\n'+uart.read())

str(uart) is some representation of the uart object, not the data that was read by the uart.

Anyway you should connect the GND lines. Unless the USB cables are very short, the will pick up noise.

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 4:13 pm

Runing your code on my ESP32 results in:

UART(2, baudrate=115201, bits=8, parity=None, stop=1, tx=17, rx=16, rts=-1, cts=-1, txbuf=256, rxbuf=256, timeout=0, timeout_char=0)

Which is expected, since it prints the representation of the UART object.

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 4:15 pm

Try to use other pins for the UART. Pins 16 and 17 are sometimes used internally for other signals.

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Need to convert UART received data.

Post by MMliam » Sat Jul 23, 2022 4:51 pm

The code should be:

from machine import UART
import ubinascii

uart=UART(2, 115200)
print('\n'+uart.read())

str(uart) is some representation of the uart object, not the data that was read by the uart.
Actually, it was my intention to print the uart object.

Tried grounding; it didn't make any difference. The problem seems that the sender write is sending an the entire buffer, not just the string.
I may try re-mapping the pins; but according to the ESP-WROOM-32 pin-out, the default pins of UART2 are supposed to be free. Also, as I mentioned, the data is apparently sent with the entire buffer, so it's not like it's not working at all. It's almost as if the sender's uart.write('text') is doing a uart.write(buf) instead.

I do appreciate you help :)

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 6:59 pm

I loaded a WROOM device with most recent MicroPthon, and had you Send code running. No problem. Only "Hello\n\r" was sent. So I'm still assume it's picking up noise.
Are the wires and breadboard fine?
Can you do in a sequence:
a) set up sender and receiver UArt as first step,
b) run uart.read() to flush the input buffer form any picked up junk during init.
c) send the data
d) read the data.

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Need to convert UART received data.

Post by MMliam » Sat Jul 23, 2022 7:15 pm

The jumpers are standard stranded wire, about 15cm.
I have done a read to clear the buffer, followed by a write and I still get the garbage.

I will say the amount of garbage does vary from send to send, which suggests noise. But, the data at the end "Hello" is never corrupted.

Are you using any terminaters; pull-ups?

Did you perform an ESP32 to ESP32 for your test?

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

Re: Need to convert UART received data.

Post by Roberthh » Sat Jul 23, 2022 7:28 pm

I did not run esp32 to esp32, just esp32 to logic-analyzer, which has an input impedance of ~50kOhm. But I repeated the test with 2 ESP32 with not problems. Just the sent data "Hello\r\n" arrived.

Post Reply