Skip to content

Commit 974a82a

Browse files
author
Alasdair Allan
authored
Merge pull request #22 from 7west/master
A better example for UART on the Pico
2 parents 1b39602 + 8b1bf5f commit 974a82a

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

uart/README.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
= Using UART on the Raspberry Pi Pico
2+
:xrefstyle: short
3+
4+
Send data from the UART1 port to the UART0 port.
5+
6+
== Other code to try
7+
[source.python]
8+
uart0 = UART(0) #opens a UART connection at the default baudrate of 115,200
9+
uart0.readline() #reads until the CR (\r) and NL (\n) characters then returns the line
10+
11+
12+
== Wiring information
13+
14+
See <<uart-wiring-diagram>> for wiring instructions.
15+
16+
[[uart-wiring-diagram]]
17+
[pdfwidth=75%]
18+
.Wiring two of the Pico's ports together. Be sure to wire UART0:TX to UART1:RX and UART0:RX to UART1:TX.
19+
image::pico_uart_example.png[]
20+
21+
== List of Files
22+
23+
A list of files with descriptions of their function;
24+
25+
uart.py:: The example code.

uart/pico_uart_example.png

148 KB
Loading

uart/uart.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
from machine import UART, Pin
2-
from time import sleep_us
3-
4-
class myUART(UART):
5-
def readUntil(self, termination, maxlen=-1, includeTermination=True):
6-
result = ''
7-
while maxlen < 0 or len(result) < maxlen:
8-
if self.any():
9-
#print("here")
10-
result += chr(self.read(1)[0])
11-
#print(result)
12-
if result.endswith(termination):
13-
if not includeTermination:
14-
result = result[:-len(termination)]
15-
break
16-
sleep_us(10)
17-
return result
18-
19-
uart = myUART(0, baudrate=9600, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1)
20-
21-
uart.write("AT+GMR\r\n")
22-
print(uart.readUntil('OK',maxlen=-1, includeTermination=True))
1+
from machine import UART, Pin
2+
import time
3+
4+
uart1 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9))
5+
6+
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
7+
8+
txData = b'hello world\n\r'
9+
uart1.write(txData)
10+
time.sleep(0.1)
11+
rxData = bytes()
12+
while uart0.any() > 0:
13+
rxData += uart0.read(1)
14+
15+
print(rxData.decode('utf-8'))

0 commit comments

Comments
 (0)