Skip to content

Commit 783bfac

Browse files
committed
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.
1 parent eff11b8 commit 783bfac

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,10 @@ import serial
198198
199199
UART.setup("UART1")
200200
201-
ser = serial.Serial(port = "/dev/ttyO1", baudrate=9600)
202-
ser.close()
203-
ser.open()
204-
if ser.isOpen():
201+
with serial.Serial(port = "/dev/ttyO1", baudrate=9600) as ser:
205202
print("Serial is open!")
206-
ser.write("Hello World!")
207-
ser.close()
203+
ser.write(b"Hello World!")
204+
208205
```
209206
* Available UART names on BeagleBone
210207
* `UART1`: /dev/ttyO1, Rx: P9_26, Tx: P9_24

docs/UART.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ Example::
1414

1515
UART.setup("UART1")
1616

17-
ser = serial.Serial(port = "/dev/ttyO1", baudrate=9600)
18-
ser.close()
19-
ser.open()
20-
if ser.isOpen():
21-
print "Serial is open!"
22-
ser.write("Hello World!")
23-
ser.close()
17+
with serial.Serial(port = "/dev/ttyO1", baudrate=9600) as ser:
18+
print("Serial is open!")
19+
ser.write(b"Hello World!")
2420

2521
.. module:: Adafruit_BBIO.UART
2622

0 commit comments

Comments
 (0)