Merge pull request #22 from 7west/master

A better example for UART on the Pico
pull/23/head
Alasdair Allan 2021-02-25 10:22:07 +00:00 zatwierdzone przez GitHub
commit 974a82a6f1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 40 dodań i 22 usunięć

25
uart/README.adoc 100644
Wyświetl plik

@ -0,0 +1,25 @@
= Using UART on the Raspberry Pi Pico
:xrefstyle: short
Send data from the UART1 port to the UART0 port.
== Other code to try
[source.python]
uart0 = UART(0) #opens a UART connection at the default baudrate of 115,200
uart0.readline() #reads until the CR (\r) and NL (\n) characters then returns the line
== Wiring information
See <<uart-wiring-diagram>> for wiring instructions.
[[uart-wiring-diagram]]
[pdfwidth=75%]
.Wiring two of the Pico's ports together. Be sure to wire UART0:TX to UART1:RX and UART0:RX to UART1:TX.
image::pico_uart_example.png[]
== List of Files
A list of files with descriptions of their function;
uart.py:: The example code.

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 148 KiB

Wyświetl plik

@ -1,22 +1,15 @@
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))
from machine import UART, Pin
import time
uart1 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9))
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
txData = b'hello world\n\r'
uart1.write(txData)
time.sleep(0.1)
rxData = bytes()
while uart0.any() > 0:
rxData += uart0.read(1)
print(rxData.decode('utf-8'))