Reading UART0 bytes from RX
Posted: 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.
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.