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.

Setup

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

UART.setup("UART1")
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

UART RX TX CTS RTS Device
UART1 P9_26 P9_24 P9_20 P9_19 /dev/ttyO1
UART2 P9_22 P9_21
/dev/ttyO2
UART3
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 file_name.py'
import Adafruit_BBIO.UART as UART
import serial

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()

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

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:
root@beaglebone:~# 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.

This guide was first published on Jun 13, 2013. It was last updated on Mar 08, 2024.

This page (UART) was last updated on Jul 23, 2013.

Text editor powered by tinymce.