Serial Port Communication - chaolunner/RaspberryPi GitHub Wiki
This is a tutorial about how to setup bluetooth on the raspberry pi and how to use serial port to communiate with raspberry pi.
UART configuration
For details, please click on the title link above. In brief,
The SoCs used on the Raspberry Pis have two built-in UARTs, a PL011 and a mini UART.
(Raspberry Pi 3 and Raspberry Pi Zero W), the PL011 UART is connected to the Bluetooth module
, while the mini UART is used for Linux console output
. On all other models the PL011 is used for the Linux console output
.
The particular deficiencies of the mini UART compared to the PL011 are:
- No break detection
- No framing errors detection
- No parity bit
- No receive timeout interrupt
- No DCD, DSR, DTR or RI signals
So if you want to communicate through a USB connection, you may want to use a stronger PL011 UART on the Linux console output
(This will inevitably affect the function of Bluetooth module
). The specific implementation steps are as follows:
-
First, find the
/boot/overlays/pi3-miniuart-bt-overlay.dtb
file or you need to download pi3-miniuart-bt. Refer to/boot/overlays/README
for details on Device Tree Overlays. -
Use the command
ls -l /dev
in the Raspberry Pi command terminal to view the current serial port mapping relation of Raspberry Pi 3. By default you should see the following output:serial0 -> ttyS0 serial1 -> ttyAMA0
Linux console output =
serial0
, mini UART =ttyS0
Bluetooth module =
serial1
, PL011 UART =ttyAMA0
-
Next we need to edit the
/boot/config.txt
file, open the file either on your desktop computer or using the Raspberry Pi via SSHsudo nano /boot/config.txt
-
You need to add the following lines:
dtoverlay=pi3-miniuart-bt-overlay
If you want to minimize the impact of Bluetooth module functionality, you may also need to add
force_turbo=1
For more information, please refer to force_turbo
-
Exit and save your changes
https://github.com/chaolunner/RaspberryPi/wiki/images/config-text.png
-
Now edit
/boot/cmdline.txt
sudo nano /boot/cmdline.txt
-
Change the file to the following:
dwc_otg.lpm_enable=0 console=serial1,115200 console=tty1 root=/dev/mmcblk0p2 kgdboc=serial1,115200 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
-
Exit and save your changes
-
If you want to disable the built in bluetooth you need to stop hciattach trying to use the modem via uart0 which will disable the relevant systemd service
sudo systemctl disable hciuart
-
Now edit /lib/systemd/system/hciuart.server and replace ttyAMA0 with ttyS0.
sudo nano /lib/systemd/system/hciuart.service
-
Replace ttyAMA0 with ttyS0
-
Exit and save your changes
https://github.com/chaolunner/RaspberryPi/wiki/images/hciuart-service.png
-
You need to update the operating system with the latest patches with:
sudo apt-get update
sudo apt-get upgrade
sudo reboot
-
Once your Raspberry Pi 3 has rebooted you should now have access to the serial console via the GPIO header at 115,200 baud.
https://github.com/chaolunner/RaspberryPi/wiki/images/uart-login-success.png
-
If you want to use the hardware UART without the serial console you do not need to make the changes to the cmdline.txt file as shown above.
Bluetooth
-
In a terminal on the Pi, run this command
sdptool browse local
If you encounter the situation shown in the following figure:
https://github.com/chaolunner/RaspberryPi/wiki/images/sdptool-browse-local.png
-
First, you need to setup the Serial Port Profile (SPP) on the Raspberry Pi. Edit this file:
sudo nano /etc/systemd/system/dbus-org.bluez.service
-
Add ' -C' at the end of the 'ExecStart=' line, to start the bluetooth daemon in 'compatibility' mode. Add a new 'ExecStartPost=' immediately after that line, to add the SP Profile. The two lines should look like this:
ExecStart=/usr/lib/bluetooth/bluetoothd -C ExecStartPost=/usr/bin/sdptool add SP
https://github.com/chaolunner/RaspberryPi/wiki/images/serial-port-profile.png
-
Save and reboot.
-
Then if you run this command
sdptool browse local
again, you should see the results shown in the following picturehttps://github.com/chaolunner/RaspberryPi/wiki/images/sdptool-browse-local-success.png
-
-
Turn on Bluetooth and enable
Make Discoverable
on the Pi. -
Two way to make the rfcomm watch hci0:
-
The first, In a terminal on the Pi, run this command every time after reboot:
sudo rfcomm watch hci0
-
The second:
-
You can automate the rfcomm command with a service unit. Create a new file with sudo
sudo nano /etc/systemd/system/rfcomm.service
-
and enter the following
[Unit] Description=RFCOMM service After=bluetooth.service Requires=bluetooth.service [Service] ExecStart=/usr/bin/rfcomm watch hci0 [Install] WantedBy=multi-user.target
https://github.com/chaolunner/RaspberryPi/wiki/images/rfcomm-service.png
-
Enable that to start at boot time with
sudo systemctl enable rfcomm
-
Now either reboot or start it manually with
sudo systemctl start rfcomm
You also can use
systemctl status rfcomm
check the result of rfcomm service. If the operation is correct, you should see the following results:https://github.com/chaolunner/RaspberryPi/wiki/images/systemctl-status-rfcomm.png
-
-
-
Use your devices eg. MacBook to connect with the Pi.
-
Connection on Mac
Port confirmation.
https://github.com/chaolunner/RaspberryPi/wiki/images/port-confirmation.png
Connect with
screen /dev/tty.raspberrypi-SerialPort
-
Connection confirmation
On the Pi, open another terminal and enter the following command
sudo echo "hello" > /dev/rfcomm0
If the terminal on the Mac has a hello display, it is successful.
Code by Python
-
Install pySerial
pip install pyserial
https://github.com/chaolunner/RaspberryPi/wiki/images/install-pyserial.png
-
Use Terminal Confirmation Port
python -m serials.tools.list_ports
https://github.com/chaolunner/RaspberryPi/wiki/images/serials-tools-list_ports.png
-
Use Python Confirmation Port
import serial.tools.list_ports comports = list(serial.tools.list_ports.comports()) if len(comports) <= 0: print("No port found!") else: for list_port_info in comports: print(list_port_info.device)
Communiate with Unity
The most important thing is not to forget to add a newline character to the end of the data to be sent! Otherwise, when you use the ReadLine() method, the program may be block.
-
Raspberry Pi Part
-
Unity Part
Linux User and Group Manage Commands
-
cat /etc/group
- Check the users in the groups. -
cat /etc/group | grep bluetooth
- Check the users in the bluetooth group. -
sudo usermod -G bluetooth -a pi
- Appendpi
user in the bluetooth group. -
sudo gpasswd -d pi bluetooth
- Removepi
user in the bluetooth group.
Reference
Raspberry Pi 3 UART Overlay Workaround