UART
-
- Posts: 7
- Joined: Fri Jul 29, 2022 5:47 pm
UART
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?
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?
-
- Posts: 7
- Joined: Fri Jul 29, 2022 5:47 pm
Re: UART
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.
how can I send only the FF 03 00 09 09 41 D6 exactly like this in hex and receive in hex my response.
Re: UART
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
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
-
- Posts: 7
- Joined: Fri Jul 29, 2022 5:47 pm
Re: UART
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)
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)
Re: UART
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'.
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'.
-
- Posts: 7
- Joined: Fri Jul 29, 2022 5:47 pm
Re: UART
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)
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)
Re: UART
No. Like this:
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.
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)
-
- Posts: 7
- Joined: Fri Jul 29, 2022 5:47 pm
Re: UART
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>
<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>