From 873ab463942bc44c57c0fa097ae07605b7971416 Mon Sep 17 00:00:00 2001 From: Alasdair Allan Date: Tue, 23 Feb 2021 09:43:04 +0000 Subject: [PATCH] Added UART example https://github.com/raspberrypi/pico-micropython-examples/issues/19#issuecomment-783668548 --- uart/uart.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 uart/uart.py diff --git a/uart/uart.py b/uart/uart.py new file mode 100644 index 0000000..87bd2df --- /dev/null +++ b/uart/uart.py @@ -0,0 +1,22 @@ +from machine import UART, Pin +from time import sleep_us + +class myUART(UART): + def readUntil(self, termination, maxlen=-1, includeTermination=True): + result = '' + while maxlen < 0 or len(result) < maxlen: + if self.any(): + #print("here") + result += chr(self.read(1)[0]) + #print(result) + if result.endswith(termination): + if not includeTermination: + result = result[:-len(termination)] + break + sleep_us(10) + return result + +uart = myUART(0, baudrate=9600, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1) + +uart.write("AT+GMR\r\n") +print(uart.readUntil('OK',maxlen=-1, includeTermination=True)) \ No newline at end of file