UART

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
tom.ferreira
Posts: 7
Joined: Fri Jul 29, 2022 5:47 pm

UART

Post by tom.ferreira » Fri Jul 29, 2022 5:57 pm

Hey guys,

i'm using an esp32 to communicate with a sensor via UART and in order to receive a response from the sensor, it has to receive (FF 03 00 09 00 01 41 D6) from the esp. I'm not receiving anythings back and that makes me think that the problem is in the way i'm trying to send the array of hexdecimal:
uart.write(b'\xFF\x03\x00\x09\x00\x01\x41\xD6')

can someone help me?

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

Re: UART

Post by Roberthh » Fri Jul 29, 2022 8:01 pm

The data you send looks fine. Check the baudrate, bit numbers, polarity.

tom.ferreira
Posts: 7
Joined: Fri Jul 29, 2022 5:47 pm

Re: UART

Post by tom.ferreira » Sun Jul 31, 2022 6:41 pm

With my code when I print the uart.write I get b'\xff\x03\x00\t\x00\x01A\xd6'
how can I send only the FF 03 00 09 09 41 D6 exactly like this in hex and receive in hex my response.

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

Re: UART

Post by Roberthh » Sun Jul 31, 2022 6:48 pm

The printed data represent the values. print has the habit, to display every printable byte as character. So in the printed string \x41 is printed as A. You can create a HEX string with binascii.hexlify.

import binascii
print(binascii.hexlify(b'\xff\x03\x00\t\x00\x01A\xd6'))

return b'ff030009000141d6'. Another option is using the utility mentioned here: viewtopic.php?f=2&t=12788&p=69756&hilit=dump#p69756

tom.ferreira
Posts: 7
Joined: Fri Jul 29, 2022 5:47 pm

Re: UART

Post by tom.ferreira » Sun Jul 31, 2022 7:01 pm

This is the code. Its sending what you said it would, but it keeps returning "none" as uart.read. I think its the "b" in what im sending that is affecting the response of the sensor (b'ff030009000141d6'). I have to send ff 03 00 09 00 41 d6 to the sensor and receive another 8 bits array as response.

import machine
import time
from machine import Pin
import binascii

rts = machine.Pin(23, machine.Pin.OUT)
uart = machine.UART(2, 9600)

while True:
rts.on()
a = (binascii.hexlify(b'\xff\x03\x00\t\x00\x01A\xd6'))
uart.write(a)
print(a)
time.sleep(1)
rts.off()
data = uart.read()
print (data)

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

Re: UART

Post by Roberthh » Sun Jul 31, 2022 7:11 pm

The b' is not part of a, it's just the way a is printed. It you enter:

print(len(a))

you get 16, which is the number of bytes in a. Note, that binascii does not insert the space character between the bytes. If you wanr a string with spaces, you could e.g. use

cmd = b'\xff\x03\x00\t\x00\x01A\xd6'
hex = ' '.join(["%02X" % x for x in cmd])

Then hex contains the string 'FF 03 00 09 00 01 41 D6'.

tom.ferreira
Posts: 7
Joined: Fri Jul 29, 2022 5:47 pm

Re: UART

Post by tom.ferreira » Sun Jul 31, 2022 7:18 pm

Like this?:

import machine
import time
from machine import Pin
import binascii


rts = machine.Pin(23, machine.Pin.OUT)
uart = machine.UART(2, 9600)

while True:
rts.on()
cmd = (binascii.hexlify(b'\xff\x03\x00\t\x00\x01A\xd6'))
hex = ' '.join(["%02X" % x for x in cmd])
uart.write(cmd)
print(cmd)
time.sleep(1)
rts.off()
data = uart.read()
print (data)

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

Re: UART

Post by Roberthh » Sun Jul 31, 2022 7:40 pm

No. Like this:

Code: Select all

import machine
import time
from machine import Pin

rts = machine.Pin(23, machine.Pin.OUT)
uart = machine.UART(2, 9600)

while True:
    rts.on()
    cmd = b'\xff\x03\x00\t\x00\x01\x41\xd6'
    hex = ' '.join(["%02X" % x for x in cmd])
    uart.write(hex)
    print(hex)
    time.sleep(1)
    rts.off()
    data = uart.read)
    print (data)
Note that uart.read() returns immediately if there was no data yet, bit will arrive later. So it may be that the device already has sent data, but you did not wait for it.

tom.ferreira
Posts: 7
Joined: Fri Jul 29, 2022 5:47 pm

Re: UART

Post by tom.ferreira » Sun Jul 31, 2022 8:10 pm

FF 03 00 09 00 01 41 D6
<bound_method>
FF 03 00 09 00 01 41 D6
<bound_method>
1st line what i am sending
2nd line supost to be what i am receiving
do you know what is happening? before that i was receiving <none>

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

Re: UART

Post by Roberthh » Sun Jul 31, 2022 8:13 pm

should be:

data = uart.read()

Post Reply