The Adafruit IO Python library will export the UART device tree overlays as a convenience. There are five serial ports brought to the expansion headers (UART3 only has a single direction, TX), and one (UART0) with dedicated headers that aren't available to use in your Python programs.


To setup and export the UART, you can do the following:
import Adafruit_BBIO.UART as UART

That's it!

Also, there is a cleanup() method ready to go, but it's not currently working due to a bug in the kernel that causes kernel panics when unloading device tree overlays. We'll update this when it's working. A workaround is to either leave the UART enabled, or restart your BeagleBone Black.

Pin Table for UART

UART1 P9_26 P9_24 P9_20 P9_19 /dev/ttyO1
UART2 P9_22 P9_21
P9_42 P8_36 P8_34 /dev/ttyO3
UART4 P9_11 P9_13 P8_35 P8_33 /dev/ttyO4
UART5 P8_38 P8_37 P8_31 P8_32 /dev/ttyO5

Using UART with Python

You can use the pyserial module in Python, but you'll first need to install it using pip. If you don't have pip installed, you can follow the instructions on the installation pages for this tutorial.

SSH into the BeagleBone Black, and execute the following command:
pip install pyserial
Below is a very simple python program that is a good starting point. Save it to a file, and execute it with 'python'
import Adafruit_BBIO.UART as UART
import serial


ser = serial.Serial(port = "/dev/ttyO1", baudrate=9600)
if ser.isOpen():
	print "Serial is open!"
    ser.write("Hello World!")

# Eventually, you'll want to clean up, but leave this commented for now, 
# as it doesn't work yet

Testing and Using the UART

You can easily test that everything is working, without having to code anything, or installing any other dependencies to get started.

Next, you'll want to connect two wires to the UART pins. We're just going to cross the RX/TX of the UART1 and UART2. The first wire should connect from P9_24 to P9_22. The second wire should connect from P9_26 to P9_21.
Next, export the UART1 and UART2 in the python interpreter with the Adafruit IO library:
[email protected]:~# python
Python 2.7.3 (default, May 29 2013, 21:25:00) 
[GCC 4.7.3 20130205 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Adafruit_BBIO.UART as UART
>>> UART.setup("UART1")
>>> UART.setup("UART2")
>>> exit()
Once you've done that, execute the following commands to launch minicom (using two separate ssh sessions using your terminal of choice, mine are separated by tabs in OS X):
#first terminal window:
minicom -b 9600 -D /dev/ttyO1

#second terminal window:
minicom -b 9600 -D /dev/ttyO2
It should look like this once you've opened minicom:
Now, type in one of the terminal windows, and hit enter. Look at the other terminal, and you should see something like this:
You can exit from minicom by typing Ctrl-A, then Z, then X, and hit enter at the dialog.
Last updated on 2015-05-04 at 04.27.11 PM Published on 2013-06-13 at 05.16.24 PM