Setup
To setup and export the UART, you can do the following:import Adafruit_BBIO.UART as UART UART.setup("UART1")
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
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.
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()
#first terminal window: minicom -b 9600 -D /dev/ttyO1 #second terminal window: minicom -b 9600 -D /dev/ttyO2