Use new python-serial API

Version 3.0 of python-serial introduced an updated API, we may as well
use it.

This change also uses context-managers for dealing with UARTs as they
are less error-prone and the code is much cleaner/shorter.
This commit is contained in:
Sam Bristow 2018-10-09 23:39:43 +13:00
parent eff11b83fe
commit 783bfacee8
2 changed files with 6 additions and 13 deletions

View file

@ -198,13 +198,10 @@ import serial
UART.setup("UART1")
ser = serial.Serial(port = "/dev/ttyO1", baudrate=9600)
ser.close()
ser.open()
if ser.isOpen():
with serial.Serial(port = "/dev/ttyO1", baudrate=9600) as ser:
print("Serial is open!")
ser.write("Hello World!")
ser.close()
ser.write(b"Hello World!")
```
* Available UART names on BeagleBone
* `UART1`: /dev/ttyO1, Rx: P9_26, Tx: P9_24

View file

@ -14,13 +14,9 @@ Example::
UART.setup("UART1")
ser = serial.Serial(port = "/dev/ttyO1", baudrate=9600)
ser.close()
ser.open()
if ser.isOpen():
print "Serial is open!"
ser.write("Hello World!")
ser.close()
with serial.Serial(port = "/dev/ttyO1", baudrate=9600) as ser:
print("Serial is open!")
ser.write(b"Hello World!")
.. module:: Adafruit_BBIO.UART