raspberrypi / pico-micropython-examples

Examples to accompany the "Raspberry Pi Pico Python SDK" book.
BSD 3-Clause "New" or "Revised" License
1.01k stars 228 forks source link

A potentially better example for UART on the Pico #22

Closed 7west closed 3 years ago

7west commented 3 years ago

This simple example should help users access the basic functionality of UART with MicroPython on the Pico

aallan commented 3 years ago

@benevpi Could you take a look, LGTM?

benevpi commented 3 years ago

I completely agree with the comments in the linked thread about the previous example requiring users to know a bit more about MicroPython than is really necessary to access the hardware. Plus the AT command is a bit of unexplained magic in the previous example.

This is fine, but I'd be tempted to do something looping back so you don't need extra hardware to run it. The following works if you connect GPIO 8 to 1 and 9 to 0

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(1)
rxData = bytes()
while uart0.any() > 0:
    uart1.write(txData)
    time.sleep(1)
    rxData = uart0.readline()
    print(rxData.decode('utf-8'))
7west commented 3 years ago

I like the idea of not needing extra hardware! Probably goes without saying, but maybe there should be a comment in the code or a sentence in the book mentioning how the pins should be hooked up for this to work.

7west commented 3 years ago

Sorry for the multiple comments. I think your example is a little off. It prints "hello world" twice, then reads it with uart0. And then keeps reading it and writing it. Maybe this would be better:

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'
time.sleep(1)
rxData = bytes()
while uart0.any() > 0:
    rxData = uart0.readline()
    print(rxData.decode('utf-8'))

Also I prefer ".read" over ".readline" because some UART connections don't automatically include the "\n\r", but that's your call.

lurch commented 3 years ago

@7west In your most recent example you seem to have forgotten to write to uart1? :laughing:

7west commented 3 years ago

Yes. I got a little crazy with the backspace key. There should be a "uart1.write(txData)" before the time.sleep.

aallan commented 3 years ago

Do you want to update the PR?

benevpi commented 3 years ago

using read over readline is fine with me. Makes sense that it could cause problems on systems without line endings.

lurch commented 3 years ago

...and if you fancy knocking up a quick README and a wiring diagram showing how to do the UART-crossover (something like https://github.com/raspberrypi/pico-micropython-examples/tree/master/i2c/1306oled ) that'd be fantastic :+1: (but don't worry if not)

7west commented 3 years ago

I can do the readme! I'll give the fritzing a shot. Give me a day or two, if you can.

7west commented 3 years ago

I think I've got it all ready. I changed the read() in the while loop to append to the 'rxData' variable, that way people can kinda do whatever they want with the data they've received instead of printing and losing it. I also shortened the sleep to 0.1 seconds. I hope that is okay.

It took 4 commits because I'm pretty new to this and I was initially a little hasty. Thank you for letting me contribute!

benevpi commented 3 years ago

I've tested it and can confirm it works. Thanks for this @7west.

I'll add my approval, but I don't have permissions to merge it. @aallan do you want to do the honours?

aallan commented 3 years ago

Documentation updated. This will be published in the next PDF documentation release which will probably be next week.