Reading UART0 bytes from RX

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
happyday
Posts: 12
Joined: Sat Jan 21, 2017 12:11 pm

Reading UART0 bytes from RX

Post by happyday » Sat Jan 21, 2017 3:28 pm

I have a CO2 sensor that uses a UART. The only UART that seems available is UART0. I am running MicroPython from WebREPL so the CO2 sensor can use the UART.
I create a main.py and upload. The gasdata has the right return bytes from the CO2 sensor (verified with a logic analyzer):
WORKS:

from machine import UART
if __name__ == '__main__':
uart = UART(0,9600)
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79')
uart.readinto(gasdata)
for value in gasdata:
print(value)

DOESN'T WORK (bytes in gasdata are all = 0):

from machine import Timer
from machine import UART
def getReadings():
co2 = UART(0,9600)
gasdata = bytearray(9)
co2.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79')
co2.readinto(gasdata)
for value in gasdata:
print(value)
def takeReadings():
getReadings()

if __name__ == '__main__':
t = Timer(1)
t.init(period=5000,mode=Timer.PERIODIC,callback=lambda t:takeReadings())

What am I missing to be able to get the correct bytes read and into gasdata? (what am i not understanding?)

thank you.

User avatar
deshipu
Posts: 1347
Joined: Thu May 28, 2015 5:54 pm

Re: Reading UART0 bytes from RX

Post by deshipu » Sat Jan 21, 2017 8:13 pm

I'm not sure, but perhaps you have to wait for the device to send the reply, not read it immediately.

happyday
Posts: 12
Joined: Sat Jan 21, 2017 12:11 pm

Re: Reading UART0 bytes from RX

Post by happyday » Sat Jan 21, 2017 8:35 pm

Thank you. From looking at the bytes within a logic analyzer and having to code work just within main.py, i do not think it is about waiting. Rather I think it might be the memory allocated to hold the incoming bytes of the UART0 are not being held onto so that the readinto() gets a chance to copy it into it's buffer. I don't know.

User avatar
pythoncoder
Posts: 3544
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Reading UART0 bytes from RX

Post by pythoncoder » Sun Jan 22, 2017 7:24 am

Assuming your buffer size is the same in the first example it should work. The uart.readinto method should block until either the buffer fills or a timeout occurs. If the UART timeout exceeds the timer period and you're getting fewer than 9 characters things might get re-entrant.

There are no buffer lifetime issues in your code as far as I can see. The buffer is declared as a local object and it's only used in the function body. It will be deallocated on return from the function because no references to it are in scope; but by then it has served its purpose.

Have you tried issuing takeReadings() at the REPL rather than in response to a timer callback? This would isolate possible re-entrancy issues.

I should perhaps warn that my MicroPython UART experience is restricted to the Pyboard: if there is an ESP8266 specific issue it needs identifying and reporting.
Peter Hinch

Post Reply