How to get UART serial ports working on the beaglebone black

Beaglebone Black serial port connection

How to connect serial ports on the BBB, enable them and then user them.

How to get UART serial ports working on the beaglebone black. The BBB by default has the UART pins set as normal GPIO pins so they need to be changed to UART pins first before they will work.

I saw the problem with trying a few different programming languages including python and Node-Red. With Node-Red it looked like it connected but it just didn’t receive messages being sent or what appeared to be being sent. Node-Red would indicated the serial port was connected.

This short tutorial creates a small script which is run when the BBB starts up before your application runs. This instruction enables UART4 (ttyS4).

This is a command to enter on command line
This is text to past into the 
file you are creating
This is output text, ie text echoed by a script

The instruction uses nano as the command line text editor. Saving and exiting nano is <Ctrl>x then y for yes to save then <enter>.

Change to root user

sudo su -

Create /usr/local/sbin/setTty.sh which is the script that will configure the UART serial port on the Beaglebone Black

nano /usr/local/sbin/setTty.sh
#!/bin/sh
config-pin p9.11 uart
config-pin p9.13 uart
stty -F /dev/ttyO4 sane

FYI: The ‘sane’ on the stty line sets the port to sane settings which are designed to work for the majority of settings.

Make the file executable for all users and writable for root user

chmod 755 /usr/local/sbin/setTty.sh

Now create the test script /usr/local/sbin/testTty.sh

nano /usr/local/sbin/testTty.sh
#!/bin/sh
echo "/dev/ttyO4 state is"
stty -F /dev/ttyO4

Make the file executable for all users and writable for root only

chmod 755 /usr/local/sbin/testTty.sh

Create the service configuration file that makes the system run it on startup

nano /lib/systemd/system/setTty.service
[Unit]
Description=Configure pins to enable ttyS4 as a UART
After=syslog.target network.target
[Service]
Type=simple
ExecStart=/usr/local/sbin/setTty.sh
[Install]
WantedBy=multi-user.target

Enable the service to run on startup

systemctl enable setTty.service
Created symlink /etc/systemd/system/multi-user.target.wants/setTty.service ? /lib/systemd/system/setTty.service.

Now start and test:

systemctl start setTty.service
/usr/local/sbin/testTty.sh

Success results in the following output:

/dev/ttyO4 state is
speed 9600 baud; line = 0;

Failure results in the following output:

/dev/ttyO4 state is
speed 9600 baud; line = 0;
min = 1; time = 0;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

Now restart and run the test again to make sure it is working on boot:

shutdown -r now

Once rebooted, log back in (as a normal non-root user) and run the test again

/usr/local/sbin/testTty.sh

This was written and tested on the Beaglebone black running the bone-debian-9.3-iot-armhf-2018-01-28-4gb image.

Thanks to the following posts for information that enabled this tutorial:

Richard’s response to Michael’s question at https://groups.google.com/forum/#!topic/node-red/mKJNYdv3-wo

http://mybeagleboneblackfindings.blogspot.com.au/2013/10/running-script-on-beaglebone-black-boot.html