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 SSH

    sudo 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

Code by Python

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.

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 - Append pi user in the bluetooth group.

  • sudo gpasswd -d pi bluetooth - Remove pi user in the bluetooth group.

Reference

Raspberry Pi 3 UART Overlay Workaround

RaspberryPi (jessie)とMac間のBluetooth通信

SerialPort または Uniduino を使った Unity と Arduino を連携させる方法調べてみた